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