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