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