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