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