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