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