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