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