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