xref: /petsc/src/mat/interface/matrix.c (revision a790c00499656cdc34e95ec8f15f35cf5f4cdcbb)
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
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    When extracting submatrices from a parallel matrix, each processor can
6322    form a different submatrix by setting the rows and columns of its
6323    individual index sets according to the local submatrix desired.
6324 
6325    When finished using the submatrices, the user should destroy
6326    them with MatDestroyMatrices().
6327 
6328    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6329    original matrix has not changed from that last call to MatGetSubMatrices().
6330 
6331    This routine creates the matrices in submat; you should NOT create them before
6332    calling it. It also allocates the array of matrix pointers submat.
6333 
6334    For BAIJ matrices the index sets must respect the block structure, that is if they
6335    request one row/column in a block, they must request all rows/columns that are in
6336    that block. For example, if the block size is 2 you cannot request just row 0 and
6337    column 0.
6338 
6339    Fortran Note:
6340    The Fortran interface is slightly different from that given below; it
6341    requires one to pass in  as submat a Mat (integer) array of size at least m.
6342 
6343    Level: advanced
6344 
6345    Concepts: matrices^accessing submatrices
6346    Concepts: submatrices
6347 
6348 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6349 @*/
6350 PetscErrorCode  MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6351 {
6352   PetscErrorCode ierr;
6353   PetscInt        i;
6354   PetscBool       eq;
6355 
6356   PetscFunctionBegin;
6357   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6358   PetscValidType(mat,1);
6359   if (n) {
6360     PetscValidPointer(irow,3);
6361     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6362     PetscValidPointer(icol,4);
6363     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6364   }
6365   PetscValidPointer(submat,6);
6366   if (n && scall == MAT_REUSE_MATRIX) {
6367     PetscValidPointer(*submat,6);
6368     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6369   }
6370   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6371   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6372   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6373   MatCheckPreallocated(mat,1);
6374 
6375   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6376   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6377   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6378   for (i=0; i<n; i++) {
6379     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6380       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6381       if (eq) {
6382 	if (mat->symmetric){
6383 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6384 	} else if (mat->hermitian) {
6385 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6386 	} else if (mat->structurally_symmetric) {
6387 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6388 	}
6389       }
6390     }
6391   }
6392   PetscFunctionReturn(0);
6393 }
6394 
6395 #undef __FUNCT__
6396 #define __FUNCT__ "MatGetSubMatricesParallel"
6397 PetscErrorCode  MatGetSubMatricesParallel(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6398 {
6399   PetscErrorCode ierr;
6400   PetscInt        i;
6401   PetscBool       eq;
6402 
6403   PetscFunctionBegin;
6404   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6405   PetscValidType(mat,1);
6406   if (n) {
6407     PetscValidPointer(irow,3);
6408     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6409     PetscValidPointer(icol,4);
6410     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6411   }
6412   PetscValidPointer(submat,6);
6413   if (n && scall == MAT_REUSE_MATRIX) {
6414     PetscValidPointer(*submat,6);
6415     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6416   }
6417   if (!mat->ops->getsubmatricesparallel) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6418   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6419   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6420   MatCheckPreallocated(mat,1);
6421 
6422   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6423   ierr = (*mat->ops->getsubmatricesparallel)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6424   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6425   for (i=0; i<n; i++) {
6426     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6427       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6428       if (eq) {
6429 	if (mat->symmetric){
6430 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6431 	} else if (mat->hermitian) {
6432 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6433 	} else if (mat->structurally_symmetric) {
6434 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6435 	}
6436       }
6437     }
6438   }
6439   PetscFunctionReturn(0);
6440 }
6441 
6442 #undef __FUNCT__
6443 #define __FUNCT__ "MatDestroyMatrices"
6444 /*@C
6445    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
6446 
6447    Collective on Mat
6448 
6449    Input Parameters:
6450 +  n - the number of local matrices
6451 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6452                        sequence of MatGetSubMatrices())
6453 
6454    Level: advanced
6455 
6456     Notes: Frees not only the matrices, but also the array that contains the matrices
6457            In Fortran will not free the array.
6458 
6459 .seealso: MatGetSubMatrices()
6460 @*/
6461 PetscErrorCode  MatDestroyMatrices(PetscInt n,Mat *mat[])
6462 {
6463   PetscErrorCode ierr;
6464   PetscInt       i;
6465 
6466   PetscFunctionBegin;
6467   if (!*mat) PetscFunctionReturn(0);
6468   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
6469   PetscValidPointer(mat,2);
6470   for (i=0; i<n; i++) {
6471     ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr);
6472   }
6473   /* memory is allocated even if n = 0 */
6474   ierr = PetscFree(*mat);CHKERRQ(ierr);
6475   *mat = PETSC_NULL;
6476   PetscFunctionReturn(0);
6477 }
6478 
6479 #undef __FUNCT__
6480 #define __FUNCT__ "MatGetSeqNonzeroStructure"
6481 /*@C
6482    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
6483 
6484    Collective on Mat
6485 
6486    Input Parameters:
6487 .  mat - the matrix
6488 
6489    Output Parameter:
6490 .  matstruct - the sequential matrix with the nonzero structure of mat
6491 
6492   Level: intermediate
6493 
6494 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
6495 @*/
6496 PetscErrorCode  MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
6497 {
6498   PetscErrorCode ierr;
6499 
6500   PetscFunctionBegin;
6501   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6502   PetscValidPointer(matstruct,2);
6503 
6504   PetscValidType(mat,1);
6505   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6506   MatCheckPreallocated(mat,1);
6507 
6508   if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
6509   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6510   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
6511   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6512   PetscFunctionReturn(0);
6513 }
6514 
6515 #undef __FUNCT__
6516 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
6517 /*@C
6518    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
6519 
6520    Collective on Mat
6521 
6522    Input Parameters:
6523 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6524                        sequence of MatGetSequentialNonzeroStructure())
6525 
6526    Level: advanced
6527 
6528     Notes: Frees not only the matrices, but also the array that contains the matrices
6529 
6530 .seealso: MatGetSeqNonzeroStructure()
6531 @*/
6532 PetscErrorCode  MatDestroySeqNonzeroStructure(Mat *mat)
6533 {
6534   PetscErrorCode ierr;
6535 
6536   PetscFunctionBegin;
6537   PetscValidPointer(mat,1);
6538   ierr = MatDestroy(mat);CHKERRQ(ierr);
6539   PetscFunctionReturn(0);
6540 }
6541 
6542 #undef __FUNCT__
6543 #define __FUNCT__ "MatIncreaseOverlap"
6544 /*@
6545    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6546    replaces the index sets by larger ones that represent submatrices with
6547    additional overlap.
6548 
6549    Collective on Mat
6550 
6551    Input Parameters:
6552 +  mat - the matrix
6553 .  n   - the number of index sets
6554 .  is  - the array of index sets (these index sets will changed during the call)
6555 -  ov  - the additional overlap requested
6556 
6557    Level: developer
6558 
6559    Concepts: overlap
6560    Concepts: ASM^computing overlap
6561 
6562 .seealso: MatGetSubMatrices()
6563 @*/
6564 PetscErrorCode  MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6565 {
6566   PetscErrorCode ierr;
6567 
6568   PetscFunctionBegin;
6569   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6570   PetscValidType(mat,1);
6571   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6572   if (n) {
6573     PetscValidPointer(is,3);
6574     PetscValidHeaderSpecific(*is,IS_CLASSID,3);
6575   }
6576   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6577   if (mat->factortype)     SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6578   MatCheckPreallocated(mat,1);
6579 
6580   if (!ov) PetscFunctionReturn(0);
6581   if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6582   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6583   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
6584   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6585   PetscFunctionReturn(0);
6586 }
6587 
6588 #undef __FUNCT__
6589 #define __FUNCT__ "MatGetBlockSize"
6590 /*@
6591    MatGetBlockSize - Returns the matrix block size; useful especially for the
6592    block row and block diagonal formats.
6593 
6594    Not Collective
6595 
6596    Input Parameter:
6597 .  mat - the matrix
6598 
6599    Output Parameter:
6600 .  bs - block size
6601 
6602    Notes:
6603    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6604 
6605    Level: intermediate
6606 
6607    Concepts: matrices^block size
6608 
6609 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
6610 @*/
6611 PetscErrorCode  MatGetBlockSize(Mat mat,PetscInt *bs)
6612 {
6613 
6614   PetscFunctionBegin;
6615   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6616   PetscValidType(mat,1);
6617   PetscValidIntPointer(bs,2);
6618   MatCheckPreallocated(mat,1);
6619   *bs = mat->rmap->bs;
6620   PetscFunctionReturn(0);
6621 }
6622 
6623 #undef __FUNCT__
6624 #define __FUNCT__ "MatGetBlockSizes"
6625 /*@
6626    MatGetBlockSizes - Returns the matrix block row and column sizes;
6627    useful especially for the block row and block diagonal formats.
6628 
6629    Not Collective
6630 
6631    Input Parameter:
6632 .  mat - the matrix
6633 
6634    Output Parameter:
6635 .  rbs - row block size
6636 .  cbs - coumn block size
6637 
6638    Notes:
6639    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6640 
6641    Level: intermediate
6642 
6643    Concepts: matrices^block size
6644 
6645 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6646 @*/
6647 PetscErrorCode  MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
6648 {
6649 
6650   PetscFunctionBegin;
6651   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6652   PetscValidType(mat,1);
6653   if(rbs) PetscValidIntPointer(rbs,2);
6654   if(cbs) PetscValidIntPointer(cbs,3);
6655   MatCheckPreallocated(mat,1);
6656   if(rbs) *rbs = mat->rmap->bs;
6657   if(cbs) *cbs = mat->cmap->bs;
6658   PetscFunctionReturn(0);
6659 }
6660 
6661 #undef __FUNCT__
6662 #define __FUNCT__ "MatSetBlockSize"
6663 /*@
6664    MatSetBlockSize - Sets the matrix block size.
6665 
6666    Logically Collective on Mat
6667 
6668    Input Parameters:
6669 +  mat - the matrix
6670 -  bs - block size
6671 
6672    Notes:
6673      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6674 
6675    Level: intermediate
6676 
6677    Concepts: matrices^block size
6678 
6679 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6680 @*/
6681 PetscErrorCode  MatSetBlockSize(Mat mat,PetscInt bs)
6682 {
6683   PetscErrorCode ierr;
6684 
6685   PetscFunctionBegin;
6686   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6687   PetscValidLogicalCollectiveInt(mat,bs,2);
6688   ierr = PetscLayoutSetBlockSize(mat->rmap,bs);CHKERRQ(ierr);
6689   ierr = PetscLayoutSetBlockSize(mat->cmap,bs);CHKERRQ(ierr);
6690   PetscFunctionReturn(0);
6691 }
6692 
6693 #undef __FUNCT__
6694 #define __FUNCT__ "MatSetBlockSizes"
6695 /*@
6696    MatSetBlockSizes - Sets the matrix block row and column sizes.
6697 
6698    Logically Collective on Mat
6699 
6700    Input Parameters:
6701 +  mat - the matrix
6702 -  rbs - row block size
6703 -  cbs - column block size
6704 
6705    Notes:
6706      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6707 
6708    Level: intermediate
6709 
6710    Concepts: matrices^block size
6711 
6712 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6713 @*/
6714 PetscErrorCode  MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
6715 {
6716   PetscErrorCode ierr;
6717 
6718   PetscFunctionBegin;
6719   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6720   PetscValidLogicalCollectiveInt(mat,rbs,2);
6721   ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr);
6722   ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr);
6723   PetscFunctionReturn(0);
6724 }
6725 
6726 #undef __FUNCT__
6727 #define __FUNCT__ "MatGetRowIJ"
6728 /*@C
6729     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
6730 
6731    Collective on Mat
6732 
6733     Input Parameters:
6734 +   mat - the matrix
6735 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
6736 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
6737 -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
6738                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6739                  always used.
6740 
6741     Output Parameters:
6742 +   n - number of rows in the (possibly compressed) matrix
6743 .   ia - the row pointers [of length n+1]
6744 .   ja - the column indices
6745 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
6746            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
6747 
6748     Level: developer
6749 
6750     Notes: You CANNOT change any of the ia[] or ja[] values.
6751 
6752            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
6753 
6754     Fortran Node
6755 
6756            In Fortran use
6757 $           PetscInt ia(1), ja(1)
6758 $           PetscOffset iia, jja
6759 $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
6760 $
6761 $          or
6762 $
6763 $           PetscScalar, pointer :: xx_v(:)
6764 $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
6765 
6766 
6767        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
6768 
6769 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray()
6770 @*/
6771 PetscErrorCode  MatGetRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6772 {
6773   PetscErrorCode ierr;
6774 
6775   PetscFunctionBegin;
6776   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6777   PetscValidType(mat,1);
6778   PetscValidIntPointer(n,4);
6779   if (ia) PetscValidIntPointer(ia,5);
6780   if (ja) PetscValidIntPointer(ja,6);
6781   PetscValidIntPointer(done,7);
6782   MatCheckPreallocated(mat,1);
6783   if (!mat->ops->getrowij) *done = PETSC_FALSE;
6784   else {
6785     *done = PETSC_TRUE;
6786     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6787     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6788     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6789   }
6790   PetscFunctionReturn(0);
6791 }
6792 
6793 #undef __FUNCT__
6794 #define __FUNCT__ "MatGetColumnIJ"
6795 /*@C
6796     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
6797 
6798     Collective on Mat
6799 
6800     Input Parameters:
6801 +   mat - the matrix
6802 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6803 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6804                 symmetrized
6805 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6806                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6807                  always used.
6808 
6809     Output Parameters:
6810 +   n - number of columns in the (possibly compressed) matrix
6811 .   ia - the column pointers
6812 .   ja - the row indices
6813 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
6814 
6815     Level: developer
6816 
6817 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6818 @*/
6819 PetscErrorCode  MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6820 {
6821   PetscErrorCode ierr;
6822 
6823   PetscFunctionBegin;
6824   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6825   PetscValidType(mat,1);
6826   PetscValidIntPointer(n,4);
6827   if (ia) PetscValidIntPointer(ia,5);
6828   if (ja) PetscValidIntPointer(ja,6);
6829   PetscValidIntPointer(done,7);
6830   MatCheckPreallocated(mat,1);
6831   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
6832   else {
6833     *done = PETSC_TRUE;
6834     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6835   }
6836   PetscFunctionReturn(0);
6837 }
6838 
6839 #undef __FUNCT__
6840 #define __FUNCT__ "MatRestoreRowIJ"
6841 /*@C
6842     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
6843     MatGetRowIJ().
6844 
6845     Collective on Mat
6846 
6847     Input Parameters:
6848 +   mat - the matrix
6849 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6850 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6851                 symmetrized
6852 -   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6853                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6854                  always used.
6855 
6856     Output Parameters:
6857 +   n - size of (possibly compressed) matrix
6858 .   ia - the row pointers
6859 .   ja - the column indices
6860 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6861 
6862     Level: developer
6863 
6864 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6865 @*/
6866 PetscErrorCode  MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6867 {
6868   PetscErrorCode ierr;
6869 
6870   PetscFunctionBegin;
6871   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6872   PetscValidType(mat,1);
6873   if (ia) PetscValidIntPointer(ia,5);
6874   if (ja) PetscValidIntPointer(ja,6);
6875   PetscValidIntPointer(done,7);
6876   MatCheckPreallocated(mat,1);
6877 
6878   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
6879   else {
6880     *done = PETSC_TRUE;
6881     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6882   }
6883   PetscFunctionReturn(0);
6884 }
6885 
6886 #undef __FUNCT__
6887 #define __FUNCT__ "MatRestoreColumnIJ"
6888 /*@C
6889     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
6890     MatGetColumnIJ().
6891 
6892     Collective on Mat
6893 
6894     Input Parameters:
6895 +   mat - the matrix
6896 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6897 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6898                 symmetrized
6899 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6900                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6901                  always used.
6902 
6903     Output Parameters:
6904 +   n - size of (possibly compressed) matrix
6905 .   ia - the column pointers
6906 .   ja - the row indices
6907 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6908 
6909     Level: developer
6910 
6911 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
6912 @*/
6913 PetscErrorCode  MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6914 {
6915   PetscErrorCode ierr;
6916 
6917   PetscFunctionBegin;
6918   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6919   PetscValidType(mat,1);
6920   if (ia) PetscValidIntPointer(ia,5);
6921   if (ja) PetscValidIntPointer(ja,6);
6922   PetscValidIntPointer(done,7);
6923   MatCheckPreallocated(mat,1);
6924 
6925   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
6926   else {
6927     *done = PETSC_TRUE;
6928     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6929   }
6930   PetscFunctionReturn(0);
6931 }
6932 
6933 #undef __FUNCT__
6934 #define __FUNCT__ "MatColoringPatch"
6935 /*@C
6936     MatColoringPatch -Used inside matrix coloring routines that
6937     use MatGetRowIJ() and/or MatGetColumnIJ().
6938 
6939     Collective on Mat
6940 
6941     Input Parameters:
6942 +   mat - the matrix
6943 .   ncolors - max color value
6944 .   n   - number of entries in colorarray
6945 -   colorarray - array indicating color for each column
6946 
6947     Output Parameters:
6948 .   iscoloring - coloring generated using colorarray information
6949 
6950     Level: developer
6951 
6952 .seealso: MatGetRowIJ(), MatGetColumnIJ()
6953 
6954 @*/
6955 PetscErrorCode  MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
6956 {
6957   PetscErrorCode ierr;
6958 
6959   PetscFunctionBegin;
6960   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6961   PetscValidType(mat,1);
6962   PetscValidIntPointer(colorarray,4);
6963   PetscValidPointer(iscoloring,5);
6964   MatCheckPreallocated(mat,1);
6965 
6966   if (!mat->ops->coloringpatch){
6967     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6968   } else {
6969     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6970   }
6971   PetscFunctionReturn(0);
6972 }
6973 
6974 
6975 #undef __FUNCT__
6976 #define __FUNCT__ "MatSetUnfactored"
6977 /*@
6978    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
6979 
6980    Logically Collective on Mat
6981 
6982    Input Parameter:
6983 .  mat - the factored matrix to be reset
6984 
6985    Notes:
6986    This routine should be used only with factored matrices formed by in-place
6987    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
6988    format).  This option can save memory, for example, when solving nonlinear
6989    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
6990    ILU(0) preconditioner.
6991 
6992    Note that one can specify in-place ILU(0) factorization by calling
6993 .vb
6994      PCType(pc,PCILU);
6995      PCFactorSeUseInPlace(pc);
6996 .ve
6997    or by using the options -pc_type ilu -pc_factor_in_place
6998 
6999    In-place factorization ILU(0) can also be used as a local
7000    solver for the blocks within the block Jacobi or additive Schwarz
7001    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
7002    of these preconditioners in the <a href="../../docs/manual.pdf#ch_pc">PC chapter of the users manual</a> for details on setting
7003    local solver options.
7004 
7005    Most users should employ the simplified KSP interface for linear solvers
7006    instead of working directly with matrix algebra routines such as this.
7007    See, e.g., KSPCreate().
7008 
7009    Level: developer
7010 
7011 .seealso: PCFactorSetUseInPlace()
7012 
7013    Concepts: matrices^unfactored
7014 
7015 @*/
7016 PetscErrorCode  MatSetUnfactored(Mat mat)
7017 {
7018   PetscErrorCode ierr;
7019 
7020   PetscFunctionBegin;
7021   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7022   PetscValidType(mat,1);
7023   MatCheckPreallocated(mat,1);
7024   mat->factortype = MAT_FACTOR_NONE;
7025   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
7026   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
7027   PetscFunctionReturn(0);
7028 }
7029 
7030 /*MC
7031     MatGetArrayF90 - Accesses a matrix array from Fortran90.
7032 
7033     Synopsis:
7034     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)
7035 
7036     Not collective
7037 
7038     Input Parameter:
7039 .   x - matrix
7040 
7041     Output Parameters:
7042 +   xx_v - the Fortran90 pointer to the array
7043 -   ierr - error code
7044 
7045     Example of Usage:
7046 .vb
7047       PetscScalar, pointer xx_v(:,:)
7048       ....
7049       call MatGetArrayF90(x,xx_v,ierr)
7050       a = xx_v(3)
7051       call MatRestoreArrayF90(x,xx_v,ierr)
7052 .ve
7053 
7054     Notes:
7055     Not yet supported for all F90 compilers
7056 
7057     Level: advanced
7058 
7059 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
7060 
7061     Concepts: matrices^accessing array
7062 
7063 M*/
7064 
7065 /*MC
7066     MatRestoreArrayF90 - Restores a matrix array that has been
7067     accessed with MatGetArrayF90().
7068 
7069     Synopsis:
7070     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
7071 
7072     Not collective
7073 
7074     Input Parameters:
7075 +   x - matrix
7076 -   xx_v - the Fortran90 pointer to the array
7077 
7078     Output Parameter:
7079 .   ierr - error code
7080 
7081     Example of Usage:
7082 .vb
7083        PetscScalar, pointer xx_v(:)
7084        ....
7085        call MatGetArrayF90(x,xx_v,ierr)
7086        a = xx_v(3)
7087        call MatRestoreArrayF90(x,xx_v,ierr)
7088 .ve
7089 
7090     Notes:
7091     Not yet supported for all F90 compilers
7092 
7093     Level: advanced
7094 
7095 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
7096 
7097 M*/
7098 
7099 
7100 #undef __FUNCT__
7101 #define __FUNCT__ "MatGetSubMatrix"
7102 /*@
7103     MatGetSubMatrix - Gets a single submatrix on the same number of processors
7104                       as the original matrix.
7105 
7106     Collective on Mat
7107 
7108     Input Parameters:
7109 +   mat - the original matrix
7110 .   isrow - parallel IS containing the rows this processor should obtain
7111 .   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.
7112 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7113 
7114     Output Parameter:
7115 .   newmat - the new submatrix, of the same type as the old
7116 
7117     Level: advanced
7118 
7119     Notes:
7120     The submatrix will be able to be multiplied with vectors using the same layout as iscol.
7121 
7122     The rows in isrow will be sorted into the same order as the original matrix on each process.
7123 
7124       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7125    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
7126    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7127    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7128    you are finished using it.
7129 
7130     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7131     the input matrix.
7132 
7133     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran).
7134 
7135    Example usage:
7136    Consider the following 8x8 matrix with 34 non-zero values, that is
7137    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7138    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7139    as follows:
7140 
7141 .vb
7142             1  2  0  |  0  3  0  |  0  4
7143     Proc0   0  5  6  |  7  0  0  |  8  0
7144             9  0 10  | 11  0  0  | 12  0
7145     -------------------------------------
7146            13  0 14  | 15 16 17  |  0  0
7147     Proc1   0 18  0  | 19 20 21  |  0  0
7148             0  0  0  | 22 23  0  | 24  0
7149     -------------------------------------
7150     Proc2  25 26 27  |  0  0 28  | 29  0
7151            30  0  0  | 31 32 33  |  0 34
7152 .ve
7153 
7154     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is
7155 
7156 .vb
7157             2  0  |  0  3  0  |  0
7158     Proc0   5  6  |  7  0  0  |  8
7159     -------------------------------
7160     Proc1  18  0  | 19 20 21  |  0
7161     -------------------------------
7162     Proc2  26 27  |  0  0 28  | 29
7163             0  0  | 31 32 33  |  0
7164 .ve
7165 
7166 
7167     Concepts: matrices^submatrices
7168 
7169 .seealso: MatGetSubMatrices()
7170 @*/
7171 PetscErrorCode  MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7172 {
7173   PetscErrorCode ierr;
7174   PetscMPIInt    size;
7175   Mat            *local;
7176   IS             iscoltmp;
7177 
7178   PetscFunctionBegin;
7179   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7180   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
7181   if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
7182   PetscValidPointer(newmat,5);
7183   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5);
7184   PetscValidType(mat,1);
7185   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7186   MatCheckPreallocated(mat,1);
7187   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7188 
7189   if (!iscol) {
7190     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr);
7191   } else {
7192     iscoltmp = iscol;
7193   }
7194 
7195   /* if original matrix is on just one processor then use submatrix generated */
7196   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7197     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
7198     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7199     PetscFunctionReturn(0);
7200   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
7201     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
7202     *newmat = *local;
7203     ierr    = PetscFree(local);CHKERRQ(ierr);
7204     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7205     PetscFunctionReturn(0);
7206   } else if (!mat->ops->getsubmatrix) {
7207     /* Create a new matrix type that implements the operation using the full matrix */
7208     switch (cll) {
7209       case MAT_INITIAL_MATRIX:
7210         ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr);
7211         break;
7212       case MAT_REUSE_MATRIX:
7213         ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr);
7214         break;
7215       default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
7216     }
7217     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7218     PetscFunctionReturn(0);
7219   }
7220 
7221   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7222   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr);
7223   if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7224   if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);}
7225   PetscFunctionReturn(0);
7226 }
7227 
7228 #undef __FUNCT__
7229 #define __FUNCT__ "MatStashSetInitialSize"
7230 /*@
7231    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
7232    used during the assembly process to store values that belong to
7233    other processors.
7234 
7235    Not Collective
7236 
7237    Input Parameters:
7238 +  mat   - the matrix
7239 .  size  - the initial size of the stash.
7240 -  bsize - the initial size of the block-stash(if used).
7241 
7242    Options Database Keys:
7243 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
7244 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
7245 
7246    Level: intermediate
7247 
7248    Notes:
7249      The block-stash is used for values set with MatSetValuesBlocked() while
7250      the stash is used for values set with MatSetValues()
7251 
7252      Run with the option -info and look for output of the form
7253      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
7254      to determine the appropriate value, MM, to use for size and
7255      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
7256      to determine the value, BMM to use for bsize
7257 
7258    Concepts: stash^setting matrix size
7259    Concepts: matrices^stash
7260 
7261 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()
7262 
7263 @*/
7264 PetscErrorCode  MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
7265 {
7266   PetscErrorCode ierr;
7267 
7268   PetscFunctionBegin;
7269   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7270   PetscValidType(mat,1);
7271   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
7272   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
7273   PetscFunctionReturn(0);
7274 }
7275 
7276 #undef __FUNCT__
7277 #define __FUNCT__ "MatInterpolateAdd"
7278 /*@
7279    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
7280      the matrix
7281 
7282    Neighbor-wise Collective on Mat
7283 
7284    Input Parameters:
7285 +  mat   - the matrix
7286 .  x,y - the vectors
7287 -  w - where the result is stored
7288 
7289    Level: intermediate
7290 
7291    Notes:
7292     w may be the same vector as y.
7293 
7294     This allows one to use either the restriction or interpolation (its transpose)
7295     matrix to do the interpolation
7296 
7297     Concepts: interpolation
7298 
7299 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7300 
7301 @*/
7302 PetscErrorCode  MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
7303 {
7304   PetscErrorCode ierr;
7305   PetscInt       M,N,Ny;
7306 
7307   PetscFunctionBegin;
7308   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7309   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7310   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7311   PetscValidHeaderSpecific(w,VEC_CLASSID,4);
7312   PetscValidType(A,1);
7313   MatCheckPreallocated(A,1);
7314   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7315   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7316   if (M == Ny) {
7317     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
7318   } else {
7319     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
7320   }
7321   PetscFunctionReturn(0);
7322 }
7323 
7324 #undef __FUNCT__
7325 #define __FUNCT__ "MatInterpolate"
7326 /*@
7327    MatInterpolate - y = A*x or A'*x depending on the shape of
7328      the matrix
7329 
7330    Neighbor-wise Collective on Mat
7331 
7332    Input Parameters:
7333 +  mat   - the matrix
7334 -  x,y - the vectors
7335 
7336    Level: intermediate
7337 
7338    Notes:
7339     This allows one to use either the restriction or interpolation (its transpose)
7340     matrix to do the interpolation
7341 
7342    Concepts: matrices^interpolation
7343 
7344 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7345 
7346 @*/
7347 PetscErrorCode  MatInterpolate(Mat A,Vec x,Vec y)
7348 {
7349   PetscErrorCode ierr;
7350   PetscInt       M,N,Ny;
7351 
7352   PetscFunctionBegin;
7353   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7354   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7355   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7356   PetscValidType(A,1);
7357   MatCheckPreallocated(A,1);
7358   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7359   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7360   if (M == Ny) {
7361     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7362   } else {
7363     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7364   }
7365   PetscFunctionReturn(0);
7366 }
7367 
7368 #undef __FUNCT__
7369 #define __FUNCT__ "MatRestrict"
7370 /*@
7371    MatRestrict - y = A*x or A'*x
7372 
7373    Neighbor-wise Collective on Mat
7374 
7375    Input Parameters:
7376 +  mat   - the matrix
7377 -  x,y - the vectors
7378 
7379    Level: intermediate
7380 
7381    Notes:
7382     This allows one to use either the restriction or interpolation (its transpose)
7383     matrix to do the restriction
7384 
7385    Concepts: matrices^restriction
7386 
7387 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
7388 
7389 @*/
7390 PetscErrorCode  MatRestrict(Mat A,Vec x,Vec y)
7391 {
7392   PetscErrorCode ierr;
7393   PetscInt       M,N,Ny;
7394 
7395   PetscFunctionBegin;
7396   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7397   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7398   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7399   PetscValidType(A,1);
7400   MatCheckPreallocated(A,1);
7401 
7402   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7403   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7404   if (M == Ny) {
7405     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7406   } else {
7407     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7408   }
7409   PetscFunctionReturn(0);
7410 }
7411 
7412 #undef __FUNCT__
7413 #define __FUNCT__ "MatGetNullSpace"
7414 /*@
7415    MatGetNullSpace - retrieves the null space to a matrix.
7416 
7417    Logically Collective on Mat and MatNullSpace
7418 
7419    Input Parameters:
7420 +  mat - the matrix
7421 -  nullsp - the null space object
7422 
7423    Level: developer
7424 
7425    Notes:
7426       This null space is used by solvers. Overwrites any previous null space that may have been attached
7427 
7428    Concepts: null space^attaching to matrix
7429 
7430 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7431 @*/
7432 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
7433 {
7434   PetscFunctionBegin;
7435   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7436   PetscValidType(mat,1);
7437   PetscValidPointer(nullsp,2);
7438   *nullsp = mat->nullsp;
7439   PetscFunctionReturn(0);
7440 }
7441 
7442 #undef __FUNCT__
7443 #define __FUNCT__ "MatSetNullSpace"
7444 /*@
7445    MatSetNullSpace - attaches a null space to a matrix.
7446         This null space will be removed from the resulting vector whenever
7447         MatMult() is called
7448 
7449    Logically Collective on Mat and MatNullSpace
7450 
7451    Input Parameters:
7452 +  mat - the matrix
7453 -  nullsp - the null space object
7454 
7455    Level: advanced
7456 
7457    Notes:
7458       This null space is used by solvers. Overwrites any previous null space that may have been attached
7459 
7460    Concepts: null space^attaching to matrix
7461 
7462 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7463 @*/
7464 PetscErrorCode  MatSetNullSpace(Mat mat,MatNullSpace nullsp)
7465 {
7466   PetscErrorCode ierr;
7467 
7468   PetscFunctionBegin;
7469   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7470   PetscValidType(mat,1);
7471   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7472   MatCheckPreallocated(mat,1);
7473   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7474   ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr);
7475   mat->nullsp = nullsp;
7476   PetscFunctionReturn(0);
7477 }
7478 
7479 #undef __FUNCT__
7480 #define __FUNCT__ "MatSetNearNullSpace"
7481 /*@
7482    MatSetNearNullSpace - attaches a null space to a matrix.
7483         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.
7484 
7485    Logically Collective on Mat and MatNullSpace
7486 
7487    Input Parameters:
7488 +  mat - the matrix
7489 -  nullsp - the null space object
7490 
7491    Level: advanced
7492 
7493    Notes:
7494       Overwrites any previous near null space that may have been attached
7495 
7496    Concepts: null space^attaching to matrix
7497 
7498 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace()
7499 @*/
7500 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
7501 {
7502   PetscErrorCode ierr;
7503 
7504   PetscFunctionBegin;
7505   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7506   PetscValidType(mat,1);
7507   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7508   MatCheckPreallocated(mat,1);
7509   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7510   ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr);
7511   mat->nearnullsp = nullsp;
7512   PetscFunctionReturn(0);
7513 }
7514 
7515 #undef __FUNCT__
7516 #define __FUNCT__ "MatGetNearNullSpace"
7517 /*@
7518    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()
7519 
7520    Not Collective
7521 
7522    Input Parameters:
7523 .  mat - the matrix
7524 
7525    Output Parameters:
7526 .  nullsp - the null space object, PETSC_NULL if not set
7527 
7528    Level: developer
7529 
7530    Concepts: null space^attaching to matrix
7531 
7532 .seealso: MatSetNearNullSpace(), MatGetNullSpace()
7533 @*/
7534 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
7535 {
7536 
7537   PetscFunctionBegin;
7538   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7539   PetscValidType(mat,1);
7540   PetscValidPointer(nullsp,2);
7541   MatCheckPreallocated(mat,1);
7542   *nullsp = mat->nearnullsp;
7543   PetscFunctionReturn(0);
7544 }
7545 
7546 #undef __FUNCT__
7547 #define __FUNCT__ "MatICCFactor"
7548 /*@C
7549    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
7550 
7551    Collective on Mat
7552 
7553    Input Parameters:
7554 +  mat - the matrix
7555 .  row - row/column permutation
7556 .  fill - expected fill factor >= 1.0
7557 -  level - level of fill, for ICC(k)
7558 
7559    Notes:
7560    Probably really in-place only when level of fill is zero, otherwise allocates
7561    new space to store factored matrix and deletes previous memory.
7562 
7563    Most users should employ the simplified KSP interface for linear solvers
7564    instead of working directly with matrix algebra routines such as this.
7565    See, e.g., KSPCreate().
7566 
7567    Level: developer
7568 
7569    Concepts: matrices^incomplete Cholesky factorization
7570    Concepts: Cholesky factorization
7571 
7572 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
7573 
7574     Developer Note: fortran interface is not autogenerated as the f90
7575     interface defintion cannot be generated correctly [due to MatFactorInfo]
7576 
7577 @*/
7578 PetscErrorCode  MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
7579 {
7580   PetscErrorCode ierr;
7581 
7582   PetscFunctionBegin;
7583   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7584   PetscValidType(mat,1);
7585   if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2);
7586   PetscValidPointer(info,3);
7587   if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square");
7588   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7589   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7590   if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7591   MatCheckPreallocated(mat,1);
7592   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
7593   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7594   PetscFunctionReturn(0);
7595 }
7596 
7597 #undef __FUNCT__
7598 #define __FUNCT__ "MatSetValuesAdic"
7599 /*@
7600    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
7601 
7602    Not Collective
7603 
7604    Input Parameters:
7605 +  mat - the matrix
7606 -  v - the values compute with ADIC
7607 
7608    Level: developer
7609 
7610    Notes:
7611      Must call MatSetColoring() before using this routine. Also this matrix must already
7612      have its nonzero pattern determined.
7613 
7614 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7615           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
7616 @*/
7617 PetscErrorCode  MatSetValuesAdic(Mat mat,void *v)
7618 {
7619   PetscErrorCode ierr;
7620 
7621   PetscFunctionBegin;
7622   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7623   PetscValidType(mat,1);
7624   PetscValidPointer(mat,2);
7625 
7626   if (!mat->assembled) {
7627     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7628   }
7629   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7630   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7631   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
7632   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7633   ierr = MatView_Private(mat);CHKERRQ(ierr);
7634   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7635   PetscFunctionReturn(0);
7636 }
7637 
7638 
7639 #undef __FUNCT__
7640 #define __FUNCT__ "MatSetColoring"
7641 /*@
7642    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
7643 
7644    Not Collective
7645 
7646    Input Parameters:
7647 +  mat - the matrix
7648 -  coloring - the coloring
7649 
7650    Level: developer
7651 
7652 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7653           MatSetValues(), MatSetValuesAdic()
7654 @*/
7655 PetscErrorCode  MatSetColoring(Mat mat,ISColoring coloring)
7656 {
7657   PetscErrorCode ierr;
7658 
7659   PetscFunctionBegin;
7660   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7661   PetscValidType(mat,1);
7662   PetscValidPointer(coloring,2);
7663 
7664   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7665   if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7666   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
7667   PetscFunctionReturn(0);
7668 }
7669 
7670 #undef __FUNCT__
7671 #define __FUNCT__ "MatSetValuesAdifor"
7672 /*@
7673    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
7674 
7675    Not Collective
7676 
7677    Input Parameters:
7678 +  mat - the matrix
7679 .  nl - leading dimension of v
7680 -  v - the values compute with ADIFOR
7681 
7682    Level: developer
7683 
7684    Notes:
7685      Must call MatSetColoring() before using this routine. Also this matrix must already
7686      have its nonzero pattern determined.
7687 
7688 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7689           MatSetValues(), MatSetColoring()
7690 @*/
7691 PetscErrorCode  MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
7692 {
7693   PetscErrorCode ierr;
7694 
7695   PetscFunctionBegin;
7696   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7697   PetscValidType(mat,1);
7698   PetscValidPointer(v,3);
7699 
7700   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7701   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7702   if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7703   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
7704   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7705   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7706   PetscFunctionReturn(0);
7707 }
7708 
7709 #undef __FUNCT__
7710 #define __FUNCT__ "MatDiagonalScaleLocal"
7711 /*@
7712    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
7713          ghosted ones.
7714 
7715    Not Collective
7716 
7717    Input Parameters:
7718 +  mat - the matrix
7719 -  diag = the diagonal values, including ghost ones
7720 
7721    Level: developer
7722 
7723    Notes: Works only for MPIAIJ and MPIBAIJ matrices
7724 
7725 .seealso: MatDiagonalScale()
7726 @*/
7727 PetscErrorCode  MatDiagonalScaleLocal(Mat mat,Vec diag)
7728 {
7729   PetscErrorCode ierr;
7730   PetscMPIInt    size;
7731 
7732   PetscFunctionBegin;
7733   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7734   PetscValidHeaderSpecific(diag,VEC_CLASSID,2);
7735   PetscValidType(mat,1);
7736 
7737   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7738   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7739   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7740   if (size == 1) {
7741     PetscInt n,m;
7742     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
7743     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
7744     if (m == n) {
7745       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
7746     } else {
7747       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
7748     }
7749   } else {
7750     ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr);
7751   }
7752   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7753   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7754   PetscFunctionReturn(0);
7755 }
7756 
7757 #undef __FUNCT__
7758 #define __FUNCT__ "MatGetInertia"
7759 /*@
7760    MatGetInertia - Gets the inertia from a factored matrix
7761 
7762    Collective on Mat
7763 
7764    Input Parameter:
7765 .  mat - the matrix
7766 
7767    Output Parameters:
7768 +   nneg - number of negative eigenvalues
7769 .   nzero - number of zero eigenvalues
7770 -   npos - number of positive eigenvalues
7771 
7772    Level: advanced
7773 
7774    Notes: Matrix must have been factored by MatCholeskyFactor()
7775 
7776 
7777 @*/
7778 PetscErrorCode  MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
7779 {
7780   PetscErrorCode ierr;
7781 
7782   PetscFunctionBegin;
7783   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7784   PetscValidType(mat,1);
7785   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7786   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
7787   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7788   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
7789   PetscFunctionReturn(0);
7790 }
7791 
7792 /* ----------------------------------------------------------------*/
7793 #undef __FUNCT__
7794 #define __FUNCT__ "MatSolves"
7795 /*@C
7796    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
7797 
7798    Neighbor-wise Collective on Mat and Vecs
7799 
7800    Input Parameters:
7801 +  mat - the factored matrix
7802 -  b - the right-hand-side vectors
7803 
7804    Output Parameter:
7805 .  x - the result vectors
7806 
7807    Notes:
7808    The vectors b and x cannot be the same.  I.e., one cannot
7809    call MatSolves(A,x,x).
7810 
7811    Notes:
7812    Most users should employ the simplified KSP interface for linear solvers
7813    instead of working directly with matrix algebra routines such as this.
7814    See, e.g., KSPCreate().
7815 
7816    Level: developer
7817 
7818    Concepts: matrices^triangular solves
7819 
7820 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
7821 @*/
7822 PetscErrorCode  MatSolves(Mat mat,Vecs b,Vecs x)
7823 {
7824   PetscErrorCode ierr;
7825 
7826   PetscFunctionBegin;
7827   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7828   PetscValidType(mat,1);
7829   if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors");
7830   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7831   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
7832 
7833   if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7834   MatCheckPreallocated(mat,1);
7835   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7836   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
7837   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7838   PetscFunctionReturn(0);
7839 }
7840 
7841 #undef __FUNCT__
7842 #define __FUNCT__ "MatIsSymmetric"
7843 /*@
7844    MatIsSymmetric - Test whether a matrix is symmetric
7845 
7846    Collective on Mat
7847 
7848    Input Parameter:
7849 +  A - the matrix to test
7850 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
7851 
7852    Output Parameters:
7853 .  flg - the result
7854 
7855    Notes: For real numbers MatIsSymmetric() and MatIsHermitian() return identical results
7856 
7857    Level: intermediate
7858 
7859    Concepts: matrix^symmetry
7860 
7861 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7862 @*/
7863 PetscErrorCode  MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
7864 {
7865   PetscErrorCode ierr;
7866 
7867   PetscFunctionBegin;
7868   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7869   PetscValidPointer(flg,2);
7870 
7871   if (!A->symmetric_set) {
7872     if (!A->ops->issymmetric) {
7873       const MatType mattype;
7874       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7875       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7876     }
7877     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7878     if (!tol) {
7879       A->symmetric_set = PETSC_TRUE;
7880       A->symmetric = *flg;
7881       if (A->symmetric) {
7882 	A->structurally_symmetric_set = PETSC_TRUE;
7883 	A->structurally_symmetric     = PETSC_TRUE;
7884       }
7885     }
7886   } else if (A->symmetric) {
7887     *flg = PETSC_TRUE;
7888   } else if (!tol) {
7889     *flg = PETSC_FALSE;
7890   } else {
7891     if (!A->ops->issymmetric) {
7892       const MatType mattype;
7893       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7894       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7895     }
7896     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7897   }
7898   PetscFunctionReturn(0);
7899 }
7900 
7901 #undef __FUNCT__
7902 #define __FUNCT__ "MatIsHermitian"
7903 /*@
7904    MatIsHermitian - Test whether a matrix is Hermitian
7905 
7906    Collective on Mat
7907 
7908    Input Parameter:
7909 +  A - the matrix to test
7910 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
7911 
7912    Output Parameters:
7913 .  flg - the result
7914 
7915    Level: intermediate
7916 
7917    Concepts: matrix^symmetry
7918 
7919 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
7920           MatIsSymmetricKnown(), MatIsSymmetric()
7921 @*/
7922 PetscErrorCode  MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
7923 {
7924   PetscErrorCode ierr;
7925 
7926   PetscFunctionBegin;
7927   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7928   PetscValidPointer(flg,2);
7929 
7930   if (!A->hermitian_set) {
7931     if (!A->ops->ishermitian) {
7932       const MatType mattype;
7933       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7934       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7935     }
7936     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7937     if (!tol) {
7938       A->hermitian_set = PETSC_TRUE;
7939       A->hermitian = *flg;
7940       if (A->hermitian) {
7941 	A->structurally_symmetric_set = PETSC_TRUE;
7942 	A->structurally_symmetric     = PETSC_TRUE;
7943       }
7944     }
7945   } else if (A->hermitian) {
7946     *flg = PETSC_TRUE;
7947   } else if (!tol) {
7948     *flg = PETSC_FALSE;
7949   } else {
7950     if (!A->ops->ishermitian) {
7951       const MatType mattype;
7952       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7953       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7954     }
7955     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7956   }
7957   PetscFunctionReturn(0);
7958 }
7959 
7960 #undef __FUNCT__
7961 #define __FUNCT__ "MatIsSymmetricKnown"
7962 /*@
7963    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
7964 
7965    Not Collective
7966 
7967    Input Parameter:
7968 .  A - the matrix to check
7969 
7970    Output Parameters:
7971 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
7972 -  flg - the result
7973 
7974    Level: advanced
7975 
7976    Concepts: matrix^symmetry
7977 
7978    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
7979          if you want it explicitly checked
7980 
7981 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
7982 @*/
7983 PetscErrorCode  MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
7984 {
7985   PetscFunctionBegin;
7986   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7987   PetscValidPointer(set,2);
7988   PetscValidPointer(flg,3);
7989   if (A->symmetric_set) {
7990     *set = PETSC_TRUE;
7991     *flg = A->symmetric;
7992   } else {
7993     *set = PETSC_FALSE;
7994   }
7995   PetscFunctionReturn(0);
7996 }
7997 
7998 #undef __FUNCT__
7999 #define __FUNCT__ "MatIsHermitianKnown"
8000 /*@
8001    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
8002 
8003    Not Collective
8004 
8005    Input Parameter:
8006 .  A - the matrix to check
8007 
8008    Output Parameters:
8009 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8010 -  flg - the result
8011 
8012    Level: advanced
8013 
8014    Concepts: matrix^symmetry
8015 
8016    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8017          if you want it explicitly checked
8018 
8019 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8020 @*/
8021 PetscErrorCode  MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8022 {
8023   PetscFunctionBegin;
8024   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8025   PetscValidPointer(set,2);
8026   PetscValidPointer(flg,3);
8027   if (A->hermitian_set) {
8028     *set = PETSC_TRUE;
8029     *flg = A->hermitian;
8030   } else {
8031     *set = PETSC_FALSE;
8032   }
8033   PetscFunctionReturn(0);
8034 }
8035 
8036 #undef __FUNCT__
8037 #define __FUNCT__ "MatIsStructurallySymmetric"
8038 /*@
8039    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
8040 
8041    Collective on Mat
8042 
8043    Input Parameter:
8044 .  A - the matrix to test
8045 
8046    Output Parameters:
8047 .  flg - the result
8048 
8049    Level: intermediate
8050 
8051    Concepts: matrix^symmetry
8052 
8053 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8054 @*/
8055 PetscErrorCode  MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8056 {
8057   PetscErrorCode ierr;
8058 
8059   PetscFunctionBegin;
8060   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8061   PetscValidPointer(flg,2);
8062   if (!A->structurally_symmetric_set) {
8063     if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8064     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
8065     A->structurally_symmetric_set = PETSC_TRUE;
8066   }
8067   *flg = A->structurally_symmetric;
8068   PetscFunctionReturn(0);
8069 }
8070 
8071 #undef __FUNCT__
8072 #define __FUNCT__ "MatStashGetInfo"
8073 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
8074 /*@
8075    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8076        to be communicated to other processors during the MatAssemblyBegin/End() process
8077 
8078     Not collective
8079 
8080    Input Parameter:
8081 .   vec - the vector
8082 
8083    Output Parameters:
8084 +   nstash   - the size of the stash
8085 .   reallocs - the number of additional mallocs incurred.
8086 .   bnstash   - the size of the block stash
8087 -   breallocs - the number of additional mallocs incurred.in the block stash
8088 
8089    Level: advanced
8090 
8091 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
8092 
8093 @*/
8094 PetscErrorCode  MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8095 {
8096   PetscErrorCode ierr;
8097   PetscFunctionBegin;
8098   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
8099   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
8100   PetscFunctionReturn(0);
8101 }
8102 
8103 #undef __FUNCT__
8104 #define __FUNCT__ "MatGetVecs"
8105 /*@C
8106    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
8107      parallel layout
8108 
8109    Collective on Mat
8110 
8111    Input Parameter:
8112 .  mat - the matrix
8113 
8114    Output Parameter:
8115 +   right - (optional) vector that the matrix can be multiplied against
8116 -   left - (optional) vector that the matrix vector product can be stored in
8117 
8118   Level: advanced
8119 
8120 .seealso: MatCreate()
8121 @*/
8122 PetscErrorCode  MatGetVecs(Mat mat,Vec *right,Vec *left)
8123 {
8124   PetscErrorCode ierr;
8125 
8126   PetscFunctionBegin;
8127   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8128   PetscValidType(mat,1);
8129   MatCheckPreallocated(mat,1);
8130   if (mat->ops->getvecs) {
8131     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
8132   } else {
8133     PetscMPIInt size;
8134     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
8135     if (right) {
8136       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
8137       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8138       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
8139       ierr = VecSetType(*right,VECSTANDARD);CHKERRQ(ierr);
8140       ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr);
8141     }
8142     if (left) {
8143       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
8144       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8145       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
8146       ierr = VecSetType(*left,VECSTANDARD);CHKERRQ(ierr);
8147       ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr);
8148     }
8149   }
8150   PetscFunctionReturn(0);
8151 }
8152 
8153 #undef __FUNCT__
8154 #define __FUNCT__ "MatFactorInfoInitialize"
8155 /*@C
8156    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8157      with default values.
8158 
8159    Not Collective
8160 
8161    Input Parameters:
8162 .    info - the MatFactorInfo data structure
8163 
8164 
8165    Notes: The solvers are generally used through the KSP and PC objects, for example
8166           PCLU, PCILU, PCCHOLESKY, PCICC
8167 
8168    Level: developer
8169 
8170 .seealso: MatFactorInfo
8171 
8172     Developer Note: fortran interface is not autogenerated as the f90
8173     interface defintion cannot be generated correctly [due to MatFactorInfo]
8174 
8175 @*/
8176 
8177 PetscErrorCode  MatFactorInfoInitialize(MatFactorInfo *info)
8178 {
8179   PetscErrorCode ierr;
8180 
8181   PetscFunctionBegin;
8182   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
8183   PetscFunctionReturn(0);
8184 }
8185 
8186 #undef __FUNCT__
8187 #define __FUNCT__ "MatPtAP"
8188 /*@
8189    MatPtAP - Creates the matrix product C = P^T * A * P
8190 
8191    Neighbor-wise Collective on Mat
8192 
8193    Input Parameters:
8194 +  A - the matrix
8195 .  P - the projection matrix
8196 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8197 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P))
8198 
8199    Output Parameters:
8200 .  C - the product matrix
8201 
8202    Notes:
8203    C will be created and must be destroyed by the user with MatDestroy().
8204 
8205    This routine is currently only implemented for pairs of AIJ matrices and classes
8206    which inherit from AIJ.
8207 
8208    Level: intermediate
8209 
8210 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
8211 @*/
8212 PetscErrorCode  MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8213 {
8214   PetscErrorCode ierr;
8215 
8216   PetscFunctionBegin;
8217   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8218   PetscValidType(A,1);
8219   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8220   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8221   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8222   PetscValidType(P,2);
8223   MatCheckPreallocated(P,2);
8224   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8225   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8226   PetscValidPointer(C,3);
8227   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);
8228   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8229   MatCheckPreallocated(A,1);
8230 
8231   if (!A->ops->ptap) {
8232     const MatType mattype;
8233     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8234     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype);
8235   }
8236   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8237   ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
8238   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8239   PetscFunctionReturn(0);
8240 }
8241 
8242 #undef __FUNCT__
8243 #define __FUNCT__ "MatPtAPNumeric"
8244 /*@
8245    MatPtAPNumeric - Computes the matrix product C = P^T * A * P
8246 
8247    Neighbor-wise Collective on Mat
8248 
8249    Input Parameters:
8250 +  A - the matrix
8251 -  P - the projection matrix
8252 
8253    Output Parameters:
8254 .  C - the product matrix
8255 
8256    Notes:
8257    C must have been created by calling MatPtAPSymbolic and must be destroyed by
8258    the user using MatDeatroy().
8259 
8260    This routine is currently only implemented for pairs of AIJ matrices and classes
8261    which inherit from AIJ.  C will be of type MATAIJ.
8262 
8263    Level: intermediate
8264 
8265 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
8266 @*/
8267 PetscErrorCode  MatPtAPNumeric(Mat A,Mat P,Mat C)
8268 {
8269   PetscErrorCode ierr;
8270 
8271   PetscFunctionBegin;
8272   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8273   PetscValidType(A,1);
8274   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8275   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8276   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8277   PetscValidType(P,2);
8278   MatCheckPreallocated(P,2);
8279   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8280   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8281   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8282   PetscValidType(C,3);
8283   MatCheckPreallocated(C,3);
8284   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8285   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);
8286   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);
8287   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);
8288   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);
8289   MatCheckPreallocated(A,1);
8290 
8291   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8292   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
8293   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8294   PetscFunctionReturn(0);
8295 }
8296 
8297 #undef __FUNCT__
8298 #define __FUNCT__ "MatPtAPSymbolic"
8299 /*@
8300    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P
8301 
8302    Neighbor-wise Collective on Mat
8303 
8304    Input Parameters:
8305 +  A - the matrix
8306 -  P - the projection matrix
8307 
8308    Output Parameters:
8309 .  C - the (i,j) structure of the product matrix
8310 
8311    Notes:
8312    C will be created and must be destroyed by the user with MatDestroy().
8313 
8314    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8315    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8316    this (i,j) structure by calling MatPtAPNumeric().
8317 
8318    Level: intermediate
8319 
8320 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
8321 @*/
8322 PetscErrorCode  MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
8323 {
8324   PetscErrorCode ierr;
8325 
8326   PetscFunctionBegin;
8327   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8328   PetscValidType(A,1);
8329   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8330   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8331   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8332   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8333   PetscValidType(P,2);
8334   MatCheckPreallocated(P,2);
8335   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8336   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8337   PetscValidPointer(C,3);
8338 
8339   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);
8340   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);
8341   MatCheckPreallocated(A,1);
8342   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8343   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
8344   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8345 
8346   /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */
8347 
8348   PetscFunctionReturn(0);
8349 }
8350 
8351 #undef __FUNCT__
8352 #define __FUNCT__ "MatRARt"
8353 /*@
8354    MatRARt - Creates the matrix product C = R * A * R^T
8355 
8356    Neighbor-wise Collective on Mat
8357 
8358    Input Parameters:
8359 +  A - the matrix
8360 .  R - the projection matrix
8361 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8362 -  fill - expected fill as ratio of nnz(C)/nnz(A)
8363 
8364    Output Parameters:
8365 .  C - the product matrix
8366 
8367    Notes:
8368    C will be created and must be destroyed by the user with MatDestroy().
8369 
8370    This routine is currently only implemented for pairs of AIJ matrices and classes
8371    which inherit from AIJ.
8372 
8373    Level: intermediate
8374 
8375 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
8376 @*/
8377 PetscErrorCode  MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
8378 {
8379   PetscErrorCode ierr;
8380 
8381   PetscFunctionBegin;
8382   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8383   PetscValidType(A,1);
8384   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8385   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8386   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8387   PetscValidType(R,2);
8388   MatCheckPreallocated(R,2);
8389   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8390   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8391   PetscValidPointer(C,3);
8392   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);
8393   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8394   MatCheckPreallocated(A,1);
8395 
8396   if (!A->ops->rart) {
8397     const MatType mattype;
8398     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8399     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support RARt",mattype);
8400   }
8401   ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8402   ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr);
8403   ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8404   PetscFunctionReturn(0);
8405 }
8406 
8407 #undef __FUNCT__
8408 #define __FUNCT__ "MatRARtNumeric"
8409 /*@
8410    MatRARtNumeric - Computes the matrix product C = R * A * R^T
8411 
8412    Neighbor-wise Collective on Mat
8413 
8414    Input Parameters:
8415 +  A - the matrix
8416 -  R - the projection matrix
8417 
8418    Output Parameters:
8419 .  C - the product matrix
8420 
8421    Notes:
8422    C must have been created by calling MatRARtSymbolic and must be destroyed by
8423    the user using MatDeatroy().
8424 
8425    This routine is currently only implemented for pairs of AIJ matrices and classes
8426    which inherit from AIJ.  C will be of type MATAIJ.
8427 
8428    Level: intermediate
8429 
8430 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
8431 @*/
8432 PetscErrorCode  MatRARtNumeric(Mat A,Mat R,Mat C)
8433 {
8434   PetscErrorCode ierr;
8435 
8436   PetscFunctionBegin;
8437   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8438   PetscValidType(A,1);
8439   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8440   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8441   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8442   PetscValidType(R,2);
8443   MatCheckPreallocated(R,2);
8444   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8445   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8446   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8447   PetscValidType(C,3);
8448   MatCheckPreallocated(C,3);
8449   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8450   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);
8451   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);
8452   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);
8453   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);
8454   MatCheckPreallocated(A,1);
8455 
8456   ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8457   ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr);
8458   ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8459   PetscFunctionReturn(0);
8460 }
8461 
8462 #undef __FUNCT__
8463 #define __FUNCT__ "MatRARtSymbolic"
8464 /*@
8465    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T
8466 
8467    Neighbor-wise Collective on Mat
8468 
8469    Input Parameters:
8470 +  A - the matrix
8471 -  R - the projection matrix
8472 
8473    Output Parameters:
8474 .  C - the (i,j) structure of the product matrix
8475 
8476    Notes:
8477    C will be created and must be destroyed by the user with MatDestroy().
8478 
8479    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8480    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8481    this (i,j) structure by calling MatRARtNumeric().
8482 
8483    Level: intermediate
8484 
8485 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
8486 @*/
8487 PetscErrorCode  MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
8488 {
8489   PetscErrorCode ierr;
8490 
8491   PetscFunctionBegin;
8492   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8493   PetscValidType(A,1);
8494   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8495   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8496   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8497   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8498   PetscValidType(R,2);
8499   MatCheckPreallocated(R,2);
8500   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8501   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8502   PetscValidPointer(C,3);
8503 
8504   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);
8505   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);
8506   MatCheckPreallocated(A,1);
8507   ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8508   ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr);
8509   ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8510 
8511   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
8512   PetscFunctionReturn(0);
8513 }
8514 
8515 extern PetscErrorCode MatQueryOp(MPI_Comm comm, void (**function)(void), const char op[], PetscInt numArgs, ...);
8516 
8517 #undef __FUNCT__
8518 #define __FUNCT__ "MatMatMult"
8519 /*@
8520    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
8521 
8522    Neighbor-wise Collective on Mat
8523 
8524    Input Parameters:
8525 +  A - the left matrix
8526 .  B - the right matrix
8527 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8528 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
8529           if the result is a dense matrix this is irrelevent
8530 
8531    Output Parameters:
8532 .  C - the product matrix
8533 
8534    Notes:
8535    Unless scall is MAT_REUSE_MATRIX C will be created.
8536 
8537    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8538 
8539    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8540    actually needed.
8541 
8542    If you have many matrices with the same non-zero structure to multiply, you
8543    should either
8544 $   1) use MAT_REUSE_MATRIX in all calls but the first or
8545 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
8546 
8547    Level: intermediate
8548 
8549 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
8550 @*/
8551 PetscErrorCode  MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8552 {
8553   PetscErrorCode ierr;
8554   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8555   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8556   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
8557 
8558   PetscFunctionBegin;
8559   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8560   PetscValidType(A,1);
8561   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8562   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8563   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8564   PetscValidType(B,2);
8565   MatCheckPreallocated(B,2);
8566   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8567   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8568   PetscValidPointer(C,3);
8569   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);
8570   if (scall == MAT_REUSE_MATRIX){
8571     PetscValidPointer(*C,5);
8572     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
8573     ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8574     ierr = (*(*C)->ops->matmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8575     ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8576   }
8577   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8578   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8579   MatCheckPreallocated(A,1);
8580 
8581   fA = A->ops->matmult;
8582   fB = B->ops->matmult;
8583   if (fB == fA) {
8584     if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8585     mult = fB;
8586   } else {
8587     /* dispatch based on the type of A and B from their PetscObject's PetscFLists. */
8588     char  multname[256];
8589     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
8590     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8591     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
8592     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8593     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
8594     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
8595     if(!mult){
8596       /* dual dispatch using MatQueryOp */
8597       ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&mult), "MatMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8598       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);
8599     }
8600   }
8601   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8602   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
8603   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8604   PetscFunctionReturn(0);
8605 }
8606 
8607 #undef __FUNCT__
8608 #define __FUNCT__ "MatMatMultSymbolic"
8609 /*@
8610    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
8611    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
8612 
8613    Neighbor-wise Collective on Mat
8614 
8615    Input Parameters:
8616 +  A - the left matrix
8617 .  B - the right matrix
8618 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
8619       if C is a dense matrix this is irrelevent
8620 
8621    Output Parameters:
8622 .  C - the product matrix
8623 
8624    Notes:
8625    Unless scall is MAT_REUSE_MATRIX C will be created.
8626 
8627    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8628    actually needed.
8629 
8630    This routine is currently implemented for
8631     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
8632     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8633     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8634 
8635    Level: intermediate
8636 
8637    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
8638      We should incorporate them into PETSc.
8639 
8640 .seealso: MatMatMult(), MatMatMultNumeric()
8641 @*/
8642 PetscErrorCode  MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
8643 {
8644   PetscErrorCode ierr;
8645   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
8646   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
8647   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
8648 
8649   PetscFunctionBegin;
8650   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8651   PetscValidType(A,1);
8652   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8653   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8654 
8655   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8656   PetscValidType(B,2);
8657   MatCheckPreallocated(B,2);
8658   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8659   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8660   PetscValidPointer(C,3);
8661 
8662   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);
8663   if (fill == PETSC_DEFAULT) fill = 2.0;
8664   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8665   MatCheckPreallocated(A,1);
8666 
8667   Asymbolic = A->ops->matmultsymbolic;
8668   Bsymbolic = B->ops->matmultsymbolic;
8669   if (Asymbolic == Bsymbolic){
8670     if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
8671     symbolic = Bsymbolic;
8672   } else { /* dispatch based on the type of A and B */
8673     char  symbolicname[256];
8674     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
8675     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8676     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
8677     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8678     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
8679     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
8680     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);
8681   }
8682   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8683   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
8684   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8685   PetscFunctionReturn(0);
8686 }
8687 
8688 #undef __FUNCT__
8689 #define __FUNCT__ "MatMatMultNumeric"
8690 /*@
8691    MatMatMultNumeric - Performs the numeric matrix-matrix product.
8692    Call this routine after first calling MatMatMultSymbolic().
8693 
8694    Neighbor-wise Collective on Mat
8695 
8696    Input Parameters:
8697 +  A - the left matrix
8698 -  B - the right matrix
8699 
8700    Output Parameters:
8701 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
8702 
8703    Notes:
8704    C must have been created with MatMatMultSymbolic().
8705 
8706    This routine is currently implemented for
8707     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
8708     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8709     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8710 
8711    Level: intermediate
8712 
8713 .seealso: MatMatMult(), MatMatMultSymbolic()
8714 @*/
8715 PetscErrorCode  MatMatMultNumeric(Mat A,Mat B,Mat C)
8716 {
8717   PetscErrorCode ierr;
8718   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
8719   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
8720   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
8721 
8722   PetscFunctionBegin;
8723   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8724   PetscValidType(A,1);
8725   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8726   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8727 
8728   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8729   PetscValidType(B,2);
8730   MatCheckPreallocated(B,2);
8731   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8732   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8733 
8734   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8735   PetscValidType(C,3);
8736   MatCheckPreallocated(C,3);
8737   if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8738   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8739 
8740   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);
8741   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);
8742   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);
8743   MatCheckPreallocated(A,1);
8744 
8745   Anumeric = A->ops->matmultnumeric;
8746   Bnumeric = B->ops->matmultnumeric;
8747   if (Anumeric == Bnumeric){
8748     if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
8749     numeric = Bnumeric;
8750   } else {
8751     char  numericname[256];
8752     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
8753     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8754     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
8755     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8756     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
8757     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
8758     if (!numeric)
8759       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);
8760   }
8761   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8762   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
8763   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8764   PetscFunctionReturn(0);
8765 }
8766 
8767 #undef __FUNCT__
8768 #define __FUNCT__ "MatMatTransposeMult"
8769 /*@
8770    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.
8771 
8772    Neighbor-wise Collective on Mat
8773 
8774    Input Parameters:
8775 +  A - the left matrix
8776 .  B - the right matrix
8777 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8778 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8779 
8780    Output Parameters:
8781 .  C - the product matrix
8782 
8783    Notes:
8784    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8785 
8786    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8787 
8788   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8789    actually needed.
8790 
8791    This routine is currently only implemented for pairs of SeqAIJ matrices.  C will be of type MATSEQAIJ.
8792 
8793    Level: intermediate
8794 
8795 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
8796 @*/
8797 PetscErrorCode  MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8798 {
8799   PetscErrorCode ierr;
8800   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8801   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8802 
8803   PetscFunctionBegin;
8804   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8805   PetscValidType(A,1);
8806   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8807   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8808   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8809   PetscValidType(B,2);
8810   MatCheckPreallocated(B,2);
8811   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8812   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8813   PetscValidPointer(C,3);
8814   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);
8815   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8816   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8817   MatCheckPreallocated(A,1);
8818 
8819   fA = A->ops->mattransposemult;
8820   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
8821   fB = B->ops->mattransposemult;
8822   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
8823   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);
8824 
8825   if (scall == MAT_INITIAL_MATRIX){
8826     ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8827     ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr);
8828     ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8829   }
8830   ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8831   ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr);
8832   ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8833   PetscFunctionReturn(0);
8834 }
8835 
8836 #undef __FUNCT__
8837 #define __FUNCT__ "MatTransposeMatMult"
8838 /*@
8839    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.
8840 
8841    Neighbor-wise Collective on Mat
8842 
8843    Input Parameters:
8844 +  A - the left matrix
8845 .  B - the right matrix
8846 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8847 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8848 
8849    Output Parameters:
8850 .  C - the product matrix
8851 
8852    Notes:
8853    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8854 
8855    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8856 
8857   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8858    actually needed.
8859 
8860    This routine is currently only implemented for pairs of SeqAIJ matrices and pairs of SeqDense matrices and classes
8861    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.
8862 
8863    Level: intermediate
8864 
8865 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
8866 @*/
8867 PetscErrorCode  MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8868 {
8869   PetscErrorCode ierr;
8870   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8871   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8872   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*);
8873 
8874   PetscFunctionBegin;
8875   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8876   PetscValidType(A,1);
8877   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8878   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8879   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8880   PetscValidType(B,2);
8881   MatCheckPreallocated(B,2);
8882   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8883   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8884   PetscValidPointer(C,3);
8885   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);
8886   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8887   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8888   MatCheckPreallocated(A,1);
8889 
8890   fA = A->ops->transposematmult;
8891   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
8892   fB = B->ops->transposematmult;
8893   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8894   if (fB==fA) {
8895     transposematmult = fA;
8896   }
8897   else {
8898     /* dual dispatch using MatQueryOp */
8899     ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&transposematmult), "MatTansposeMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8900     if(!transposematmult)
8901       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);
8902   }
8903   ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8904   ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8905   ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8906   PetscFunctionReturn(0);
8907 }
8908 
8909 #undef __FUNCT__
8910 #define __FUNCT__ "MatGetRedundantMatrix"
8911 /*@C
8912    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
8913 
8914    Collective on Mat
8915 
8916    Input Parameters:
8917 +  mat - the matrix
8918 .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
8919 .  subcomm - MPI communicator split from the communicator where mat resides in
8920 .  mlocal_red - number of local rows of the redundant matrix
8921 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8922 
8923    Output Parameter:
8924 .  matredundant - redundant matrix
8925 
8926    Notes:
8927    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
8928    original matrix has not changed from that last call to MatGetRedundantMatrix().
8929 
8930    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
8931    calling it.
8932 
8933    Only MPIAIJ matrix is supported.
8934 
8935    Level: advanced
8936 
8937    Concepts: subcommunicator
8938    Concepts: duplicate matrix
8939 
8940 .seealso: MatDestroy()
8941 @*/
8942 PetscErrorCode  MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
8943 {
8944   PetscErrorCode ierr;
8945 
8946   PetscFunctionBegin;
8947   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8948   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
8949     PetscValidPointer(*matredundant,6);
8950     PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6);
8951   }
8952   if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8953   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8954   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8955   MatCheckPreallocated(mat,1);
8956 
8957   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8958   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
8959   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8960   PetscFunctionReturn(0);
8961 }
8962 
8963 #undef __FUNCT__
8964 #define __FUNCT__ "MatGetMultiProcBlock"
8965 /*@C
8966    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
8967    a given 'mat' object. Each submatrix can span multiple procs.
8968 
8969    Collective on Mat
8970 
8971    Input Parameters:
8972 +  mat - the matrix
8973 .  subcomm - the subcommunicator obtained by com_split(comm)
8974 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8975 
8976    Output Parameter:
8977 .  subMat - 'parallel submatrices each spans a given subcomm
8978 
8979   Notes:
8980   The submatrix partition across processors is dicated by 'subComm' a
8981   communicator obtained by com_split(comm). The comm_split
8982   is not restriced to be grouped with consequitive original ranks.
8983 
8984   Due the comm_split() usage, the parallel layout of the submatrices
8985   map directly to the layout of the original matrix [wrt the local
8986   row,col partitioning]. So the original 'DiagonalMat' naturally maps
8987   into the 'DiagonalMat' of the subMat, hence it is used directly from
8988   the subMat. However the offDiagMat looses some columns - and this is
8989   reconstructed with MatSetValues()
8990 
8991   Level: advanced
8992 
8993   Concepts: subcommunicator
8994   Concepts: submatrices
8995 
8996 .seealso: MatGetSubMatrices()
8997 @*/
8998 PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat* subMat)
8999 {
9000   PetscErrorCode ierr;
9001   PetscMPIInt    commsize,subCommSize;
9002 
9003   PetscFunctionBegin;
9004   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&commsize);CHKERRQ(ierr);
9005   ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr);
9006   if (subCommSize > commsize) SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);
9007 
9008   ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9009   ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr);
9010   ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9011   PetscFunctionReturn(0);
9012 }
9013 
9014 #undef __FUNCT__
9015 #define __FUNCT__ "MatGetLocalSubMatrix"
9016 /*@
9017    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering
9018 
9019    Not Collective
9020 
9021    Input Arguments:
9022    mat - matrix to extract local submatrix from
9023    isrow - local row indices for submatrix
9024    iscol - local column indices for submatrix
9025 
9026    Output Arguments:
9027    submat - the submatrix
9028 
9029    Level: intermediate
9030 
9031    Notes:
9032    The submat should be returned with MatRestoreLocalSubMatrix().
9033 
9034    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
9035    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.
9036 
9037    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
9038    MatSetValuesBlockedLocal() will also be implemented.
9039 
9040 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef()
9041 @*/
9042 PetscErrorCode  MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9043 {
9044   PetscErrorCode ierr;
9045 
9046   PetscFunctionBegin;
9047   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9048   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9049   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9050   PetscCheckSameComm(isrow,2,iscol,3);
9051   PetscValidPointer(submat,4);
9052 
9053   if (mat->ops->getlocalsubmatrix) {
9054     ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9055   } else {
9056     ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr);
9057   }
9058   PetscFunctionReturn(0);
9059 }
9060 
9061 #undef __FUNCT__
9062 #define __FUNCT__ "MatRestoreLocalSubMatrix"
9063 /*@
9064    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering
9065 
9066    Not Collective
9067 
9068    Input Arguments:
9069    mat - matrix to extract local submatrix from
9070    isrow - local row indices for submatrix
9071    iscol - local column indices for submatrix
9072    submat - the submatrix
9073 
9074    Level: intermediate
9075 
9076 .seealso: MatGetLocalSubMatrix()
9077 @*/
9078 PetscErrorCode  MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9079 {
9080   PetscErrorCode ierr;
9081 
9082   PetscFunctionBegin;
9083   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9084   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9085   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9086   PetscCheckSameComm(isrow,2,iscol,3);
9087   PetscValidPointer(submat,4);
9088   if (*submat) {PetscValidHeaderSpecific(*submat,MAT_CLASSID,4);}
9089 
9090   if (mat->ops->restorelocalsubmatrix) {
9091     ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9092   } else {
9093     ierr = MatDestroy(submat);CHKERRQ(ierr);
9094   }
9095   *submat = PETSC_NULL;
9096   PetscFunctionReturn(0);
9097 }
9098 
9099 /* --------------------------------------------------------*/
9100 #undef __FUNCT__
9101 #define __FUNCT__ "MatFindZeroDiagonals"
9102 /*@
9103    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no entry in the matrix
9104 
9105    Collective on Mat
9106 
9107    Input Parameter:
9108 .  mat - the matrix
9109 
9110    Output Parameter:
9111 .  is - if any rows have zero diagonals this contains the list of them
9112 
9113    Level: developer
9114 
9115    Concepts: matrix-vector product
9116 
9117 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9118 @*/
9119 PetscErrorCode  MatFindZeroDiagonals(Mat mat,IS *is)
9120 {
9121   PetscErrorCode ierr;
9122 
9123   PetscFunctionBegin;
9124   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9125   PetscValidType(mat,1);
9126   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9127   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9128 
9129   if (!mat->ops->findzerodiagonals) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"This matrix type does not have a find zero diagonals defined");
9130   ierr = (*mat->ops->findzerodiagonals)(mat,is);CHKERRQ(ierr);
9131   PetscFunctionReturn(0);
9132 }
9133 
9134 #undef __FUNCT__
9135 #define __FUNCT__ "MatInvertBlockDiagonal"
9136 /*@
9137   MatInvertBlockDiagonal - Inverts the block diagonal entries.
9138 
9139   Collective on Mat
9140 
9141   Input Parameters:
9142 . mat - the matrix
9143 
9144   Output Parameters:
9145 . values - the block inverses in column major order (FORTRAN-like)
9146 
9147   Level: advanced
9148 @*/
9149 PetscErrorCode  MatInvertBlockDiagonal(Mat mat,PetscScalar **values)
9150 {
9151   PetscErrorCode ierr;
9152 
9153   PetscFunctionBegin;
9154   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9155   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9156   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9157   if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
9158   ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr);
9159   PetscFunctionReturn(0);
9160 }
9161 
9162 #undef __FUNCT__
9163 #define __FUNCT__ "MatTransposeColoringDestroy"
9164 /*@C
9165     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
9166     via MatTransposeColoringCreate().
9167 
9168     Collective on MatTransposeColoring
9169 
9170     Input Parameter:
9171 .   c - coloring context
9172 
9173     Level: intermediate
9174 
9175 .seealso: MatTransposeColoringCreate()
9176 @*/
9177 PetscErrorCode  MatTransposeColoringDestroy(MatTransposeColoring *c)
9178 {
9179   PetscErrorCode       ierr;
9180   MatTransposeColoring matcolor=*c;
9181 
9182   PetscFunctionBegin;
9183   if (!matcolor) PetscFunctionReturn(0);
9184   if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);}
9185 
9186   ierr = PetscFree(matcolor->ncolumns);CHKERRQ(ierr);
9187   ierr = PetscFree(matcolor->nrows);CHKERRQ(ierr);
9188   ierr = PetscFree(matcolor->colorforrow);CHKERRQ(ierr);
9189   ierr = PetscFree2(matcolor->rows,matcolor->columnsforspidx);CHKERRQ(ierr);
9190   ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr);
9191   ierr = PetscFree(matcolor->columns);CHKERRQ(ierr);
9192   ierr = PetscHeaderDestroy(c);CHKERRQ(ierr);
9193   PetscFunctionReturn(0);
9194 }
9195 
9196 #undef __FUNCT__
9197 #define __FUNCT__ "MatTransColoringApplySpToDen"
9198 /*@C
9199     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
9200     a MatTransposeColoring context has been created, computes a dense B^T by Apply
9201     MatTransposeColoring to sparse B.
9202 
9203     Collective on MatTransposeColoring
9204 
9205     Input Parameters:
9206 +   B - sparse matrix B
9207 .   Btdense - symbolic dense matrix B^T
9208 -   coloring - coloring context created with MatTransposeColoringCreate()
9209 
9210     Output Parameter:
9211 .   Btdense - dense matrix B^T
9212 
9213     Options Database Keys:
9214 +    -mat_transpose_coloring_view - Activates basic viewing or coloring
9215 .    -mat_transpose_coloring_view_draw - Activates drawing of coloring
9216 -    -mat_transpose_coloring_view_info - Activates viewing of coloring info
9217 
9218     Level: intermediate
9219 
9220 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy()
9221 
9222 .keywords: coloring
9223 @*/
9224 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
9225 {
9226   PetscErrorCode ierr;
9227 
9228   PetscFunctionBegin;
9229   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
9230   PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2);
9231   PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3);
9232 
9233   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
9234   ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr);
9235   PetscFunctionReturn(0);
9236 }
9237 
9238 #undef __FUNCT__
9239 #define __FUNCT__ "MatTransColoringApplyDenToSp"
9240 /*@C
9241     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
9242     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
9243     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
9244     Csp from Cden.
9245 
9246     Collective on MatTransposeColoring
9247 
9248     Input Parameters:
9249 +   coloring - coloring context created with MatTransposeColoringCreate()
9250 -   Cden - matrix product of a sparse matrix and a dense matrix Btdense
9251 
9252     Output Parameter:
9253 .   Csp - sparse matrix
9254 
9255     Options Database Keys:
9256 +    -mat_multtranspose_coloring_view - Activates basic viewing or coloring
9257 .    -mat_multtranspose_coloring_view_draw - Activates drawing of coloring
9258 -    -mat_multtranspose_coloring_view_info - Activates viewing of coloring info
9259 
9260     Level: intermediate
9261 
9262 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()
9263 
9264 .keywords: coloring
9265 @*/
9266 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
9267 {
9268   PetscErrorCode ierr;
9269 
9270   PetscFunctionBegin;
9271   PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1);
9272   PetscValidHeaderSpecific(Cden,MAT_CLASSID,2);
9273   PetscValidHeaderSpecific(Csp,MAT_CLASSID,3);
9274 
9275   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
9276   ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr);
9277   PetscFunctionReturn(0);
9278 }
9279 
9280 #undef __FUNCT__
9281 #define __FUNCT__ "MatTransposeColoringCreate"
9282 /*@C
9283    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.
9284 
9285    Collective on Mat
9286 
9287    Input Parameters:
9288 +  mat - the matrix product C
9289 -  iscoloring - the coloring of the matrix; usually obtained with MatGetColoring() or DMCreateColoring()
9290 
9291     Output Parameter:
9292 .   color - the new coloring context
9293 
9294     Level: intermediate
9295 
9296 .seealso: MatTransposeColoringDestroy(), MatTransposeColoringSetFromOptions(), MatTransColoringApplySpToDen(),
9297            MatTransColoringApplyDen()ToSp, MatTransposeColoringView(),
9298 @*/
9299 PetscErrorCode  MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
9300 {
9301   MatTransposeColoring  c;
9302   MPI_Comm              comm;
9303   PetscErrorCode        ierr;
9304 
9305   PetscFunctionBegin;
9306   ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9307   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
9308   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);
9309 
9310   c->ctype = iscoloring->ctype;
9311   if (mat->ops->transposecoloringcreate) {
9312     ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr);
9313   } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Code not yet written for this matrix type");
9314 
9315   *color = c;
9316   ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9317   PetscFunctionReturn(0);
9318 }
9319