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