xref: /petsc/src/mat/interface/matrix.c (revision 0e9bae810fdaeb60e2713eaa8ddb89f42e079fd1)
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 = -1;
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 = -1;
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 = -1;
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 = -1;
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 = MatDenseGetArray(B,&bb);CHKERRQ(ierr);
3114   ierr = MatDenseGetArray(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 = MatDenseRestoreArray(B,&bb);CHKERRQ(ierr);
3128   ierr = MatDenseRestoreArray(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, 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, 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(), PetscDrawCreate(), 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__ "MatGetSubMatrices"
6296 /*@C
6297    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
6298    points to an array of valid matrices, they may be reused to store the new
6299    submatrices.
6300 
6301    Collective on Mat
6302 
6303    Input Parameters:
6304 +  mat - the matrix
6305 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6306 .  irow, icol - index sets of rows and columns to extract (must be sorted)
6307 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6308 
6309    Output Parameter:
6310 .  submat - the array of submatrices
6311 
6312    Notes:
6313    MatGetSubMatrices() can extract ONLY sequential submatrices
6314    (from both sequential and parallel matrices). Use MatGetSubMatrix()
6315    to extract a parallel submatrix.
6316 
6317    Currently both row and column indices must be sorted to guarantee
6318    correctness with all matrix types.
6319 
6320    When extracting submatrices from a parallel matrix, each processor can
6321    form a different submatrix by setting the rows and columns of its
6322    individual index sets according to the local submatrix desired.
6323 
6324    When finished using the submatrices, the user should destroy
6325    them with MatDestroyMatrices().
6326 
6327    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6328    original matrix has not changed from that last call to MatGetSubMatrices().
6329 
6330    This routine creates the matrices in submat; you should NOT create them before
6331    calling it. It also allocates the array of matrix pointers submat.
6332 
6333    For BAIJ matrices the index sets must respect the block structure, that is if they
6334    request one row/column in a block, they must request all rows/columns that are in
6335    that block. For example, if the block size is 2 you cannot request just row 0 and
6336    column 0.
6337 
6338    Fortran Note:
6339    The Fortran interface is slightly different from that given below; it
6340    requires one to pass in  as submat a Mat (integer) array of size at least m.
6341 
6342    Level: advanced
6343 
6344    Concepts: matrices^accessing submatrices
6345    Concepts: submatrices
6346 
6347 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6348 @*/
6349 PetscErrorCode  MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6350 {
6351   PetscErrorCode ierr;
6352   PetscInt        i;
6353   PetscBool       eq;
6354 
6355   PetscFunctionBegin;
6356   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6357   PetscValidType(mat,1);
6358   if (n) {
6359     PetscValidPointer(irow,3);
6360     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6361     PetscValidPointer(icol,4);
6362     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6363   }
6364   PetscValidPointer(submat,6);
6365   if (n && scall == MAT_REUSE_MATRIX) {
6366     PetscValidPointer(*submat,6);
6367     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6368   }
6369   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6370   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6371   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6372   MatCheckPreallocated(mat,1);
6373 
6374   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6375   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6376   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6377   for (i=0; i<n; i++) {
6378     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6379       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6380       if (eq) {
6381 	if (mat->symmetric){
6382 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6383 	} else if (mat->hermitian) {
6384 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6385 	} else if (mat->structurally_symmetric) {
6386 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6387 	}
6388       }
6389     }
6390   }
6391   PetscFunctionReturn(0);
6392 }
6393 
6394 #undef __FUNCT__
6395 #define __FUNCT__ "MatGetSubMatricesParallel"
6396 PetscErrorCode  MatGetSubMatricesParallel(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6397 {
6398   PetscErrorCode ierr;
6399   PetscInt        i;
6400   PetscBool       eq;
6401 
6402   PetscFunctionBegin;
6403   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6404   PetscValidType(mat,1);
6405   if (n) {
6406     PetscValidPointer(irow,3);
6407     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6408     PetscValidPointer(icol,4);
6409     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6410   }
6411   PetscValidPointer(submat,6);
6412   if (n && scall == MAT_REUSE_MATRIX) {
6413     PetscValidPointer(*submat,6);
6414     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6415   }
6416   if (!mat->ops->getsubmatricesparallel) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6417   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6418   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6419   MatCheckPreallocated(mat,1);
6420 
6421   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6422   ierr = (*mat->ops->getsubmatricesparallel)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6423   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6424   for (i=0; i<n; i++) {
6425     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6426       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6427       if (eq) {
6428 	if (mat->symmetric){
6429 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6430 	} else if (mat->hermitian) {
6431 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6432 	} else if (mat->structurally_symmetric) {
6433 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6434 	}
6435       }
6436     }
6437   }
6438   PetscFunctionReturn(0);
6439 }
6440 
6441 #undef __FUNCT__
6442 #define __FUNCT__ "MatDestroyMatrices"
6443 /*@C
6444    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
6445 
6446    Collective on Mat
6447 
6448    Input Parameters:
6449 +  n - the number of local matrices
6450 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6451                        sequence of MatGetSubMatrices())
6452 
6453    Level: advanced
6454 
6455     Notes: Frees not only the matrices, but also the array that contains the matrices
6456            In Fortran will not free the array.
6457 
6458 .seealso: MatGetSubMatrices()
6459 @*/
6460 PetscErrorCode  MatDestroyMatrices(PetscInt n,Mat *mat[])
6461 {
6462   PetscErrorCode ierr;
6463   PetscInt       i;
6464 
6465   PetscFunctionBegin;
6466   if (!*mat) PetscFunctionReturn(0);
6467   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
6468   PetscValidPointer(mat,2);
6469   for (i=0; i<n; i++) {
6470     ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr);
6471   }
6472   /* memory is allocated even if n = 0 */
6473   ierr = PetscFree(*mat);CHKERRQ(ierr);
6474   *mat = PETSC_NULL;
6475   PetscFunctionReturn(0);
6476 }
6477 
6478 #undef __FUNCT__
6479 #define __FUNCT__ "MatGetSeqNonzeroStructure"
6480 /*@C
6481    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
6482 
6483    Collective on Mat
6484 
6485    Input Parameters:
6486 .  mat - the matrix
6487 
6488    Output Parameter:
6489 .  matstruct - the sequential matrix with the nonzero structure of mat
6490 
6491   Level: intermediate
6492 
6493 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
6494 @*/
6495 PetscErrorCode  MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
6496 {
6497   PetscErrorCode ierr;
6498 
6499   PetscFunctionBegin;
6500   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6501   PetscValidPointer(matstruct,2);
6502 
6503   PetscValidType(mat,1);
6504   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6505   MatCheckPreallocated(mat,1);
6506 
6507   if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
6508   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6509   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
6510   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6511   PetscFunctionReturn(0);
6512 }
6513 
6514 #undef __FUNCT__
6515 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
6516 /*@C
6517    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
6518 
6519    Collective on Mat
6520 
6521    Input Parameters:
6522 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6523                        sequence of MatGetSequentialNonzeroStructure())
6524 
6525    Level: advanced
6526 
6527     Notes: Frees not only the matrices, but also the array that contains the matrices
6528 
6529 .seealso: MatGetSeqNonzeroStructure()
6530 @*/
6531 PetscErrorCode  MatDestroySeqNonzeroStructure(Mat *mat)
6532 {
6533   PetscErrorCode ierr;
6534 
6535   PetscFunctionBegin;
6536   PetscValidPointer(mat,1);
6537   ierr = MatDestroy(mat);CHKERRQ(ierr);
6538   PetscFunctionReturn(0);
6539 }
6540 
6541 #undef __FUNCT__
6542 #define __FUNCT__ "MatIncreaseOverlap"
6543 /*@
6544    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6545    replaces the index sets by larger ones that represent submatrices with
6546    additional overlap.
6547 
6548    Collective on Mat
6549 
6550    Input Parameters:
6551 +  mat - the matrix
6552 .  n   - the number of index sets
6553 .  is  - the array of index sets (these index sets will changed during the call)
6554 -  ov  - the additional overlap requested
6555 
6556    Level: developer
6557 
6558    Concepts: overlap
6559    Concepts: ASM^computing overlap
6560 
6561 .seealso: MatGetSubMatrices()
6562 @*/
6563 PetscErrorCode  MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6564 {
6565   PetscErrorCode ierr;
6566 
6567   PetscFunctionBegin;
6568   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6569   PetscValidType(mat,1);
6570   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6571   if (n) {
6572     PetscValidPointer(is,3);
6573     PetscValidHeaderSpecific(*is,IS_CLASSID,3);
6574   }
6575   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6576   if (mat->factortype)     SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6577   MatCheckPreallocated(mat,1);
6578 
6579   if (!ov) PetscFunctionReturn(0);
6580   if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6581   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6582   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
6583   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6584   PetscFunctionReturn(0);
6585 }
6586 
6587 #undef __FUNCT__
6588 #define __FUNCT__ "MatGetBlockSize"
6589 /*@
6590    MatGetBlockSize - Returns the matrix block size; useful especially for the
6591    block row and block diagonal formats.
6592 
6593    Not Collective
6594 
6595    Input Parameter:
6596 .  mat - the matrix
6597 
6598    Output Parameter:
6599 .  bs - block size
6600 
6601    Notes:
6602    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6603 
6604    Level: intermediate
6605 
6606    Concepts: matrices^block size
6607 
6608 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
6609 @*/
6610 PetscErrorCode  MatGetBlockSize(Mat mat,PetscInt *bs)
6611 {
6612 
6613   PetscFunctionBegin;
6614   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6615   PetscValidType(mat,1);
6616   PetscValidIntPointer(bs,2);
6617   MatCheckPreallocated(mat,1);
6618   *bs = mat->rmap->bs;
6619   PetscFunctionReturn(0);
6620 }
6621 
6622 #undef __FUNCT__
6623 #define __FUNCT__ "MatGetBlockSizes"
6624 /*@
6625    MatGetBlockSizes - Returns the matrix block row and column sizes;
6626    useful especially for the block row and block diagonal formats.
6627 
6628    Not Collective
6629 
6630    Input Parameter:
6631 .  mat - the matrix
6632 
6633    Output Parameter:
6634 .  rbs - row block size
6635 .  cbs - coumn block size
6636 
6637    Notes:
6638    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6639 
6640    Level: intermediate
6641 
6642    Concepts: matrices^block size
6643 
6644 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6645 @*/
6646 PetscErrorCode  MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
6647 {
6648 
6649   PetscFunctionBegin;
6650   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6651   PetscValidType(mat,1);
6652   if(rbs) PetscValidIntPointer(rbs,2);
6653   if(cbs) PetscValidIntPointer(cbs,3);
6654   MatCheckPreallocated(mat,1);
6655   if(rbs) *rbs = mat->rmap->bs;
6656   if(cbs) *cbs = mat->cmap->bs;
6657   PetscFunctionReturn(0);
6658 }
6659 
6660 #undef __FUNCT__
6661 #define __FUNCT__ "MatSetBlockSize"
6662 /*@
6663    MatSetBlockSize - Sets the matrix block size.
6664 
6665    Logically Collective on Mat
6666 
6667    Input Parameters:
6668 +  mat - the matrix
6669 -  bs - block size
6670 
6671    Notes:
6672      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6673 
6674    Level: intermediate
6675 
6676    Concepts: matrices^block size
6677 
6678 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6679 @*/
6680 PetscErrorCode  MatSetBlockSize(Mat mat,PetscInt bs)
6681 {
6682   PetscErrorCode ierr;
6683 
6684   PetscFunctionBegin;
6685   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6686   PetscValidLogicalCollectiveInt(mat,bs,2);
6687   ierr = PetscLayoutSetBlockSize(mat->rmap,bs);CHKERRQ(ierr);
6688   ierr = PetscLayoutSetBlockSize(mat->cmap,bs);CHKERRQ(ierr);
6689   PetscFunctionReturn(0);
6690 }
6691 
6692 #undef __FUNCT__
6693 #define __FUNCT__ "MatSetBlockSizes"
6694 /*@
6695    MatSetBlockSizes - Sets the matrix block row and column sizes.
6696 
6697    Logically Collective on Mat
6698 
6699    Input Parameters:
6700 +  mat - the matrix
6701 -  rbs - row block size
6702 -  cbs - column block size
6703 
6704    Notes:
6705      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6706 
6707    Level: intermediate
6708 
6709    Concepts: matrices^block size
6710 
6711 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6712 @*/
6713 PetscErrorCode  MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
6714 {
6715   PetscErrorCode ierr;
6716 
6717   PetscFunctionBegin;
6718   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6719   PetscValidLogicalCollectiveInt(mat,rbs,2);
6720   ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr);
6721   ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr);
6722   PetscFunctionReturn(0);
6723 }
6724 
6725 #undef __FUNCT__
6726 #define __FUNCT__ "MatGetRowIJ"
6727 /*@C
6728     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
6729 
6730    Collective on Mat
6731 
6732     Input Parameters:
6733 +   mat - the matrix
6734 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
6735 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
6736 -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
6737                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6738                  always used.
6739 
6740     Output Parameters:
6741 +   n - number of rows in the (possibly compressed) matrix
6742 .   ia - the row pointers [of length n+1]
6743 .   ja - the column indices
6744 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
6745            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
6746 
6747     Level: developer
6748 
6749     Notes: You CANNOT change any of the ia[] or ja[] values.
6750 
6751            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
6752 
6753     Fortran Node
6754 
6755            In Fortran use
6756 $           PetscInt ia(1), ja(1)
6757 $           PetscOffset iia, jja
6758 $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
6759 $
6760 $          or
6761 $
6762 $           PetscScalar, pointer :: xx_v(:)
6763 $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
6764 
6765 
6766        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
6767 
6768 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
6769 @*/
6770 PetscErrorCode  MatGetRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6771 {
6772   PetscErrorCode ierr;
6773 
6774   PetscFunctionBegin;
6775   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6776   PetscValidType(mat,1);
6777   PetscValidIntPointer(n,4);
6778   if (ia) PetscValidIntPointer(ia,5);
6779   if (ja) PetscValidIntPointer(ja,6);
6780   PetscValidIntPointer(done,7);
6781   MatCheckPreallocated(mat,1);
6782   if (!mat->ops->getrowij) *done = PETSC_FALSE;
6783   else {
6784     *done = PETSC_TRUE;
6785     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6786     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6787     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6788   }
6789   PetscFunctionReturn(0);
6790 }
6791 
6792 #undef __FUNCT__
6793 #define __FUNCT__ "MatGetColumnIJ"
6794 /*@C
6795     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
6796 
6797     Collective on Mat
6798 
6799     Input Parameters:
6800 +   mat - the matrix
6801 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6802 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6803                 symmetrized
6804 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6805                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6806                  always used.
6807 
6808     Output Parameters:
6809 +   n - number of columns in the (possibly compressed) matrix
6810 .   ia - the column pointers
6811 .   ja - the row indices
6812 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
6813 
6814     Level: developer
6815 
6816 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6817 @*/
6818 PetscErrorCode  MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6819 {
6820   PetscErrorCode ierr;
6821 
6822   PetscFunctionBegin;
6823   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6824   PetscValidType(mat,1);
6825   PetscValidIntPointer(n,4);
6826   if (ia) PetscValidIntPointer(ia,5);
6827   if (ja) PetscValidIntPointer(ja,6);
6828   PetscValidIntPointer(done,7);
6829   MatCheckPreallocated(mat,1);
6830   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
6831   else {
6832     *done = PETSC_TRUE;
6833     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6834   }
6835   PetscFunctionReturn(0);
6836 }
6837 
6838 #undef __FUNCT__
6839 #define __FUNCT__ "MatRestoreRowIJ"
6840 /*@C
6841     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
6842     MatGetRowIJ().
6843 
6844     Collective on Mat
6845 
6846     Input Parameters:
6847 +   mat - the matrix
6848 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6849 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6850                 symmetrized
6851 -   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6852                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6853                  always used.
6854 
6855     Output Parameters:
6856 +   n - size of (possibly compressed) matrix
6857 .   ia - the row pointers
6858 .   ja - the column indices
6859 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6860 
6861     Level: developer
6862 
6863 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6864 @*/
6865 PetscErrorCode  MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6866 {
6867   PetscErrorCode ierr;
6868 
6869   PetscFunctionBegin;
6870   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6871   PetscValidType(mat,1);
6872   if (ia) PetscValidIntPointer(ia,5);
6873   if (ja) PetscValidIntPointer(ja,6);
6874   PetscValidIntPointer(done,7);
6875   MatCheckPreallocated(mat,1);
6876 
6877   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
6878   else {
6879     *done = PETSC_TRUE;
6880     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6881   }
6882   PetscFunctionReturn(0);
6883 }
6884 
6885 #undef __FUNCT__
6886 #define __FUNCT__ "MatRestoreColumnIJ"
6887 /*@C
6888     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
6889     MatGetColumnIJ().
6890 
6891     Collective on Mat
6892 
6893     Input Parameters:
6894 +   mat - the matrix
6895 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6896 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6897                 symmetrized
6898 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6899                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6900                  always used.
6901 
6902     Output Parameters:
6903 +   n - size of (possibly compressed) matrix
6904 .   ia - the column pointers
6905 .   ja - the row indices
6906 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6907 
6908     Level: developer
6909 
6910 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
6911 @*/
6912 PetscErrorCode  MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6913 {
6914   PetscErrorCode ierr;
6915 
6916   PetscFunctionBegin;
6917   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6918   PetscValidType(mat,1);
6919   if (ia) PetscValidIntPointer(ia,5);
6920   if (ja) PetscValidIntPointer(ja,6);
6921   PetscValidIntPointer(done,7);
6922   MatCheckPreallocated(mat,1);
6923 
6924   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
6925   else {
6926     *done = PETSC_TRUE;
6927     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6928   }
6929   PetscFunctionReturn(0);
6930 }
6931 
6932 #undef __FUNCT__
6933 #define __FUNCT__ "MatColoringPatch"
6934 /*@C
6935     MatColoringPatch -Used inside matrix coloring routines that
6936     use MatGetRowIJ() and/or MatGetColumnIJ().
6937 
6938     Collective on Mat
6939 
6940     Input Parameters:
6941 +   mat - the matrix
6942 .   ncolors - max color value
6943 .   n   - number of entries in colorarray
6944 -   colorarray - array indicating color for each column
6945 
6946     Output Parameters:
6947 .   iscoloring - coloring generated using colorarray information
6948 
6949     Level: developer
6950 
6951 .seealso: MatGetRowIJ(), MatGetColumnIJ()
6952 
6953 @*/
6954 PetscErrorCode  MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
6955 {
6956   PetscErrorCode ierr;
6957 
6958   PetscFunctionBegin;
6959   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6960   PetscValidType(mat,1);
6961   PetscValidIntPointer(colorarray,4);
6962   PetscValidPointer(iscoloring,5);
6963   MatCheckPreallocated(mat,1);
6964 
6965   if (!mat->ops->coloringpatch){
6966     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6967   } else {
6968     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6969   }
6970   PetscFunctionReturn(0);
6971 }
6972 
6973 
6974 #undef __FUNCT__
6975 #define __FUNCT__ "MatSetUnfactored"
6976 /*@
6977    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
6978 
6979    Logically Collective on Mat
6980 
6981    Input Parameter:
6982 .  mat - the factored matrix to be reset
6983 
6984    Notes:
6985    This routine should be used only with factored matrices formed by in-place
6986    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
6987    format).  This option can save memory, for example, when solving nonlinear
6988    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
6989    ILU(0) preconditioner.
6990 
6991    Note that one can specify in-place ILU(0) factorization by calling
6992 .vb
6993      PCType(pc,PCILU);
6994      PCFactorSeUseInPlace(pc);
6995 .ve
6996    or by using the options -pc_type ilu -pc_factor_in_place
6997 
6998    In-place factorization ILU(0) can also be used as a local
6999    solver for the blocks within the block Jacobi or additive Schwarz
7000    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
7001    of these preconditioners in the <a href="../../docs/manual.pdf#ch_pc">PC chapter of the users manual</a> for details on setting
7002    local solver options.
7003 
7004    Most users should employ the simplified KSP interface for linear solvers
7005    instead of working directly with matrix algebra routines such as this.
7006    See, e.g., KSPCreate().
7007 
7008    Level: developer
7009 
7010 .seealso: PCFactorSetUseInPlace()
7011 
7012    Concepts: matrices^unfactored
7013 
7014 @*/
7015 PetscErrorCode  MatSetUnfactored(Mat mat)
7016 {
7017   PetscErrorCode ierr;
7018 
7019   PetscFunctionBegin;
7020   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7021   PetscValidType(mat,1);
7022   MatCheckPreallocated(mat,1);
7023   mat->factortype = MAT_FACTOR_NONE;
7024   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
7025   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
7026   PetscFunctionReturn(0);
7027 }
7028 
7029 /*MC
7030     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.
7031 
7032     Synopsis:
7033     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)
7034 
7035     Not collective
7036 
7037     Input Parameter:
7038 .   x - matrix
7039 
7040     Output Parameters:
7041 +   xx_v - the Fortran90 pointer to the array
7042 -   ierr - error code
7043 
7044     Example of Usage:
7045 .vb
7046       PetscScalar, pointer xx_v(:,:)
7047       ....
7048       call MatDenseGetArrayF90(x,xx_v,ierr)
7049       a = xx_v(3)
7050       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7051 .ve
7052 
7053     Level: advanced
7054 
7055 .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray()
7056 
7057     Concepts: matrices^accessing array
7058 
7059 M*/
7060 
7061 /*MC
7062     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7063     accessed with MatGetArrayF90().
7064 
7065     Synopsis:
7066     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
7067 
7068     Not collective
7069 
7070     Input Parameters:
7071 +   x - matrix
7072 -   xx_v - the Fortran90 pointer to the array
7073 
7074     Output Parameter:
7075 .   ierr - error code
7076 
7077     Example of Usage:
7078 .vb
7079        PetscScalar, pointer xx_v(:)
7080        ....
7081        call MatDenseGetArrayF90(x,xx_v,ierr)
7082        a = xx_v(3)
7083        call MatDenseRestoreArrayF90(x,xx_v,ierr)
7084 .ve
7085 
7086     Level: advanced
7087 
7088 .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray()
7089 
7090 M*/
7091 
7092 
7093 #undef __FUNCT__
7094 #define __FUNCT__ "MatGetSubMatrix"
7095 /*@
7096     MatGetSubMatrix - Gets a single submatrix on the same number of processors
7097                       as the original matrix.
7098 
7099     Collective on Mat
7100 
7101     Input Parameters:
7102 +   mat - the original matrix
7103 .   isrow - parallel IS containing the rows this processor should obtain
7104 .   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.
7105 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7106 
7107     Output Parameter:
7108 .   newmat - the new submatrix, of the same type as the old
7109 
7110     Level: advanced
7111 
7112     Notes:
7113     The submatrix will be able to be multiplied with vectors using the same layout as iscol.
7114 
7115     The rows in isrow will be sorted into the same order as the original matrix on each process.
7116 
7117       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7118    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
7119    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7120    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7121    you are finished using it.
7122 
7123     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7124     the input matrix.
7125 
7126     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran).
7127 
7128    Example usage:
7129    Consider the following 8x8 matrix with 34 non-zero values, that is
7130    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7131    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7132    as follows:
7133 
7134 .vb
7135             1  2  0  |  0  3  0  |  0  4
7136     Proc0   0  5  6  |  7  0  0  |  8  0
7137             9  0 10  | 11  0  0  | 12  0
7138     -------------------------------------
7139            13  0 14  | 15 16 17  |  0  0
7140     Proc1   0 18  0  | 19 20 21  |  0  0
7141             0  0  0  | 22 23  0  | 24  0
7142     -------------------------------------
7143     Proc2  25 26 27  |  0  0 28  | 29  0
7144            30  0  0  | 31 32 33  |  0 34
7145 .ve
7146 
7147     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is
7148 
7149 .vb
7150             2  0  |  0  3  0  |  0
7151     Proc0   5  6  |  7  0  0  |  8
7152     -------------------------------
7153     Proc1  18  0  | 19 20 21  |  0
7154     -------------------------------
7155     Proc2  26 27  |  0  0 28  | 29
7156             0  0  | 31 32 33  |  0
7157 .ve
7158 
7159 
7160     Concepts: matrices^submatrices
7161 
7162 .seealso: MatGetSubMatrices()
7163 @*/
7164 PetscErrorCode  MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7165 {
7166   PetscErrorCode ierr;
7167   PetscMPIInt    size;
7168   Mat            *local;
7169   IS             iscoltmp;
7170 
7171   PetscFunctionBegin;
7172   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7173   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
7174   if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
7175   PetscValidPointer(newmat,5);
7176   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5);
7177   PetscValidType(mat,1);
7178   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7179   MatCheckPreallocated(mat,1);
7180   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7181 
7182   if (!iscol) {
7183     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr);
7184   } else {
7185     iscoltmp = iscol;
7186   }
7187 
7188   /* if original matrix is on just one processor then use submatrix generated */
7189   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7190     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
7191     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7192     PetscFunctionReturn(0);
7193   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
7194     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
7195     *newmat = *local;
7196     ierr    = PetscFree(local);CHKERRQ(ierr);
7197     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7198     PetscFunctionReturn(0);
7199   } else if (!mat->ops->getsubmatrix) {
7200     /* Create a new matrix type that implements the operation using the full matrix */
7201     switch (cll) {
7202       case MAT_INITIAL_MATRIX:
7203         ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr);
7204         break;
7205       case MAT_REUSE_MATRIX:
7206         ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr);
7207         break;
7208       default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
7209     }
7210     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7211     PetscFunctionReturn(0);
7212   }
7213 
7214   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7215   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr);
7216   if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7217   if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);}
7218   PetscFunctionReturn(0);
7219 }
7220 
7221 #undef __FUNCT__
7222 #define __FUNCT__ "MatStashSetInitialSize"
7223 /*@
7224    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
7225    used during the assembly process to store values that belong to
7226    other processors.
7227 
7228    Not Collective
7229 
7230    Input Parameters:
7231 +  mat   - the matrix
7232 .  size  - the initial size of the stash.
7233 -  bsize - the initial size of the block-stash(if used).
7234 
7235    Options Database Keys:
7236 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
7237 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
7238 
7239    Level: intermediate
7240 
7241    Notes:
7242      The block-stash is used for values set with MatSetValuesBlocked() while
7243      the stash is used for values set with MatSetValues()
7244 
7245      Run with the option -info and look for output of the form
7246      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
7247      to determine the appropriate value, MM, to use for size and
7248      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
7249      to determine the value, BMM to use for bsize
7250 
7251    Concepts: stash^setting matrix size
7252    Concepts: matrices^stash
7253 
7254 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()
7255 
7256 @*/
7257 PetscErrorCode  MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
7258 {
7259   PetscErrorCode ierr;
7260 
7261   PetscFunctionBegin;
7262   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7263   PetscValidType(mat,1);
7264   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
7265   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
7266   PetscFunctionReturn(0);
7267 }
7268 
7269 #undef __FUNCT__
7270 #define __FUNCT__ "MatInterpolateAdd"
7271 /*@
7272    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
7273      the matrix
7274 
7275    Neighbor-wise Collective on Mat
7276 
7277    Input Parameters:
7278 +  mat   - the matrix
7279 .  x,y - the vectors
7280 -  w - where the result is stored
7281 
7282    Level: intermediate
7283 
7284    Notes:
7285     w may be the same vector as y.
7286 
7287     This allows one to use either the restriction or interpolation (its transpose)
7288     matrix to do the interpolation
7289 
7290     Concepts: interpolation
7291 
7292 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7293 
7294 @*/
7295 PetscErrorCode  MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
7296 {
7297   PetscErrorCode ierr;
7298   PetscInt       M,N,Ny;
7299 
7300   PetscFunctionBegin;
7301   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7302   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7303   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7304   PetscValidHeaderSpecific(w,VEC_CLASSID,4);
7305   PetscValidType(A,1);
7306   MatCheckPreallocated(A,1);
7307   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7308   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7309   if (M == Ny) {
7310     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
7311   } else {
7312     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
7313   }
7314   PetscFunctionReturn(0);
7315 }
7316 
7317 #undef __FUNCT__
7318 #define __FUNCT__ "MatInterpolate"
7319 /*@
7320    MatInterpolate - y = A*x or A'*x depending on the shape of
7321      the matrix
7322 
7323    Neighbor-wise Collective on Mat
7324 
7325    Input Parameters:
7326 +  mat   - the matrix
7327 -  x,y - the vectors
7328 
7329    Level: intermediate
7330 
7331    Notes:
7332     This allows one to use either the restriction or interpolation (its transpose)
7333     matrix to do the interpolation
7334 
7335    Concepts: matrices^interpolation
7336 
7337 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7338 
7339 @*/
7340 PetscErrorCode  MatInterpolate(Mat A,Vec x,Vec y)
7341 {
7342   PetscErrorCode ierr;
7343   PetscInt       M,N,Ny;
7344 
7345   PetscFunctionBegin;
7346   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7347   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7348   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7349   PetscValidType(A,1);
7350   MatCheckPreallocated(A,1);
7351   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7352   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7353   if (M == Ny) {
7354     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7355   } else {
7356     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7357   }
7358   PetscFunctionReturn(0);
7359 }
7360 
7361 #undef __FUNCT__
7362 #define __FUNCT__ "MatRestrict"
7363 /*@
7364    MatRestrict - y = A*x or A'*x
7365 
7366    Neighbor-wise Collective on Mat
7367 
7368    Input Parameters:
7369 +  mat   - the matrix
7370 -  x,y - the vectors
7371 
7372    Level: intermediate
7373 
7374    Notes:
7375     This allows one to use either the restriction or interpolation (its transpose)
7376     matrix to do the restriction
7377 
7378    Concepts: matrices^restriction
7379 
7380 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
7381 
7382 @*/
7383 PetscErrorCode  MatRestrict(Mat A,Vec x,Vec y)
7384 {
7385   PetscErrorCode ierr;
7386   PetscInt       M,N,Ny;
7387 
7388   PetscFunctionBegin;
7389   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7390   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7391   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7392   PetscValidType(A,1);
7393   MatCheckPreallocated(A,1);
7394 
7395   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7396   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7397   if (M == Ny) {
7398     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7399   } else {
7400     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7401   }
7402   PetscFunctionReturn(0);
7403 }
7404 
7405 #undef __FUNCT__
7406 #define __FUNCT__ "MatGetNullSpace"
7407 /*@
7408    MatGetNullSpace - retrieves the null space to a matrix.
7409 
7410    Logically Collective on Mat and MatNullSpace
7411 
7412    Input Parameters:
7413 +  mat - the matrix
7414 -  nullsp - the null space object
7415 
7416    Level: developer
7417 
7418    Notes:
7419       This null space is used by solvers. Overwrites any previous null space that may have been attached
7420 
7421    Concepts: null space^attaching to matrix
7422 
7423 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7424 @*/
7425 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
7426 {
7427   PetscFunctionBegin;
7428   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7429   PetscValidType(mat,1);
7430   PetscValidPointer(nullsp,2);
7431   *nullsp = mat->nullsp;
7432   PetscFunctionReturn(0);
7433 }
7434 
7435 #undef __FUNCT__
7436 #define __FUNCT__ "MatSetNullSpace"
7437 /*@
7438    MatSetNullSpace - attaches a null space to a matrix.
7439         This null space will be removed from the resulting vector whenever
7440         MatMult() is called
7441 
7442    Logically Collective on Mat and MatNullSpace
7443 
7444    Input Parameters:
7445 +  mat - the matrix
7446 -  nullsp - the null space object
7447 
7448    Level: advanced
7449 
7450    Notes:
7451       This null space is used by solvers. Overwrites any previous null space that may have been attached
7452 
7453    Concepts: null space^attaching to matrix
7454 
7455 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7456 @*/
7457 PetscErrorCode  MatSetNullSpace(Mat mat,MatNullSpace nullsp)
7458 {
7459   PetscErrorCode ierr;
7460 
7461   PetscFunctionBegin;
7462   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7463   PetscValidType(mat,1);
7464   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7465   MatCheckPreallocated(mat,1);
7466   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7467   ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr);
7468   mat->nullsp = nullsp;
7469   PetscFunctionReturn(0);
7470 }
7471 
7472 #undef __FUNCT__
7473 #define __FUNCT__ "MatSetNearNullSpace"
7474 /*@
7475    MatSetNearNullSpace - attaches a null space to a matrix.
7476         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.
7477 
7478    Logically Collective on Mat and MatNullSpace
7479 
7480    Input Parameters:
7481 +  mat - the matrix
7482 -  nullsp - the null space object
7483 
7484    Level: advanced
7485 
7486    Notes:
7487       Overwrites any previous near null space that may have been attached
7488 
7489    Concepts: null space^attaching to matrix
7490 
7491 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace()
7492 @*/
7493 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
7494 {
7495   PetscErrorCode ierr;
7496 
7497   PetscFunctionBegin;
7498   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7499   PetscValidType(mat,1);
7500   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7501   MatCheckPreallocated(mat,1);
7502   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7503   ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr);
7504   mat->nearnullsp = nullsp;
7505   PetscFunctionReturn(0);
7506 }
7507 
7508 #undef __FUNCT__
7509 #define __FUNCT__ "MatGetNearNullSpace"
7510 /*@
7511    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()
7512 
7513    Not Collective
7514 
7515    Input Parameters:
7516 .  mat - the matrix
7517 
7518    Output Parameters:
7519 .  nullsp - the null space object, PETSC_NULL if not set
7520 
7521    Level: developer
7522 
7523    Concepts: null space^attaching to matrix
7524 
7525 .seealso: MatSetNearNullSpace(), MatGetNullSpace()
7526 @*/
7527 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
7528 {
7529 
7530   PetscFunctionBegin;
7531   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7532   PetscValidType(mat,1);
7533   PetscValidPointer(nullsp,2);
7534   MatCheckPreallocated(mat,1);
7535   *nullsp = mat->nearnullsp;
7536   PetscFunctionReturn(0);
7537 }
7538 
7539 #undef __FUNCT__
7540 #define __FUNCT__ "MatICCFactor"
7541 /*@C
7542    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
7543 
7544    Collective on Mat
7545 
7546    Input Parameters:
7547 +  mat - the matrix
7548 .  row - row/column permutation
7549 .  fill - expected fill factor >= 1.0
7550 -  level - level of fill, for ICC(k)
7551 
7552    Notes:
7553    Probably really in-place only when level of fill is zero, otherwise allocates
7554    new space to store factored matrix and deletes previous memory.
7555 
7556    Most users should employ the simplified KSP interface for linear solvers
7557    instead of working directly with matrix algebra routines such as this.
7558    See, e.g., KSPCreate().
7559 
7560    Level: developer
7561 
7562    Concepts: matrices^incomplete Cholesky factorization
7563    Concepts: Cholesky factorization
7564 
7565 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
7566 
7567     Developer Note: fortran interface is not autogenerated as the f90
7568     interface defintion cannot be generated correctly [due to MatFactorInfo]
7569 
7570 @*/
7571 PetscErrorCode  MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
7572 {
7573   PetscErrorCode ierr;
7574 
7575   PetscFunctionBegin;
7576   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7577   PetscValidType(mat,1);
7578   if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2);
7579   PetscValidPointer(info,3);
7580   if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square");
7581   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7582   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7583   if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7584   MatCheckPreallocated(mat,1);
7585   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
7586   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7587   PetscFunctionReturn(0);
7588 }
7589 
7590 #undef __FUNCT__
7591 #define __FUNCT__ "MatSetValuesAdic"
7592 /*@
7593    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
7594 
7595    Not Collective
7596 
7597    Input Parameters:
7598 +  mat - the matrix
7599 -  v - the values compute with ADIC
7600 
7601    Level: developer
7602 
7603    Notes:
7604      Must call MatSetColoring() before using this routine. Also this matrix must already
7605      have its nonzero pattern determined.
7606 
7607 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7608           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
7609 @*/
7610 PetscErrorCode  MatSetValuesAdic(Mat mat,void *v)
7611 {
7612   PetscErrorCode ierr;
7613 
7614   PetscFunctionBegin;
7615   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7616   PetscValidType(mat,1);
7617   PetscValidPointer(mat,2);
7618 
7619   if (!mat->assembled) {
7620     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7621   }
7622   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7623   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7624   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
7625   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7626   ierr = MatView_Private(mat);CHKERRQ(ierr);
7627   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7628   PetscFunctionReturn(0);
7629 }
7630 
7631 
7632 #undef __FUNCT__
7633 #define __FUNCT__ "MatSetColoring"
7634 /*@
7635    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
7636 
7637    Not Collective
7638 
7639    Input Parameters:
7640 +  mat - the matrix
7641 -  coloring - the coloring
7642 
7643    Level: developer
7644 
7645 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7646           MatSetValues(), MatSetValuesAdic()
7647 @*/
7648 PetscErrorCode  MatSetColoring(Mat mat,ISColoring coloring)
7649 {
7650   PetscErrorCode ierr;
7651 
7652   PetscFunctionBegin;
7653   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7654   PetscValidType(mat,1);
7655   PetscValidPointer(coloring,2);
7656 
7657   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7658   if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7659   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
7660   PetscFunctionReturn(0);
7661 }
7662 
7663 #undef __FUNCT__
7664 #define __FUNCT__ "MatSetValuesAdifor"
7665 /*@
7666    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
7667 
7668    Not Collective
7669 
7670    Input Parameters:
7671 +  mat - the matrix
7672 .  nl - leading dimension of v
7673 -  v - the values compute with ADIFOR
7674 
7675    Level: developer
7676 
7677    Notes:
7678      Must call MatSetColoring() before using this routine. Also this matrix must already
7679      have its nonzero pattern determined.
7680 
7681 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7682           MatSetValues(), MatSetColoring()
7683 @*/
7684 PetscErrorCode  MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
7685 {
7686   PetscErrorCode ierr;
7687 
7688   PetscFunctionBegin;
7689   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7690   PetscValidType(mat,1);
7691   PetscValidPointer(v,3);
7692 
7693   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7694   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7695   if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7696   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
7697   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7698   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7699   PetscFunctionReturn(0);
7700 }
7701 
7702 #undef __FUNCT__
7703 #define __FUNCT__ "MatDiagonalScaleLocal"
7704 /*@
7705    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
7706          ghosted ones.
7707 
7708    Not Collective
7709 
7710    Input Parameters:
7711 +  mat - the matrix
7712 -  diag = the diagonal values, including ghost ones
7713 
7714    Level: developer
7715 
7716    Notes: Works only for MPIAIJ and MPIBAIJ matrices
7717 
7718 .seealso: MatDiagonalScale()
7719 @*/
7720 PetscErrorCode  MatDiagonalScaleLocal(Mat mat,Vec diag)
7721 {
7722   PetscErrorCode ierr;
7723   PetscMPIInt    size;
7724 
7725   PetscFunctionBegin;
7726   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7727   PetscValidHeaderSpecific(diag,VEC_CLASSID,2);
7728   PetscValidType(mat,1);
7729 
7730   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7731   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7732   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7733   if (size == 1) {
7734     PetscInt n,m;
7735     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
7736     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
7737     if (m == n) {
7738       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
7739     } else {
7740       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
7741     }
7742   } else {
7743     ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr);
7744   }
7745   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7746   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7747   PetscFunctionReturn(0);
7748 }
7749 
7750 #undef __FUNCT__
7751 #define __FUNCT__ "MatGetInertia"
7752 /*@
7753    MatGetInertia - Gets the inertia from a factored matrix
7754 
7755    Collective on Mat
7756 
7757    Input Parameter:
7758 .  mat - the matrix
7759 
7760    Output Parameters:
7761 +   nneg - number of negative eigenvalues
7762 .   nzero - number of zero eigenvalues
7763 -   npos - number of positive eigenvalues
7764 
7765    Level: advanced
7766 
7767    Notes: Matrix must have been factored by MatCholeskyFactor()
7768 
7769 
7770 @*/
7771 PetscErrorCode  MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
7772 {
7773   PetscErrorCode ierr;
7774 
7775   PetscFunctionBegin;
7776   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7777   PetscValidType(mat,1);
7778   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7779   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
7780   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7781   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
7782   PetscFunctionReturn(0);
7783 }
7784 
7785 /* ----------------------------------------------------------------*/
7786 #undef __FUNCT__
7787 #define __FUNCT__ "MatSolves"
7788 /*@C
7789    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
7790 
7791    Neighbor-wise Collective on Mat and Vecs
7792 
7793    Input Parameters:
7794 +  mat - the factored matrix
7795 -  b - the right-hand-side vectors
7796 
7797    Output Parameter:
7798 .  x - the result vectors
7799 
7800    Notes:
7801    The vectors b and x cannot be the same.  I.e., one cannot
7802    call MatSolves(A,x,x).
7803 
7804    Notes:
7805    Most users should employ the simplified KSP interface for linear solvers
7806    instead of working directly with matrix algebra routines such as this.
7807    See, e.g., KSPCreate().
7808 
7809    Level: developer
7810 
7811    Concepts: matrices^triangular solves
7812 
7813 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
7814 @*/
7815 PetscErrorCode  MatSolves(Mat mat,Vecs b,Vecs x)
7816 {
7817   PetscErrorCode ierr;
7818 
7819   PetscFunctionBegin;
7820   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7821   PetscValidType(mat,1);
7822   if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors");
7823   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7824   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
7825 
7826   if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7827   MatCheckPreallocated(mat,1);
7828   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7829   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
7830   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7831   PetscFunctionReturn(0);
7832 }
7833 
7834 #undef __FUNCT__
7835 #define __FUNCT__ "MatIsSymmetric"
7836 /*@
7837    MatIsSymmetric - Test whether a matrix is symmetric
7838 
7839    Collective on Mat
7840 
7841    Input Parameter:
7842 +  A - the matrix to test
7843 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
7844 
7845    Output Parameters:
7846 .  flg - the result
7847 
7848    Notes: For real numbers MatIsSymmetric() and MatIsHermitian() return identical results
7849 
7850    Level: intermediate
7851 
7852    Concepts: matrix^symmetry
7853 
7854 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7855 @*/
7856 PetscErrorCode  MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
7857 {
7858   PetscErrorCode ierr;
7859 
7860   PetscFunctionBegin;
7861   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7862   PetscValidPointer(flg,2);
7863 
7864   if (!A->symmetric_set) {
7865     if (!A->ops->issymmetric) {
7866       const MatType mattype;
7867       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7868       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7869     }
7870     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7871     if (!tol) {
7872       A->symmetric_set = PETSC_TRUE;
7873       A->symmetric = *flg;
7874       if (A->symmetric) {
7875 	A->structurally_symmetric_set = PETSC_TRUE;
7876 	A->structurally_symmetric     = PETSC_TRUE;
7877       }
7878     }
7879   } else if (A->symmetric) {
7880     *flg = PETSC_TRUE;
7881   } else if (!tol) {
7882     *flg = PETSC_FALSE;
7883   } else {
7884     if (!A->ops->issymmetric) {
7885       const MatType mattype;
7886       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7887       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7888     }
7889     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7890   }
7891   PetscFunctionReturn(0);
7892 }
7893 
7894 #undef __FUNCT__
7895 #define __FUNCT__ "MatIsHermitian"
7896 /*@
7897    MatIsHermitian - Test whether a matrix is Hermitian
7898 
7899    Collective on Mat
7900 
7901    Input Parameter:
7902 +  A - the matrix to test
7903 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
7904 
7905    Output Parameters:
7906 .  flg - the result
7907 
7908    Level: intermediate
7909 
7910    Concepts: matrix^symmetry
7911 
7912 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
7913           MatIsSymmetricKnown(), MatIsSymmetric()
7914 @*/
7915 PetscErrorCode  MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
7916 {
7917   PetscErrorCode ierr;
7918 
7919   PetscFunctionBegin;
7920   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7921   PetscValidPointer(flg,2);
7922 
7923   if (!A->hermitian_set) {
7924     if (!A->ops->ishermitian) {
7925       const MatType mattype;
7926       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7927       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7928     }
7929     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7930     if (!tol) {
7931       A->hermitian_set = PETSC_TRUE;
7932       A->hermitian = *flg;
7933       if (A->hermitian) {
7934 	A->structurally_symmetric_set = PETSC_TRUE;
7935 	A->structurally_symmetric     = PETSC_TRUE;
7936       }
7937     }
7938   } else if (A->hermitian) {
7939     *flg = PETSC_TRUE;
7940   } else if (!tol) {
7941     *flg = PETSC_FALSE;
7942   } else {
7943     if (!A->ops->ishermitian) {
7944       const MatType mattype;
7945       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7946       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7947     }
7948     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7949   }
7950   PetscFunctionReturn(0);
7951 }
7952 
7953 #undef __FUNCT__
7954 #define __FUNCT__ "MatIsSymmetricKnown"
7955 /*@
7956    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
7957 
7958    Not Collective
7959 
7960    Input Parameter:
7961 .  A - the matrix to check
7962 
7963    Output Parameters:
7964 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
7965 -  flg - the result
7966 
7967    Level: advanced
7968 
7969    Concepts: matrix^symmetry
7970 
7971    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
7972          if you want it explicitly checked
7973 
7974 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
7975 @*/
7976 PetscErrorCode  MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
7977 {
7978   PetscFunctionBegin;
7979   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7980   PetscValidPointer(set,2);
7981   PetscValidPointer(flg,3);
7982   if (A->symmetric_set) {
7983     *set = PETSC_TRUE;
7984     *flg = A->symmetric;
7985   } else {
7986     *set = PETSC_FALSE;
7987   }
7988   PetscFunctionReturn(0);
7989 }
7990 
7991 #undef __FUNCT__
7992 #define __FUNCT__ "MatIsHermitianKnown"
7993 /*@
7994    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
7995 
7996    Not Collective
7997 
7998    Input Parameter:
7999 .  A - the matrix to check
8000 
8001    Output Parameters:
8002 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8003 -  flg - the result
8004 
8005    Level: advanced
8006 
8007    Concepts: matrix^symmetry
8008 
8009    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8010          if you want it explicitly checked
8011 
8012 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8013 @*/
8014 PetscErrorCode  MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8015 {
8016   PetscFunctionBegin;
8017   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8018   PetscValidPointer(set,2);
8019   PetscValidPointer(flg,3);
8020   if (A->hermitian_set) {
8021     *set = PETSC_TRUE;
8022     *flg = A->hermitian;
8023   } else {
8024     *set = PETSC_FALSE;
8025   }
8026   PetscFunctionReturn(0);
8027 }
8028 
8029 #undef __FUNCT__
8030 #define __FUNCT__ "MatIsStructurallySymmetric"
8031 /*@
8032    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
8033 
8034    Collective on Mat
8035 
8036    Input Parameter:
8037 .  A - the matrix to test
8038 
8039    Output Parameters:
8040 .  flg - the result
8041 
8042    Level: intermediate
8043 
8044    Concepts: matrix^symmetry
8045 
8046 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8047 @*/
8048 PetscErrorCode  MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8049 {
8050   PetscErrorCode ierr;
8051 
8052   PetscFunctionBegin;
8053   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8054   PetscValidPointer(flg,2);
8055   if (!A->structurally_symmetric_set) {
8056     if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8057     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
8058     A->structurally_symmetric_set = PETSC_TRUE;
8059   }
8060   *flg = A->structurally_symmetric;
8061   PetscFunctionReturn(0);
8062 }
8063 
8064 #undef __FUNCT__
8065 #define __FUNCT__ "MatStashGetInfo"
8066 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
8067 /*@
8068    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8069        to be communicated to other processors during the MatAssemblyBegin/End() process
8070 
8071     Not collective
8072 
8073    Input Parameter:
8074 .   vec - the vector
8075 
8076    Output Parameters:
8077 +   nstash   - the size of the stash
8078 .   reallocs - the number of additional mallocs incurred.
8079 .   bnstash   - the size of the block stash
8080 -   breallocs - the number of additional mallocs incurred.in the block stash
8081 
8082    Level: advanced
8083 
8084 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
8085 
8086 @*/
8087 PetscErrorCode  MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8088 {
8089   PetscErrorCode ierr;
8090   PetscFunctionBegin;
8091   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
8092   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
8093   PetscFunctionReturn(0);
8094 }
8095 
8096 #undef __FUNCT__
8097 #define __FUNCT__ "MatGetVecs"
8098 /*@C
8099    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
8100      parallel layout
8101 
8102    Collective on Mat
8103 
8104    Input Parameter:
8105 .  mat - the matrix
8106 
8107    Output Parameter:
8108 +   right - (optional) vector that the matrix can be multiplied against
8109 -   left - (optional) vector that the matrix vector product can be stored in
8110 
8111   Level: advanced
8112 
8113 .seealso: MatCreate()
8114 @*/
8115 PetscErrorCode  MatGetVecs(Mat mat,Vec *right,Vec *left)
8116 {
8117   PetscErrorCode ierr;
8118 
8119   PetscFunctionBegin;
8120   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8121   PetscValidType(mat,1);
8122   MatCheckPreallocated(mat,1);
8123   if (mat->ops->getvecs) {
8124     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
8125   } else {
8126     PetscMPIInt size;
8127     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
8128     if (right) {
8129       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
8130       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8131       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
8132       ierr = VecSetType(*right,VECSTANDARD);CHKERRQ(ierr);
8133       ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr);
8134     }
8135     if (left) {
8136       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
8137       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8138       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
8139       ierr = VecSetType(*left,VECSTANDARD);CHKERRQ(ierr);
8140       ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr);
8141     }
8142   }
8143   PetscFunctionReturn(0);
8144 }
8145 
8146 #undef __FUNCT__
8147 #define __FUNCT__ "MatFactorInfoInitialize"
8148 /*@C
8149    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8150      with default values.
8151 
8152    Not Collective
8153 
8154    Input Parameters:
8155 .    info - the MatFactorInfo data structure
8156 
8157 
8158    Notes: The solvers are generally used through the KSP and PC objects, for example
8159           PCLU, PCILU, PCCHOLESKY, PCICC
8160 
8161    Level: developer
8162 
8163 .seealso: MatFactorInfo
8164 
8165     Developer Note: fortran interface is not autogenerated as the f90
8166     interface defintion cannot be generated correctly [due to MatFactorInfo]
8167 
8168 @*/
8169 
8170 PetscErrorCode  MatFactorInfoInitialize(MatFactorInfo *info)
8171 {
8172   PetscErrorCode ierr;
8173 
8174   PetscFunctionBegin;
8175   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
8176   PetscFunctionReturn(0);
8177 }
8178 
8179 #undef __FUNCT__
8180 #define __FUNCT__ "MatPtAP"
8181 /*@
8182    MatPtAP - Creates the matrix product C = P^T * A * P
8183 
8184    Neighbor-wise Collective on Mat
8185 
8186    Input Parameters:
8187 +  A - the matrix
8188 .  P - the projection matrix
8189 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8190 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P))
8191 
8192    Output Parameters:
8193 .  C - the product matrix
8194 
8195    Notes:
8196    C will be created and must be destroyed by the user with MatDestroy().
8197 
8198    This routine is currently only implemented for pairs of AIJ matrices and classes
8199    which inherit from AIJ.
8200 
8201    Level: intermediate
8202 
8203 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
8204 @*/
8205 PetscErrorCode  MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8206 {
8207   PetscErrorCode ierr;
8208 
8209   PetscFunctionBegin;
8210   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8211   PetscValidType(A,1);
8212   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8213   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8214   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8215   PetscValidType(P,2);
8216   MatCheckPreallocated(P,2);
8217   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8218   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8219   PetscValidPointer(C,3);
8220   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);
8221   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8222   MatCheckPreallocated(A,1);
8223 
8224   if (!A->ops->ptap) {
8225     const MatType mattype;
8226     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8227     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype);
8228   }
8229   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8230   ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
8231   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8232   PetscFunctionReturn(0);
8233 }
8234 
8235 #undef __FUNCT__
8236 #define __FUNCT__ "MatPtAPNumeric"
8237 /*@
8238    MatPtAPNumeric - Computes the matrix product C = P^T * A * P
8239 
8240    Neighbor-wise Collective on Mat
8241 
8242    Input Parameters:
8243 +  A - the matrix
8244 -  P - the projection matrix
8245 
8246    Output Parameters:
8247 .  C - the product matrix
8248 
8249    Notes:
8250    C must have been created by calling MatPtAPSymbolic and must be destroyed by
8251    the user using MatDeatroy().
8252 
8253    This routine is currently only implemented for pairs of AIJ matrices and classes
8254    which inherit from AIJ.  C will be of type MATAIJ.
8255 
8256    Level: intermediate
8257 
8258 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
8259 @*/
8260 PetscErrorCode  MatPtAPNumeric(Mat A,Mat P,Mat C)
8261 {
8262   PetscErrorCode ierr;
8263 
8264   PetscFunctionBegin;
8265   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8266   PetscValidType(A,1);
8267   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8268   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8269   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8270   PetscValidType(P,2);
8271   MatCheckPreallocated(P,2);
8272   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8273   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8274   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8275   PetscValidType(C,3);
8276   MatCheckPreallocated(C,3);
8277   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8278   if (P->cmap->N!=C->rmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N);
8279   if (P->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
8280   if (A->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
8281   if (P->cmap->N!=C->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N);
8282   MatCheckPreallocated(A,1);
8283 
8284   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8285   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
8286   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8287   PetscFunctionReturn(0);
8288 }
8289 
8290 #undef __FUNCT__
8291 #define __FUNCT__ "MatPtAPSymbolic"
8292 /*@
8293    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P
8294 
8295    Neighbor-wise Collective on Mat
8296 
8297    Input Parameters:
8298 +  A - the matrix
8299 -  P - the projection matrix
8300 
8301    Output Parameters:
8302 .  C - the (i,j) structure of the product matrix
8303 
8304    Notes:
8305    C will be created and must be destroyed by the user with MatDestroy().
8306 
8307    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8308    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8309    this (i,j) structure by calling MatPtAPNumeric().
8310 
8311    Level: intermediate
8312 
8313 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
8314 @*/
8315 PetscErrorCode  MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
8316 {
8317   PetscErrorCode ierr;
8318 
8319   PetscFunctionBegin;
8320   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8321   PetscValidType(A,1);
8322   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8323   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8324   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8325   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8326   PetscValidType(P,2);
8327   MatCheckPreallocated(P,2);
8328   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8329   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8330   PetscValidPointer(C,3);
8331 
8332   if (P->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
8333   if (A->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
8334   MatCheckPreallocated(A,1);
8335   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8336   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
8337   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8338 
8339   /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */
8340 
8341   PetscFunctionReturn(0);
8342 }
8343 
8344 #undef __FUNCT__
8345 #define __FUNCT__ "MatRARt"
8346 /*@
8347    MatRARt - Creates the matrix product C = R * A * R^T
8348 
8349    Neighbor-wise Collective on Mat
8350 
8351    Input Parameters:
8352 +  A - the matrix
8353 .  R - the projection matrix
8354 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8355 -  fill - expected fill as ratio of nnz(C)/nnz(A)
8356 
8357    Output Parameters:
8358 .  C - the product matrix
8359 
8360    Notes:
8361    C will be created and must be destroyed by the user with MatDestroy().
8362 
8363    This routine is currently only implemented for pairs of AIJ matrices and classes
8364    which inherit from AIJ.
8365 
8366    Level: intermediate
8367 
8368 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
8369 @*/
8370 PetscErrorCode  MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
8371 {
8372   PetscErrorCode ierr;
8373 
8374   PetscFunctionBegin;
8375   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8376   PetscValidType(A,1);
8377   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8378   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8379   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8380   PetscValidType(R,2);
8381   MatCheckPreallocated(R,2);
8382   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8383   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8384   PetscValidPointer(C,3);
8385   if (R->cmap->N!=A->rmap->N) SETERRQ2(((PetscObject)R)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
8386   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8387   MatCheckPreallocated(A,1);
8388 
8389   if (!A->ops->rart) {
8390     const MatType mattype;
8391     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8392     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support RARt",mattype);
8393   }
8394   ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8395   ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr);
8396   ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8397   PetscFunctionReturn(0);
8398 }
8399 
8400 #undef __FUNCT__
8401 #define __FUNCT__ "MatRARtNumeric"
8402 /*@
8403    MatRARtNumeric - Computes the matrix product C = R * A * R^T
8404 
8405    Neighbor-wise Collective on Mat
8406 
8407    Input Parameters:
8408 +  A - the matrix
8409 -  R - the projection matrix
8410 
8411    Output Parameters:
8412 .  C - the product matrix
8413 
8414    Notes:
8415    C must have been created by calling MatRARtSymbolic and must be destroyed by
8416    the user using MatDeatroy().
8417 
8418    This routine is currently only implemented for pairs of AIJ matrices and classes
8419    which inherit from AIJ.  C will be of type MATAIJ.
8420 
8421    Level: intermediate
8422 
8423 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
8424 @*/
8425 PetscErrorCode  MatRARtNumeric(Mat A,Mat R,Mat C)
8426 {
8427   PetscErrorCode ierr;
8428 
8429   PetscFunctionBegin;
8430   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8431   PetscValidType(A,1);
8432   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8433   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8434   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8435   PetscValidType(R,2);
8436   MatCheckPreallocated(R,2);
8437   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8438   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8439   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8440   PetscValidType(C,3);
8441   MatCheckPreallocated(C,3);
8442   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8443   if (R->rmap->N!=C->rmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->rmap->N);
8444   if (R->cmap->N!=A->rmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
8445   if (A->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
8446   if (R->rmap->N!=C->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->cmap->N);
8447   MatCheckPreallocated(A,1);
8448 
8449   ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8450   ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr);
8451   ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8452   PetscFunctionReturn(0);
8453 }
8454 
8455 #undef __FUNCT__
8456 #define __FUNCT__ "MatRARtSymbolic"
8457 /*@
8458    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T
8459 
8460    Neighbor-wise Collective on Mat
8461 
8462    Input Parameters:
8463 +  A - the matrix
8464 -  R - the projection matrix
8465 
8466    Output Parameters:
8467 .  C - the (i,j) structure of the product matrix
8468 
8469    Notes:
8470    C will be created and must be destroyed by the user with MatDestroy().
8471 
8472    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8473    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8474    this (i,j) structure by calling MatRARtNumeric().
8475 
8476    Level: intermediate
8477 
8478 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
8479 @*/
8480 PetscErrorCode  MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
8481 {
8482   PetscErrorCode ierr;
8483 
8484   PetscFunctionBegin;
8485   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8486   PetscValidType(A,1);
8487   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8488   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8489   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8490   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8491   PetscValidType(R,2);
8492   MatCheckPreallocated(R,2);
8493   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8494   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8495   PetscValidPointer(C,3);
8496 
8497   if (R->cmap->N!=A->rmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
8498   if (A->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
8499   MatCheckPreallocated(A,1);
8500   ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8501   ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr);
8502   ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8503 
8504   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
8505   PetscFunctionReturn(0);
8506 }
8507 
8508 extern PetscErrorCode MatQueryOp(MPI_Comm comm, void (**function)(void), const char op[], PetscInt numArgs, ...);
8509 
8510 #undef __FUNCT__
8511 #define __FUNCT__ "MatMatMult"
8512 /*@
8513    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
8514 
8515    Neighbor-wise Collective on Mat
8516 
8517    Input Parameters:
8518 +  A - the left matrix
8519 .  B - the right matrix
8520 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8521 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
8522           if the result is a dense matrix this is irrelevent
8523 
8524    Output Parameters:
8525 .  C - the product matrix
8526 
8527    Notes:
8528    Unless scall is MAT_REUSE_MATRIX C will be created.
8529 
8530    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8531 
8532    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8533    actually needed.
8534 
8535    If you have many matrices with the same non-zero structure to multiply, you
8536    should either
8537 $   1) use MAT_REUSE_MATRIX in all calls but the first or
8538 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
8539 
8540    Level: intermediate
8541 
8542 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
8543 @*/
8544 PetscErrorCode  MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8545 {
8546   PetscErrorCode ierr;
8547   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8548   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8549   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
8550 
8551   PetscFunctionBegin;
8552   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8553   PetscValidType(A,1);
8554   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8555   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8556   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8557   PetscValidType(B,2);
8558   MatCheckPreallocated(B,2);
8559   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8560   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8561   PetscValidPointer(C,3);
8562   if (B->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
8563   if (scall == MAT_REUSE_MATRIX){
8564     PetscValidPointer(*C,5);
8565     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
8566     ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8567     ierr = (*(*C)->ops->matmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8568     ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8569   }
8570   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8571   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8572   MatCheckPreallocated(A,1);
8573 
8574   fA = A->ops->matmult;
8575   fB = B->ops->matmult;
8576   if (fB == fA) {
8577     if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8578     mult = fB;
8579   } else {
8580     /* dispatch based on the type of A and B from their PetscObject's PetscFLists. */
8581     char  multname[256];
8582     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
8583     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8584     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
8585     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8586     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
8587     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
8588     if(!mult){
8589       /* dual dispatch using MatQueryOp */
8590       ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&mult), "MatMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8591       if (!mult) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
8592     }
8593   }
8594   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8595   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
8596   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8597   PetscFunctionReturn(0);
8598 }
8599 
8600 #undef __FUNCT__
8601 #define __FUNCT__ "MatMatMultSymbolic"
8602 /*@
8603    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
8604    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
8605 
8606    Neighbor-wise Collective on Mat
8607 
8608    Input Parameters:
8609 +  A - the left matrix
8610 .  B - the right matrix
8611 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
8612       if C is a dense matrix this is irrelevent
8613 
8614    Output Parameters:
8615 .  C - the product matrix
8616 
8617    Notes:
8618    Unless scall is MAT_REUSE_MATRIX C will be created.
8619 
8620    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8621    actually needed.
8622 
8623    This routine is currently implemented for
8624     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
8625     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8626     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8627 
8628    Level: intermediate
8629 
8630    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
8631      We should incorporate them into PETSc.
8632 
8633 .seealso: MatMatMult(), MatMatMultNumeric()
8634 @*/
8635 PetscErrorCode  MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
8636 {
8637   PetscErrorCode ierr;
8638   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
8639   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
8640   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
8641 
8642   PetscFunctionBegin;
8643   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8644   PetscValidType(A,1);
8645   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8646   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8647 
8648   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8649   PetscValidType(B,2);
8650   MatCheckPreallocated(B,2);
8651   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8652   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8653   PetscValidPointer(C,3);
8654 
8655   if (B->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
8656   if (fill == PETSC_DEFAULT) fill = 2.0;
8657   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8658   MatCheckPreallocated(A,1);
8659 
8660   Asymbolic = A->ops->matmultsymbolic;
8661   Bsymbolic = B->ops->matmultsymbolic;
8662   if (Asymbolic == Bsymbolic){
8663     if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
8664     symbolic = Bsymbolic;
8665   } else { /* dispatch based on the type of A and B */
8666     char  symbolicname[256];
8667     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
8668     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8669     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
8670     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8671     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
8672     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
8673     if (!symbolic) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
8674   }
8675   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8676   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
8677   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8678   PetscFunctionReturn(0);
8679 }
8680 
8681 #undef __FUNCT__
8682 #define __FUNCT__ "MatMatMultNumeric"
8683 /*@
8684    MatMatMultNumeric - Performs the numeric matrix-matrix product.
8685    Call this routine after first calling MatMatMultSymbolic().
8686 
8687    Neighbor-wise Collective on Mat
8688 
8689    Input Parameters:
8690 +  A - the left matrix
8691 -  B - the right matrix
8692 
8693    Output Parameters:
8694 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
8695 
8696    Notes:
8697    C must have been created with MatMatMultSymbolic().
8698 
8699    This routine is currently implemented for
8700     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
8701     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8702     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8703 
8704    Level: intermediate
8705 
8706 .seealso: MatMatMult(), MatMatMultSymbolic()
8707 @*/
8708 PetscErrorCode  MatMatMultNumeric(Mat A,Mat B,Mat C)
8709 {
8710   PetscErrorCode ierr;
8711   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
8712   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
8713   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
8714 
8715   PetscFunctionBegin;
8716   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8717   PetscValidType(A,1);
8718   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8719   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8720 
8721   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8722   PetscValidType(B,2);
8723   MatCheckPreallocated(B,2);
8724   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8725   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8726 
8727   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8728   PetscValidType(C,3);
8729   MatCheckPreallocated(C,3);
8730   if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8731   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8732 
8733   if (B->cmap->N!=C->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->cmap->N,C->cmap->N);
8734   if (B->rmap->N!=A->cmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
8735   if (A->rmap->N!=C->rmap->N) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",A->rmap->N,C->rmap->N);
8736   MatCheckPreallocated(A,1);
8737 
8738   Anumeric = A->ops->matmultnumeric;
8739   Bnumeric = B->ops->matmultnumeric;
8740   if (Anumeric == Bnumeric){
8741     if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
8742     numeric = Bnumeric;
8743   } else {
8744     char  numericname[256];
8745     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
8746     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8747     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
8748     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8749     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
8750     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
8751     if (!numeric)
8752       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);
8753   }
8754   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8755   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
8756   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8757   PetscFunctionReturn(0);
8758 }
8759 
8760 #undef __FUNCT__
8761 #define __FUNCT__ "MatMatTransposeMult"
8762 /*@
8763    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.
8764 
8765    Neighbor-wise Collective on Mat
8766 
8767    Input Parameters:
8768 +  A - the left matrix
8769 .  B - the right matrix
8770 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8771 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8772 
8773    Output Parameters:
8774 .  C - the product matrix
8775 
8776    Notes:
8777    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8778 
8779    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8780 
8781   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8782    actually needed.
8783 
8784    This routine is currently only implemented for pairs of SeqAIJ matrices.  C will be of type MATSEQAIJ.
8785 
8786    Level: intermediate
8787 
8788 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
8789 @*/
8790 PetscErrorCode  MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8791 {
8792   PetscErrorCode ierr;
8793   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8794   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8795 
8796   PetscFunctionBegin;
8797   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8798   PetscValidType(A,1);
8799   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8800   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8801   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8802   PetscValidType(B,2);
8803   MatCheckPreallocated(B,2);
8804   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8805   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8806   PetscValidPointer(C,3);
8807   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);
8808   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8809   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8810   MatCheckPreallocated(A,1);
8811 
8812   fA = A->ops->mattransposemult;
8813   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
8814   fB = B->ops->mattransposemult;
8815   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
8816   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);
8817 
8818   if (scall == MAT_INITIAL_MATRIX){
8819     ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8820     ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr);
8821     ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8822   }
8823   ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8824   ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr);
8825   ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8826   PetscFunctionReturn(0);
8827 }
8828 
8829 #undef __FUNCT__
8830 #define __FUNCT__ "MatTransposeMatMult"
8831 /*@
8832    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.
8833 
8834    Neighbor-wise Collective on Mat
8835 
8836    Input Parameters:
8837 +  A - the left matrix
8838 .  B - the right matrix
8839 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8840 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8841 
8842    Output Parameters:
8843 .  C - the product matrix
8844 
8845    Notes:
8846    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8847 
8848    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8849 
8850   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8851    actually needed.
8852 
8853    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
8854    which inherit from SeqAIJ.  C will be of same type as the input matrices.
8855 
8856    Level: intermediate
8857 
8858 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
8859 @*/
8860 PetscErrorCode  MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8861 {
8862   PetscErrorCode ierr;
8863   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8864   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8865   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*);
8866 
8867   PetscFunctionBegin;
8868   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8869   PetscValidType(A,1);
8870   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8871   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8872   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8873   PetscValidType(B,2);
8874   MatCheckPreallocated(B,2);
8875   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8876   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8877   PetscValidPointer(C,3);
8878   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);
8879   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8880   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8881   MatCheckPreallocated(A,1);
8882 
8883   fA = A->ops->transposematmult;
8884   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
8885   fB = B->ops->transposematmult;
8886   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8887   if (fB==fA) {
8888     transposematmult = fA;
8889   }
8890   else {
8891     /* dual dispatch using MatQueryOp */
8892     ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&transposematmult), "MatTansposeMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8893     if(!transposematmult)
8894       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);
8895   }
8896   ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8897   ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8898   ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8899   PetscFunctionReturn(0);
8900 }
8901 
8902 #undef __FUNCT__
8903 #define __FUNCT__ "MatGetRedundantMatrix"
8904 /*@C
8905    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
8906 
8907    Collective on Mat
8908 
8909    Input Parameters:
8910 +  mat - the matrix
8911 .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
8912 .  subcomm - MPI communicator split from the communicator where mat resides in
8913 .  mlocal_red - number of local rows of the redundant matrix
8914 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8915 
8916    Output Parameter:
8917 .  matredundant - redundant matrix
8918 
8919    Notes:
8920    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
8921    original matrix has not changed from that last call to MatGetRedundantMatrix().
8922 
8923    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
8924    calling it.
8925 
8926    Only MPIAIJ matrix is supported.
8927 
8928    Level: advanced
8929 
8930    Concepts: subcommunicator
8931    Concepts: duplicate matrix
8932 
8933 .seealso: MatDestroy()
8934 @*/
8935 PetscErrorCode  MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
8936 {
8937   PetscErrorCode ierr;
8938 
8939   PetscFunctionBegin;
8940   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8941   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
8942     PetscValidPointer(*matredundant,6);
8943     PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6);
8944   }
8945   if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8946   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8947   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8948   MatCheckPreallocated(mat,1);
8949 
8950   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8951   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
8952   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8953   PetscFunctionReturn(0);
8954 }
8955 
8956 #undef __FUNCT__
8957 #define __FUNCT__ "MatGetMultiProcBlock"
8958 /*@C
8959    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
8960    a given 'mat' object. Each submatrix can span multiple procs.
8961 
8962    Collective on Mat
8963 
8964    Input Parameters:
8965 +  mat - the matrix
8966 .  subcomm - the subcommunicator obtained by com_split(comm)
8967 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8968 
8969    Output Parameter:
8970 .  subMat - 'parallel submatrices each spans a given subcomm
8971 
8972   Notes:
8973   The submatrix partition across processors is dicated by 'subComm' a
8974   communicator obtained by com_split(comm). The comm_split
8975   is not restriced to be grouped with consequitive original ranks.
8976 
8977   Due the comm_split() usage, the parallel layout of the submatrices
8978   map directly to the layout of the original matrix [wrt the local
8979   row,col partitioning]. So the original 'DiagonalMat' naturally maps
8980   into the 'DiagonalMat' of the subMat, hence it is used directly from
8981   the subMat. However the offDiagMat looses some columns - and this is
8982   reconstructed with MatSetValues()
8983 
8984   Level: advanced
8985 
8986   Concepts: subcommunicator
8987   Concepts: submatrices
8988 
8989 .seealso: MatGetSubMatrices()
8990 @*/
8991 PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat* subMat)
8992 {
8993   PetscErrorCode ierr;
8994   PetscMPIInt    commsize,subCommSize;
8995 
8996   PetscFunctionBegin;
8997   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&commsize);CHKERRQ(ierr);
8998   ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr);
8999   if (subCommSize > commsize) SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);
9000 
9001   ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9002   ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr);
9003   ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9004   PetscFunctionReturn(0);
9005 }
9006 
9007 #undef __FUNCT__
9008 #define __FUNCT__ "MatGetLocalSubMatrix"
9009 /*@
9010    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering
9011 
9012    Not Collective
9013 
9014    Input Arguments:
9015    mat - matrix to extract local submatrix from
9016    isrow - local row indices for submatrix
9017    iscol - local column indices for submatrix
9018 
9019    Output Arguments:
9020    submat - the submatrix
9021 
9022    Level: intermediate
9023 
9024    Notes:
9025    The submat should be returned with MatRestoreLocalSubMatrix().
9026 
9027    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
9028    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.
9029 
9030    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
9031    MatSetValuesBlockedLocal() will also be implemented.
9032 
9033 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef()
9034 @*/
9035 PetscErrorCode  MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9036 {
9037   PetscErrorCode ierr;
9038 
9039   PetscFunctionBegin;
9040   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9041   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9042   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9043   PetscCheckSameComm(isrow,2,iscol,3);
9044   PetscValidPointer(submat,4);
9045 
9046   if (mat->ops->getlocalsubmatrix) {
9047     ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9048   } else {
9049     ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr);
9050   }
9051   PetscFunctionReturn(0);
9052 }
9053 
9054 #undef __FUNCT__
9055 #define __FUNCT__ "MatRestoreLocalSubMatrix"
9056 /*@
9057    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering
9058 
9059    Not Collective
9060 
9061    Input Arguments:
9062    mat - matrix to extract local submatrix from
9063    isrow - local row indices for submatrix
9064    iscol - local column indices for submatrix
9065    submat - the submatrix
9066 
9067    Level: intermediate
9068 
9069 .seealso: MatGetLocalSubMatrix()
9070 @*/
9071 PetscErrorCode  MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9072 {
9073   PetscErrorCode ierr;
9074 
9075   PetscFunctionBegin;
9076   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9077   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9078   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9079   PetscCheckSameComm(isrow,2,iscol,3);
9080   PetscValidPointer(submat,4);
9081   if (*submat) {PetscValidHeaderSpecific(*submat,MAT_CLASSID,4);}
9082 
9083   if (mat->ops->restorelocalsubmatrix) {
9084     ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9085   } else {
9086     ierr = MatDestroy(submat);CHKERRQ(ierr);
9087   }
9088   *submat = PETSC_NULL;
9089   PetscFunctionReturn(0);
9090 }
9091 
9092 /* --------------------------------------------------------*/
9093 #undef __FUNCT__
9094 #define __FUNCT__ "MatFindZeroDiagonals"
9095 /*@
9096    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no entry in the matrix
9097 
9098    Collective on Mat
9099 
9100    Input Parameter:
9101 .  mat - the matrix
9102 
9103    Output Parameter:
9104 .  is - if any rows have zero diagonals this contains the list of them
9105 
9106    Level: developer
9107 
9108    Concepts: matrix-vector product
9109 
9110 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9111 @*/
9112 PetscErrorCode  MatFindZeroDiagonals(Mat mat,IS *is)
9113 {
9114   PetscErrorCode ierr;
9115 
9116   PetscFunctionBegin;
9117   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9118   PetscValidType(mat,1);
9119   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9120   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9121 
9122   if (!mat->ops->findzerodiagonals) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"This matrix type does not have a find zero diagonals defined");
9123   ierr = (*mat->ops->findzerodiagonals)(mat,is);CHKERRQ(ierr);
9124   PetscFunctionReturn(0);
9125 }
9126 
9127 #undef __FUNCT__
9128 #define __FUNCT__ "MatInvertBlockDiagonal"
9129 /*@C
9130   MatInvertBlockDiagonal - Inverts the block diagonal entries.
9131 
9132   Collective on Mat
9133 
9134   Input Parameters:
9135 . mat - the matrix
9136 
9137   Output Parameters:
9138 . values - the block inverses in column major order (FORTRAN-like)
9139 
9140    Note:
9141    This routine is not available from Fortran.
9142 
9143   Level: advanced
9144 @*/
9145 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
9146 {
9147   PetscErrorCode ierr;
9148 
9149   PetscFunctionBegin;
9150   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9151   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9152   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9153   if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
9154   ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr);
9155   PetscFunctionReturn(0);
9156 }
9157 
9158 #undef __FUNCT__
9159 #define __FUNCT__ "MatTransposeColoringDestroy"
9160 /*@C
9161     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
9162     via MatTransposeColoringCreate().
9163 
9164     Collective on MatTransposeColoring
9165 
9166     Input Parameter:
9167 .   c - coloring context
9168 
9169     Level: intermediate
9170 
9171 .seealso: MatTransposeColoringCreate()
9172 @*/
9173 PetscErrorCode  MatTransposeColoringDestroy(MatTransposeColoring *c)
9174 {
9175   PetscErrorCode       ierr;
9176   MatTransposeColoring matcolor=*c;
9177 
9178   PetscFunctionBegin;
9179   if (!matcolor) PetscFunctionReturn(0);
9180   if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);}
9181 
9182   ierr = PetscFree(matcolor->ncolumns);CHKERRQ(ierr);
9183   ierr = PetscFree(matcolor->nrows);CHKERRQ(ierr);
9184   ierr = PetscFree(matcolor->colorforrow);CHKERRQ(ierr);
9185   ierr = PetscFree2(matcolor->rows,matcolor->columnsforspidx);CHKERRQ(ierr);
9186   ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr);
9187   ierr = PetscFree(matcolor->columns);CHKERRQ(ierr);
9188   ierr = PetscHeaderDestroy(c);CHKERRQ(ierr);
9189   PetscFunctionReturn(0);
9190 }
9191 
9192 #undef __FUNCT__
9193 #define __FUNCT__ "MatTransColoringApplySpToDen"
9194 /*@C
9195     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
9196     a MatTransposeColoring context has been created, computes a dense B^T by Apply
9197     MatTransposeColoring to sparse B.
9198 
9199     Collective on MatTransposeColoring
9200 
9201     Input Parameters:
9202 +   B - sparse matrix B
9203 .   Btdense - symbolic dense matrix B^T
9204 -   coloring - coloring context created with MatTransposeColoringCreate()
9205 
9206     Output Parameter:
9207 .   Btdense - dense matrix B^T
9208 
9209     Options Database Keys:
9210 +    -mat_transpose_coloring_view - Activates basic viewing or coloring
9211 .    -mat_transpose_coloring_view_draw - Activates drawing of coloring
9212 -    -mat_transpose_coloring_view_info - Activates viewing of coloring info
9213 
9214     Level: intermediate
9215 
9216 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy()
9217 
9218 .keywords: coloring
9219 @*/
9220 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
9221 {
9222   PetscErrorCode ierr;
9223 
9224   PetscFunctionBegin;
9225   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
9226   PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2);
9227   PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3);
9228 
9229   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
9230   ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr);
9231   PetscFunctionReturn(0);
9232 }
9233 
9234 #undef __FUNCT__
9235 #define __FUNCT__ "MatTransColoringApplyDenToSp"
9236 /*@C
9237     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
9238     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
9239     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
9240     Csp from Cden.
9241 
9242     Collective on MatTransposeColoring
9243 
9244     Input Parameters:
9245 +   coloring - coloring context created with MatTransposeColoringCreate()
9246 -   Cden - matrix product of a sparse matrix and a dense matrix Btdense
9247 
9248     Output Parameter:
9249 .   Csp - sparse matrix
9250 
9251     Options Database Keys:
9252 +    -mat_multtranspose_coloring_view - Activates basic viewing or coloring
9253 .    -mat_multtranspose_coloring_view_draw - Activates drawing of coloring
9254 -    -mat_multtranspose_coloring_view_info - Activates viewing of coloring info
9255 
9256     Level: intermediate
9257 
9258 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()
9259 
9260 .keywords: coloring
9261 @*/
9262 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
9263 {
9264   PetscErrorCode ierr;
9265 
9266   PetscFunctionBegin;
9267   PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1);
9268   PetscValidHeaderSpecific(Cden,MAT_CLASSID,2);
9269   PetscValidHeaderSpecific(Csp,MAT_CLASSID,3);
9270 
9271   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
9272   ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr);
9273   PetscFunctionReturn(0);
9274 }
9275 
9276 #undef __FUNCT__
9277 #define __FUNCT__ "MatTransposeColoringCreate"
9278 /*@C
9279    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.
9280 
9281    Collective on Mat
9282 
9283    Input Parameters:
9284 +  mat - the matrix product C
9285 -  iscoloring - the coloring of the matrix; usually obtained with MatGetColoring() or DMCreateColoring()
9286 
9287     Output Parameter:
9288 .   color - the new coloring context
9289 
9290     Level: intermediate
9291 
9292 .seealso: MatTransposeColoringDestroy(), MatTransposeColoringSetFromOptions(), MatTransColoringApplySpToDen(),
9293            MatTransColoringApplyDen()ToSp, MatTransposeColoringView(),
9294 @*/
9295 PetscErrorCode  MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
9296 {
9297   MatTransposeColoring  c;
9298   MPI_Comm              comm;
9299   PetscErrorCode        ierr;
9300 
9301   PetscFunctionBegin;
9302   ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9303   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
9304   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);
9305 
9306   c->ctype = iscoloring->ctype;
9307   if (mat->ops->transposecoloringcreate) {
9308     ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr);
9309   } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Code not yet written for this matrix type");
9310 
9311   *color = c;
9312   ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9313   PetscFunctionReturn(0);
9314 }
9315