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