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