xref: /petsc/src/mat/interface/matrix.c (revision 9a7c2b27c6cab4cdd1fe47297fcd76510c370c62)
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 /*@
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) PetscValidScalarPointer(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     if (mat->nullsp) {
4906       ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test",&flg,PETSC_NULL);CHKERRQ(ierr);
4907       if (flg) {
4908         ierr = MatNullSpaceTest(mat->nullsp,mat,PETSC_NULL);CHKERRQ(ierr);
4909       }
4910     }
4911   }
4912   inassm--;
4913   PetscFunctionReturn(0);
4914 }
4915 
4916 #undef __FUNCT__
4917 #define __FUNCT__ "MatSetOption"
4918 /*@
4919    MatSetOption - Sets a parameter option for a matrix. Some options
4920    may be specific to certain storage formats.  Some options
4921    determine how values will be inserted (or added). Sorted,
4922    row-oriented input will generally assemble the fastest. The default
4923    is row-oriented.
4924 
4925    Logically Collective on Mat
4926 
4927    Input Parameters:
4928 +  mat - the matrix
4929 .  option - the option, one of those listed below (and possibly others),
4930 -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)
4931 
4932   Options Describing Matrix Structure:
4933 +    MAT_SPD - symmetric positive definite
4934 -    MAT_SYMMETRIC - symmetric in terms of both structure and value
4935 .    MAT_HERMITIAN - transpose is the complex conjugation
4936 .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
4937 -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
4938                             you set to be kept with all future use of the matrix
4939                             including after MatAssemblyBegin/End() which could
4940                             potentially change the symmetry structure, i.e. you
4941                             KNOW the matrix will ALWAYS have the property you set.
4942 
4943 
4944    Options For Use with MatSetValues():
4945    Insert a logically dense subblock, which can be
4946 .    MAT_ROW_ORIENTED - row-oriented (default)
4947 
4948    Note these options reflect the data you pass in with MatSetValues(); it has
4949    nothing to do with how the data is stored internally in the matrix
4950    data structure.
4951 
4952    When (re)assembling a matrix, we can restrict the input for
4953    efficiency/debugging purposes.  These options include
4954 +    MAT_NEW_NONZERO_LOCATIONS - additional insertions will be
4955         allowed if they generate a new nonzero
4956 .    MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
4957 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
4958 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
4959 .    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
4960 +    MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if
4961         any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves
4962         performance for very large process counts.
4963 
4964    Notes:
4965    Some options are relevant only for particular matrix types and
4966    are thus ignored by others.  Other options are not supported by
4967    certain matrix types and will generate an error message if set.
4968 
4969    If using a Fortran 77 module to compute a matrix, one may need to
4970    use the column-oriented option (or convert to the row-oriented
4971    format).
4972 
4973    MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion
4974    that would generate a new entry in the nonzero structure is instead
4975    ignored.  Thus, if memory has not alredy been allocated for this particular
4976    data, then the insertion is ignored. For dense matrices, in which
4977    the entire array is allocated, no entries are ever ignored.
4978    Set after the first MatAssemblyEnd()
4979 
4980    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
4981    that would generate a new entry in the nonzero structure instead produces
4982    an error. (Currently supported for AIJ and BAIJ formats only.)
4983    This is a useful flag when using SAME_NONZERO_PATTERN in calling
4984    KSPSetOperators() to ensure that the nonzero pattern truely does
4985    remain unchanged. Set after the first MatAssemblyEnd()
4986 
4987    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
4988    that would generate a new entry that has not been preallocated will
4989    instead produce an error. (Currently supported for AIJ and BAIJ formats
4990    only.) This is a useful flag when debugging matrix memory preallocation.
4991 
4992    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
4993    other processors should be dropped, rather than stashed.
4994    This is useful if you know that the "owning" processor is also
4995    always generating the correct matrix entries, so that PETSc need
4996    not transfer duplicate entries generated on another processor.
4997 
4998    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
4999    searches during matrix assembly. When this flag is set, the hash table
5000    is created during the first Matrix Assembly. This hash table is
5001    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5002    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5003    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5004    supported by MATMPIBAIJ format only.
5005 
5006    MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries
5007    are kept in the nonzero structure
5008 
5009    MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating
5010    a zero location in the matrix
5011 
5012    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
5013    ROWBS matrix types
5014 
5015    MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the
5016         zero row routines and thus improves performance for very large process counts.
5017 
5018    MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular
5019         part of the matrix (since they should match the upper triangular part).
5020 
5021    Notes: Can only be called after MatSetSizes() and MatSetType() have been set.
5022 
5023    Level: intermediate
5024 
5025    Concepts: matrices^setting options
5026 
5027 @*/
5028 PetscErrorCode  MatSetOption(Mat mat,MatOption op,PetscBool  flg)
5029 {
5030   PetscErrorCode ierr;
5031 
5032   PetscFunctionBegin;
5033   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5034   PetscValidType(mat,1);
5035   PetscValidLogicalCollectiveEnum(mat,op,2);
5036   PetscValidLogicalCollectiveBool(mat,flg,3);
5037 
5038   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);
5039   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()");
5040 
5041   switch (op) {
5042   case MAT_NO_OFF_PROC_ENTRIES:
5043     mat->nooffprocentries                = flg;
5044     PetscFunctionReturn(0);
5045     break;
5046   case MAT_NO_OFF_PROC_ZERO_ROWS:
5047     mat->nooffproczerorows               = flg;
5048     PetscFunctionReturn(0);
5049     break;
5050   case MAT_SPD:
5051     mat->spd_set                         = PETSC_TRUE;
5052     mat->spd                             = flg;
5053     if (flg) {
5054       mat->symmetric                     = PETSC_TRUE;
5055       mat->structurally_symmetric        = PETSC_TRUE;
5056       mat->symmetric_set                 = PETSC_TRUE;
5057       mat->structurally_symmetric_set    = PETSC_TRUE;
5058     }
5059     break;
5060   case MAT_SYMMETRIC:
5061     mat->symmetric                       = flg;
5062     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5063     mat->symmetric_set                   = PETSC_TRUE;
5064     mat->structurally_symmetric_set      = flg;
5065     break;
5066   case MAT_HERMITIAN:
5067     mat->hermitian                       = flg;
5068     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5069     mat->hermitian_set                   = PETSC_TRUE;
5070     mat->structurally_symmetric_set      = flg;
5071     break;
5072   case MAT_STRUCTURALLY_SYMMETRIC:
5073     mat->structurally_symmetric          = flg;
5074     mat->structurally_symmetric_set      = PETSC_TRUE;
5075     break;
5076   case MAT_SYMMETRY_ETERNAL:
5077     mat->symmetric_eternal               = flg;
5078     break;
5079   default:
5080     break;
5081   }
5082   if (mat->ops->setoption) {
5083     ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr);
5084   }
5085   PetscFunctionReturn(0);
5086 }
5087 
5088 #undef __FUNCT__
5089 #define __FUNCT__ "MatZeroEntries"
5090 /*@
5091    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5092    this routine retains the old nonzero structure.
5093 
5094    Logically Collective on Mat
5095 
5096    Input Parameters:
5097 .  mat - the matrix
5098 
5099    Level: intermediate
5100 
5101    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.
5102    See the Performance chapter of the users manual for information on preallocating matrices.
5103 
5104    Concepts: matrices^zeroing
5105 
5106 .seealso: MatZeroRows()
5107 @*/
5108 PetscErrorCode  MatZeroEntries(Mat mat)
5109 {
5110   PetscErrorCode ierr;
5111 
5112   PetscFunctionBegin;
5113   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5114   PetscValidType(mat,1);
5115   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5116   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");
5117   if (!mat->ops->zeroentries) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5118   MatCheckPreallocated(mat,1);
5119 
5120   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
5121   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
5122   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
5123   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5124 #if defined(PETSC_HAVE_CUSP)
5125   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5126     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5127   }
5128 #endif
5129   PetscFunctionReturn(0);
5130 }
5131 
5132 #undef __FUNCT__
5133 #define __FUNCT__ "MatZeroRowsColumns"
5134 /*@C
5135    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
5136    of a set of rows and columns of a matrix.
5137 
5138    Collective on Mat
5139 
5140    Input Parameters:
5141 +  mat - the matrix
5142 .  numRows - the number of rows to remove
5143 .  rows - the global row indices
5144 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5145 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5146 -  b - optional vector of right hand side, that will be adjusted by provided solution
5147 
5148    Notes:
5149    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.
5150 
5151    The user can set a value in the diagonal entry (or for the AIJ and
5152    row formats can optionally remove the main diagonal entry from the
5153    nonzero structure as well, by passing 0.0 as the final argument).
5154 
5155    For the parallel case, all processes that share the matrix (i.e.,
5156    those in the communicator used for matrix creation) MUST call this
5157    routine, regardless of whether any rows being zeroed are owned by
5158    them.
5159 
5160    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5161    list only rows local to itself).
5162 
5163    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.
5164 
5165    Level: intermediate
5166 
5167    Concepts: matrices^zeroing rows
5168 
5169 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumnsIS()
5170 @*/
5171 PetscErrorCode  MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5172 {
5173   PetscErrorCode ierr;
5174 
5175   PetscFunctionBegin;
5176   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5177   PetscValidType(mat,1);
5178   if (numRows) PetscValidIntPointer(rows,3);
5179   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5180   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5181   if (!mat->ops->zerorowscolumns) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5182   MatCheckPreallocated(mat,1);
5183 
5184   ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5185   ierr = MatView_Private(mat);CHKERRQ(ierr);
5186   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5187 #if defined(PETSC_HAVE_CUSP)
5188   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5189     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5190   }
5191 #endif
5192   PetscFunctionReturn(0);
5193 }
5194 
5195 #undef __FUNCT__
5196 #define __FUNCT__ "MatZeroRowsColumnsIS"
5197 /*@C
5198    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
5199    of a set of rows and columns of a matrix.
5200 
5201    Collective on Mat
5202 
5203    Input Parameters:
5204 +  mat - the matrix
5205 .  is - the rows to zero
5206 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5207 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5208 -  b - optional vector of right hand side, that will be adjusted by provided solution
5209 
5210    Notes:
5211    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.
5212 
5213    The user can set a value in the diagonal entry (or for the AIJ and
5214    row formats can optionally remove the main diagonal entry from the
5215    nonzero structure as well, by passing 0.0 as the final argument).
5216 
5217    For the parallel case, all processes that share the matrix (i.e.,
5218    those in the communicator used for matrix creation) MUST call this
5219    routine, regardless of whether any rows being zeroed are owned by
5220    them.
5221 
5222    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5223    list only rows local to itself).
5224 
5225    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.
5226 
5227    Level: intermediate
5228 
5229    Concepts: matrices^zeroing rows
5230 
5231 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumns()
5232 @*/
5233 PetscErrorCode  MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5234 {
5235   PetscErrorCode ierr;
5236   PetscInt       numRows;
5237   const PetscInt *rows;
5238 
5239   PetscFunctionBegin;
5240   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5241   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5242   PetscValidType(mat,1);
5243   PetscValidType(is,2);
5244   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5245   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5246   ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5247   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5248   PetscFunctionReturn(0);
5249 }
5250 
5251 #undef __FUNCT__
5252 #define __FUNCT__ "MatZeroRows"
5253 /*@C
5254    MatZeroRows - Zeros all entries (except possibly the main diagonal)
5255    of a set of rows of a matrix.
5256 
5257    Collective on Mat
5258 
5259    Input Parameters:
5260 +  mat - the matrix
5261 .  numRows - the number of rows to remove
5262 .  rows - the global row indices
5263 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5264 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5265 -  b - optional vector of right hand side, that will be adjusted by provided solution
5266 
5267    Notes:
5268    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5269    but does not release memory.  For the dense and block diagonal
5270    formats this does not alter the nonzero structure.
5271 
5272    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5273    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5274    merely zeroed.
5275 
5276    The user can set a value in the diagonal entry (or for the AIJ and
5277    row formats can optionally remove the main diagonal entry from the
5278    nonzero structure as well, by passing 0.0 as the final argument).
5279 
5280    For the parallel case, all processes that share the matrix (i.e.,
5281    those in the communicator used for matrix creation) MUST call this
5282    routine, regardless of whether any rows being zeroed are owned by
5283    them.
5284 
5285    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5286    list only rows local to itself).
5287 
5288    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5289    owns that are to be zeroed. This saves a global synchronization in the implementation.
5290 
5291    Level: intermediate
5292 
5293    Concepts: matrices^zeroing rows
5294 
5295 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5296 @*/
5297 PetscErrorCode  MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5298 {
5299   PetscErrorCode ierr;
5300 
5301   PetscFunctionBegin;
5302   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5303   PetscValidType(mat,1);
5304   if (numRows) PetscValidIntPointer(rows,3);
5305   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5306   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5307   if (!mat->ops->zerorows) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5308   MatCheckPreallocated(mat,1);
5309 
5310   ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5311   ierr = MatView_Private(mat);CHKERRQ(ierr);
5312   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5313 #if defined(PETSC_HAVE_CUSP)
5314   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5315     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5316   }
5317 #endif
5318   PetscFunctionReturn(0);
5319 }
5320 
5321 #undef __FUNCT__
5322 #define __FUNCT__ "MatZeroRowsIS"
5323 /*@C
5324    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
5325    of a set of rows of a matrix.
5326 
5327    Collective on Mat
5328 
5329    Input Parameters:
5330 +  mat - the matrix
5331 .  is - index set of rows to remove
5332 .  diag - value put in all diagonals of eliminated rows
5333 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5334 -  b - optional vector of right hand side, that will be adjusted by provided solution
5335 
5336    Notes:
5337    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5338    but does not release memory.  For the dense and block diagonal
5339    formats this does not alter the nonzero structure.
5340 
5341    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5342    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5343    merely zeroed.
5344 
5345    The user can set a value in the diagonal entry (or for the AIJ and
5346    row formats can optionally remove the main diagonal entry from the
5347    nonzero structure as well, by passing 0.0 as the final argument).
5348 
5349    For the parallel case, all processes that share the matrix (i.e.,
5350    those in the communicator used for matrix creation) MUST call this
5351    routine, regardless of whether any rows being zeroed are owned by
5352    them.
5353 
5354    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5355    list only rows local to itself).
5356 
5357    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5358    owns that are to be zeroed. This saves a global synchronization in the implementation.
5359 
5360    Level: intermediate
5361 
5362    Concepts: matrices^zeroing rows
5363 
5364 .seealso: MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5365 @*/
5366 PetscErrorCode  MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5367 {
5368   PetscInt       numRows;
5369   const PetscInt *rows;
5370   PetscErrorCode ierr;
5371 
5372   PetscFunctionBegin;
5373   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5374   PetscValidType(mat,1);
5375   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5376   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5377   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5378   ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5379   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5380   PetscFunctionReturn(0);
5381 }
5382 
5383 #undef __FUNCT__
5384 #define __FUNCT__ "MatZeroRowsStencil"
5385 /*@C
5386    MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal)
5387    of a set of rows of a matrix. These rows must be local to the process.
5388 
5389    Collective on Mat
5390 
5391    Input Parameters:
5392 +  mat - the matrix
5393 .  numRows - the number of rows to remove
5394 .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5395 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5396 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5397 -  b - optional vector of right hand side, that will be adjusted by provided solution
5398 
5399    Notes:
5400    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5401    but does not release memory.  For the dense and block diagonal
5402    formats this does not alter the nonzero structure.
5403 
5404    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5405    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5406    merely zeroed.
5407 
5408    The user can set a value in the diagonal entry (or for the AIJ and
5409    row formats can optionally remove the main diagonal entry from the
5410    nonzero structure as well, by passing 0.0 as the final argument).
5411 
5412    For the parallel case, all processes that share the matrix (i.e.,
5413    those in the communicator used for matrix creation) MUST call this
5414    routine, regardless of whether any rows being zeroed are owned by
5415    them.
5416 
5417    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5418    list only rows local to itself).
5419 
5420    The grid coordinates are across the entire grid, not just the local portion
5421 
5422    In Fortran idxm and idxn should be declared as
5423 $     MatStencil idxm(4,m)
5424    and the values inserted using
5425 $    idxm(MatStencil_i,1) = i
5426 $    idxm(MatStencil_j,1) = j
5427 $    idxm(MatStencil_k,1) = k
5428 $    idxm(MatStencil_c,1) = c
5429    etc
5430 
5431    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5432    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5433    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5434    DMDA_BOUNDARY_PERIODIC boundary type.
5435 
5436    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
5437    a single value per point) you can skip filling those indices.
5438 
5439    Level: intermediate
5440 
5441    Concepts: matrices^zeroing rows
5442 
5443 .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5444 @*/
5445 PetscErrorCode  MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5446 {
5447   PetscInt       dim    = mat->stencil.dim;
5448   PetscInt       sdim   = dim - (1 - (PetscInt) mat->stencil.noc);
5449   PetscInt      *dims   = mat->stencil.dims+1;
5450   PetscInt      *starts = mat->stencil.starts;
5451   PetscInt      *dxm    = (PetscInt *) rows;
5452   PetscInt      *jdxm, i, j, tmp, numNewRows = 0;
5453   PetscErrorCode ierr;
5454 
5455   PetscFunctionBegin;
5456   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5457   PetscValidType(mat,1);
5458   if (numRows) PetscValidIntPointer(rows,3);
5459 
5460   ierr = PetscMalloc(numRows*sizeof(PetscInt), &jdxm);CHKERRQ(ierr);
5461   for(i = 0; i < numRows; ++i) {
5462     /* Skip unused dimensions (they are ordered k, j, i, c) */
5463     for(j = 0; j < 3-sdim; ++j) dxm++;
5464     /* Local index in X dir */
5465     tmp = *dxm++ - starts[0];
5466     /* Loop over remaining dimensions */
5467     for(j = 0; j < dim-1; ++j) {
5468       /* If nonlocal, set index to be negative */
5469       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5470       /* Update local index */
5471       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5472     }
5473     /* Skip component slot if necessary */
5474     if (mat->stencil.noc) dxm++;
5475     /* Local row number */
5476     if (tmp >= 0) {
5477       jdxm[numNewRows++] = tmp;
5478     }
5479   }
5480   ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr);
5481   ierr = PetscFree(jdxm);CHKERRQ(ierr);
5482   PetscFunctionReturn(0);
5483 }
5484 
5485 #undef __FUNCT__
5486 #define __FUNCT__ "MatZeroRowsColumnsStencil"
5487 /*@C
5488    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
5489    of a set of rows and columns of a matrix.
5490 
5491    Collective on Mat
5492 
5493    Input Parameters:
5494 +  mat - the matrix
5495 .  numRows - the number of rows/columns to remove
5496 .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5497 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5498 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5499 -  b - optional vector of right hand side, that will be adjusted by provided solution
5500 
5501    Notes:
5502    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5503    but does not release memory.  For the dense and block diagonal
5504    formats this does not alter the nonzero structure.
5505 
5506    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5507    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5508    merely zeroed.
5509 
5510    The user can set a value in the diagonal entry (or for the AIJ and
5511    row formats can optionally remove the main diagonal entry from the
5512    nonzero structure as well, by passing 0.0 as the final argument).
5513 
5514    For the parallel case, all processes that share the matrix (i.e.,
5515    those in the communicator used for matrix creation) MUST call this
5516    routine, regardless of whether any rows being zeroed are owned by
5517    them.
5518 
5519    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5520    list only rows local to itself, but the row/column numbers are given in local numbering).
5521 
5522    The grid coordinates are across the entire grid, not just the local portion
5523 
5524    In Fortran idxm and idxn should be declared as
5525 $     MatStencil idxm(4,m)
5526    and the values inserted using
5527 $    idxm(MatStencil_i,1) = i
5528 $    idxm(MatStencil_j,1) = j
5529 $    idxm(MatStencil_k,1) = k
5530 $    idxm(MatStencil_c,1) = c
5531    etc
5532 
5533    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5534    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5535    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5536    DMDA_BOUNDARY_PERIODIC boundary type.
5537 
5538    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
5539    a single value per point) you can skip filling those indices.
5540 
5541    Level: intermediate
5542 
5543    Concepts: matrices^zeroing rows
5544 
5545 .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5546 @*/
5547 PetscErrorCode  MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5548 {
5549   PetscInt       dim    = mat->stencil.dim;
5550   PetscInt       sdim   = dim - (1 - (PetscInt) mat->stencil.noc);
5551   PetscInt      *dims   = mat->stencil.dims+1;
5552   PetscInt      *starts = mat->stencil.starts;
5553   PetscInt      *dxm    = (PetscInt *) rows;
5554   PetscInt      *jdxm, i, j, tmp, numNewRows = 0;
5555   PetscErrorCode ierr;
5556 
5557   PetscFunctionBegin;
5558   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5559   PetscValidType(mat,1);
5560   if (numRows) PetscValidIntPointer(rows,3);
5561 
5562   ierr = PetscMalloc(numRows*sizeof(PetscInt), &jdxm);CHKERRQ(ierr);
5563   for(i = 0; i < numRows; ++i) {
5564     /* Skip unused dimensions (they are ordered k, j, i, c) */
5565     for(j = 0; j < 3-sdim; ++j) dxm++;
5566     /* Local index in X dir */
5567     tmp = *dxm++ - starts[0];
5568     /* Loop over remaining dimensions */
5569     for(j = 0; j < dim-1; ++j) {
5570       /* If nonlocal, set index to be negative */
5571       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5572       /* Update local index */
5573       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5574     }
5575     /* Skip component slot if necessary */
5576     if (mat->stencil.noc) dxm++;
5577     /* Local row number */
5578     if (tmp >= 0) {
5579       jdxm[numNewRows++] = tmp;
5580     }
5581   }
5582   ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr);
5583   ierr = PetscFree(jdxm);CHKERRQ(ierr);
5584   PetscFunctionReturn(0);
5585 }
5586 
5587 #undef __FUNCT__
5588 #define __FUNCT__ "MatZeroRowsLocal"
5589 /*@C
5590    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
5591    of a set of rows of a matrix; using local numbering of rows.
5592 
5593    Collective on Mat
5594 
5595    Input Parameters:
5596 +  mat - the matrix
5597 .  numRows - the number of rows to remove
5598 .  rows - the global row indices
5599 .  diag - value put in all diagonals of eliminated rows
5600 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5601 -  b - optional vector of right hand side, that will be adjusted by provided solution
5602 
5603    Notes:
5604    Before calling MatZeroRowsLocal(), the user must first set the
5605    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5606 
5607    For the AIJ matrix formats this removes the old nonzero structure,
5608    but does not release memory.  For the dense and block diagonal
5609    formats this does not alter the nonzero structure.
5610 
5611    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5612    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5613    merely zeroed.
5614 
5615    The user can set a value in the diagonal entry (or for the AIJ and
5616    row formats can optionally remove the main diagonal entry from the
5617    nonzero structure as well, by passing 0.0 as the final argument).
5618 
5619    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5620    owns that are to be zeroed. This saves a global synchronization in the implementation.
5621 
5622    Level: intermediate
5623 
5624    Concepts: matrices^zeroing
5625 
5626 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5627 @*/
5628 PetscErrorCode  MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5629 {
5630   PetscErrorCode ierr;
5631   PetscMPIInt    size;
5632 
5633   PetscFunctionBegin;
5634   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5635   PetscValidType(mat,1);
5636   if (numRows) PetscValidIntPointer(rows,3);
5637   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5638   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5639   MatCheckPreallocated(mat,1);
5640 
5641   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
5642   if (mat->ops->zerorowslocal) {
5643     ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5644   } else if (size == 1) {
5645     ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5646   } else {
5647     IS             is, newis;
5648     const PetscInt *newRows;
5649 
5650     if (!mat->rmap->mapping) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5651     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
5652     ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr);
5653     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
5654     ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr);
5655     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
5656     ierr = ISDestroy(&newis);CHKERRQ(ierr);
5657     ierr = ISDestroy(&is);CHKERRQ(ierr);
5658   }
5659   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5660 #if defined(PETSC_HAVE_CUSP)
5661   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5662     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5663   }
5664 #endif
5665   PetscFunctionReturn(0);
5666 }
5667 
5668 #undef __FUNCT__
5669 #define __FUNCT__ "MatZeroRowsLocalIS"
5670 /*@C
5671    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
5672    of a set of rows of a matrix; using local numbering of rows.
5673 
5674    Collective on Mat
5675 
5676    Input Parameters:
5677 +  mat - the matrix
5678 .  is - index set of rows to remove
5679 .  diag - value put in all diagonals of eliminated rows
5680 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5681 -  b - optional vector of right hand side, that will be adjusted by provided solution
5682 
5683    Notes:
5684    Before calling MatZeroRowsLocalIS(), the user must first set the
5685    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5686 
5687    For the AIJ matrix formats this removes the old nonzero structure,
5688    but does not release memory.  For the dense and block diagonal
5689    formats this does not alter the nonzero structure.
5690 
5691    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5692    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5693    merely zeroed.
5694 
5695    The user can set a value in the diagonal entry (or for the AIJ and
5696    row formats can optionally remove the main diagonal entry from the
5697    nonzero structure as well, by passing 0.0 as the final argument).
5698 
5699    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5700    owns that are to be zeroed. This saves a global synchronization in the implementation.
5701 
5702    Level: intermediate
5703 
5704    Concepts: matrices^zeroing
5705 
5706 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5707 @*/
5708 PetscErrorCode  MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5709 {
5710   PetscErrorCode ierr;
5711   PetscInt       numRows;
5712   const PetscInt *rows;
5713 
5714   PetscFunctionBegin;
5715   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5716   PetscValidType(mat,1);
5717   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5718   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5719   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5720   MatCheckPreallocated(mat,1);
5721 
5722   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5723   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5724   ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5725   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5726   PetscFunctionReturn(0);
5727 }
5728 
5729 #undef __FUNCT__
5730 #define __FUNCT__ "MatZeroRowsColumnsLocal"
5731 /*@C
5732    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
5733    of a set of rows and columns of a matrix; using local numbering of rows.
5734 
5735    Collective on Mat
5736 
5737    Input Parameters:
5738 +  mat - the matrix
5739 .  numRows - the number of rows to remove
5740 .  rows - the global row indices
5741 .  diag - value put in all diagonals of eliminated rows
5742 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5743 -  b - optional vector of right hand side, that will be adjusted by provided solution
5744 
5745    Notes:
5746    Before calling MatZeroRowsColumnsLocal(), the user must first set the
5747    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5748 
5749    The user can set a value in the diagonal entry (or for the AIJ and
5750    row formats can optionally remove the main diagonal entry from the
5751    nonzero structure as well, by passing 0.0 as the final argument).
5752 
5753    Level: intermediate
5754 
5755    Concepts: matrices^zeroing
5756 
5757 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5758 @*/
5759 PetscErrorCode  MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5760 {
5761   PetscErrorCode ierr;
5762   PetscMPIInt    size;
5763 
5764   PetscFunctionBegin;
5765   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5766   PetscValidType(mat,1);
5767   if (numRows) PetscValidIntPointer(rows,3);
5768   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5769   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5770   MatCheckPreallocated(mat,1);
5771 
5772   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
5773   if (size == 1) {
5774     ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5775   } else {
5776     IS             is, newis;
5777     const PetscInt *newRows;
5778 
5779     if (!mat->cmap->mapping) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5780     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
5781     ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr);
5782     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
5783     ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr);
5784     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
5785     ierr = ISDestroy(&newis);CHKERRQ(ierr);
5786     ierr = ISDestroy(&is);CHKERRQ(ierr);
5787   }
5788   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5789 #if defined(PETSC_HAVE_CUSP)
5790   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5791     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5792   }
5793 #endif
5794   PetscFunctionReturn(0);
5795 }
5796 
5797 #undef __FUNCT__
5798 #define __FUNCT__ "MatZeroRowsColumnsLocalIS"
5799 /*@C
5800    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
5801    of a set of rows and columns of a matrix; using local numbering of rows.
5802 
5803    Collective on Mat
5804 
5805    Input Parameters:
5806 +  mat - the matrix
5807 .  is - index set of rows to remove
5808 .  diag - value put in all diagonals of eliminated rows
5809 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5810 -  b - optional vector of right hand side, that will be adjusted by provided solution
5811 
5812    Notes:
5813    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
5814    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5815 
5816    The user can set a value in the diagonal entry (or for the AIJ and
5817    row formats can optionally remove the main diagonal entry from the
5818    nonzero structure as well, by passing 0.0 as the final argument).
5819 
5820    Level: intermediate
5821 
5822    Concepts: matrices^zeroing
5823 
5824 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5825 @*/
5826 PetscErrorCode  MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5827 {
5828   PetscErrorCode ierr;
5829   PetscInt       numRows;
5830   const PetscInt *rows;
5831 
5832   PetscFunctionBegin;
5833   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5834   PetscValidType(mat,1);
5835   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5836   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5837   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5838   MatCheckPreallocated(mat,1);
5839 
5840   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5841   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5842   ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5843   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5844   PetscFunctionReturn(0);
5845 }
5846 
5847 #undef __FUNCT__
5848 #define __FUNCT__ "MatGetSize"
5849 /*@
5850    MatGetSize - Returns the numbers of rows and columns in a matrix.
5851 
5852    Not Collective
5853 
5854    Input Parameter:
5855 .  mat - the matrix
5856 
5857    Output Parameters:
5858 +  m - the number of global rows
5859 -  n - the number of global columns
5860 
5861    Note: both output parameters can be PETSC_NULL on input.
5862 
5863    Level: beginner
5864 
5865    Concepts: matrices^size
5866 
5867 .seealso: MatGetLocalSize()
5868 @*/
5869 PetscErrorCode  MatGetSize(Mat mat,PetscInt *m,PetscInt* n)
5870 {
5871   PetscFunctionBegin;
5872   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5873   if (m) *m = mat->rmap->N;
5874   if (n) *n = mat->cmap->N;
5875   PetscFunctionReturn(0);
5876 }
5877 
5878 #undef __FUNCT__
5879 #define __FUNCT__ "MatGetLocalSize"
5880 /*@
5881    MatGetLocalSize - Returns the number of rows and columns in a matrix
5882    stored locally.  This information may be implementation dependent, so
5883    use with care.
5884 
5885    Not Collective
5886 
5887    Input Parameters:
5888 .  mat - the matrix
5889 
5890    Output Parameters:
5891 +  m - the number of local rows
5892 -  n - the number of local columns
5893 
5894    Note: both output parameters can be PETSC_NULL on input.
5895 
5896    Level: beginner
5897 
5898    Concepts: matrices^local size
5899 
5900 .seealso: MatGetSize()
5901 @*/
5902 PetscErrorCode  MatGetLocalSize(Mat mat,PetscInt *m,PetscInt* n)
5903 {
5904   PetscFunctionBegin;
5905   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5906   if (m) PetscValidIntPointer(m,2);
5907   if (n) PetscValidIntPointer(n,3);
5908   if (m) *m = mat->rmap->n;
5909   if (n) *n = mat->cmap->n;
5910   PetscFunctionReturn(0);
5911 }
5912 
5913 #undef __FUNCT__
5914 #define __FUNCT__ "MatGetOwnershipRangeColumn"
5915 /*@
5916    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
5917    this processor. (The columns of the "diagonal block")
5918 
5919    Not Collective, unless matrix has not been allocated, then collective on Mat
5920 
5921    Input Parameters:
5922 .  mat - the matrix
5923 
5924    Output Parameters:
5925 +  m - the global index of the first local column
5926 -  n - one more than the global index of the last local column
5927 
5928    Notes: both output parameters can be PETSC_NULL on input.
5929 
5930    Level: developer
5931 
5932    Concepts: matrices^column ownership
5933 
5934 .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()
5935 
5936 @*/
5937 PetscErrorCode  MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt* n)
5938 {
5939 
5940   PetscFunctionBegin;
5941   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5942   PetscValidType(mat,1);
5943   if (m) PetscValidIntPointer(m,2);
5944   if (n) PetscValidIntPointer(n,3);
5945   MatCheckPreallocated(mat,1);
5946   if (m) *m = mat->cmap->rstart;
5947   if (n) *n = mat->cmap->rend;
5948   PetscFunctionReturn(0);
5949 }
5950 
5951 #undef __FUNCT__
5952 #define __FUNCT__ "MatGetOwnershipRange"
5953 /*@
5954    MatGetOwnershipRange - Returns the range of matrix rows owned by
5955    this processor, assuming that the matrix is laid out with the first
5956    n1 rows on the first processor, the next n2 rows on the second, etc.
5957    For certain parallel layouts this range may not be well defined.
5958 
5959    Not Collective, unless matrix has not been allocated, then collective on Mat
5960 
5961    Input Parameters:
5962 .  mat - the matrix
5963 
5964    Output Parameters:
5965 +  m - the global index of the first local row
5966 -  n - one more than the global index of the last local row
5967 
5968    Note: both output parameters can be PETSC_NULL on input.
5969 
5970    Level: beginner
5971 
5972    Concepts: matrices^row ownership
5973 
5974 .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
5975 
5976 @*/
5977 PetscErrorCode  MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt* n)
5978 {
5979 
5980   PetscFunctionBegin;
5981   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5982   PetscValidType(mat,1);
5983   if (m) PetscValidIntPointer(m,2);
5984   if (n) PetscValidIntPointer(n,3);
5985   MatCheckPreallocated(mat,1);
5986   if (m) *m = mat->rmap->rstart;
5987   if (n) *n = mat->rmap->rend;
5988   PetscFunctionReturn(0);
5989 }
5990 
5991 #undef __FUNCT__
5992 #define __FUNCT__ "MatGetOwnershipRanges"
5993 /*@C
5994    MatGetOwnershipRanges - Returns the range of matrix rows owned by
5995    each process
5996 
5997    Not Collective, unless matrix has not been allocated, then collective on Mat
5998 
5999    Input Parameters:
6000 .  mat - the matrix
6001 
6002    Output Parameters:
6003 .  ranges - start of each processors portion plus one more then the total length at the end
6004 
6005    Level: beginner
6006 
6007    Concepts: matrices^row ownership
6008 
6009 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
6010 
6011 @*/
6012 PetscErrorCode  MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6013 {
6014   PetscErrorCode ierr;
6015 
6016   PetscFunctionBegin;
6017   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6018   PetscValidType(mat,1);
6019   MatCheckPreallocated(mat,1);
6020   ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr);
6021   PetscFunctionReturn(0);
6022 }
6023 
6024 #undef __FUNCT__
6025 #define __FUNCT__ "MatGetOwnershipRangesColumn"
6026 /*@C
6027    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6028    this processor. (The columns of the "diagonal blocks" for each process)
6029 
6030    Not Collective, unless matrix has not been allocated, then collective on Mat
6031 
6032    Input Parameters:
6033 .  mat - the matrix
6034 
6035    Output Parameters:
6036 .  ranges - start of each processors portion plus one more then the total length at the end
6037 
6038    Level: beginner
6039 
6040    Concepts: matrices^column ownership
6041 
6042 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()
6043 
6044 @*/
6045 PetscErrorCode  MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6046 {
6047   PetscErrorCode ierr;
6048 
6049   PetscFunctionBegin;
6050   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6051   PetscValidType(mat,1);
6052   MatCheckPreallocated(mat,1);
6053   ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr);
6054   PetscFunctionReturn(0);
6055 }
6056 
6057 #undef __FUNCT__
6058 #define __FUNCT__ "MatGetOwnershipIS"
6059 /*@C
6060    MatGetOwnershipIS - Get row and column ownership as index sets
6061 
6062    Not Collective
6063 
6064    Input Arguments:
6065 .  A - matrix of type Elemental
6066 
6067    Output Arguments:
6068 +  rows - rows in which this process owns elements
6069 .  cols - columns in which this process owns elements
6070 
6071    Level: intermediate
6072 
6073 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MatSetValues()
6074 @*/
6075 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6076 {
6077   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);
6078 
6079   PetscFunctionBegin;
6080   MatCheckPreallocated(A,1);
6081   ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",(PetscVoidStarFunction)&f);CHKERRQ(ierr);
6082   if (f) {
6083     ierr = (*f)(A,rows,cols);CHKERRQ(ierr);
6084   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6085     if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);}
6086     if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);}
6087   }
6088   PetscFunctionReturn(0);
6089 }
6090 
6091 #undef __FUNCT__
6092 #define __FUNCT__ "MatILUFactorSymbolic"
6093 /*@C
6094    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6095    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6096    to complete the factorization.
6097 
6098    Collective on Mat
6099 
6100    Input Parameters:
6101 +  mat - the matrix
6102 .  row - row permutation
6103 .  column - column permutation
6104 -  info - structure containing
6105 $      levels - number of levels of fill.
6106 $      expected fill - as ratio of original fill.
6107 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6108                 missing diagonal entries)
6109 
6110    Output Parameters:
6111 .  fact - new matrix that has been symbolically factored
6112 
6113    Notes:
6114    See the <a href="../../docs/manual.pdf">users manual</a>  for additional information about
6115    choosing the fill factor for better efficiency.
6116 
6117    Most users should employ the simplified KSP interface for linear solvers
6118    instead of working directly with matrix algebra routines such as this.
6119    See, e.g., KSPCreate().
6120 
6121    Level: developer
6122 
6123   Concepts: matrices^symbolic LU factorization
6124   Concepts: matrices^factorization
6125   Concepts: LU^symbolic factorization
6126 
6127 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6128           MatGetOrdering(), MatFactorInfo
6129 
6130     Developer Note: fortran interface is not autogenerated as the f90
6131     interface defintion cannot be generated correctly [due to MatFactorInfo]
6132 
6133 @*/
6134 PetscErrorCode  MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6135 {
6136   PetscErrorCode ierr;
6137 
6138   PetscFunctionBegin;
6139   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6140   PetscValidType(mat,1);
6141   PetscValidHeaderSpecific(row,IS_CLASSID,2);
6142   PetscValidHeaderSpecific(col,IS_CLASSID,3);
6143   PetscValidPointer(info,4);
6144   PetscValidPointer(fact,5);
6145   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6146   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
6147   if (!(fact)->ops->ilufactorsymbolic) {
6148     const MatSolverPackage spackage;
6149     ierr = MatFactorGetSolverPackage(fact,&spackage);CHKERRQ(ierr);
6150     SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage);
6151   }
6152   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6153   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6154   MatCheckPreallocated(mat,2);
6155 
6156   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
6157   ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr);
6158   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
6159   PetscFunctionReturn(0);
6160 }
6161 
6162 #undef __FUNCT__
6163 #define __FUNCT__ "MatICCFactorSymbolic"
6164 /*@C
6165    MatICCFactorSymbolic - Performs symbolic incomplete
6166    Cholesky factorization for a symmetric matrix.  Use
6167    MatCholeskyFactorNumeric() to complete the factorization.
6168 
6169    Collective on Mat
6170 
6171    Input Parameters:
6172 +  mat - the matrix
6173 .  perm - row and column permutation
6174 -  info - structure containing
6175 $      levels - number of levels of fill.
6176 $      expected fill - as ratio of original fill.
6177 
6178    Output Parameter:
6179 .  fact - the factored matrix
6180 
6181    Notes:
6182    Most users should employ the KSP interface for linear solvers
6183    instead of working directly with matrix algebra routines such as this.
6184    See, e.g., KSPCreate().
6185 
6186    Level: developer
6187 
6188   Concepts: matrices^symbolic incomplete Cholesky factorization
6189   Concepts: matrices^factorization
6190   Concepts: Cholsky^symbolic factorization
6191 
6192 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
6193 
6194     Developer Note: fortran interface is not autogenerated as the f90
6195     interface defintion cannot be generated correctly [due to MatFactorInfo]
6196 
6197 @*/
6198 PetscErrorCode  MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6199 {
6200   PetscErrorCode ierr;
6201 
6202   PetscFunctionBegin;
6203   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6204   PetscValidType(mat,1);
6205   PetscValidHeaderSpecific(perm,IS_CLASSID,2);
6206   PetscValidPointer(info,3);
6207   PetscValidPointer(fact,4);
6208   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6209   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6210   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
6211   if (!(fact)->ops->iccfactorsymbolic) {
6212     const MatSolverPackage spackage;
6213     ierr = MatFactorGetSolverPackage(fact,&spackage);CHKERRQ(ierr);
6214     SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage);
6215   }
6216   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6217   MatCheckPreallocated(mat,2);
6218 
6219   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
6220   ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr);
6221   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
6222   PetscFunctionReturn(0);
6223 }
6224 
6225 #undef __FUNCT__
6226 #define __FUNCT__ "MatGetArray"
6227 /*@C
6228    MatGetArray - Returns a pointer to the element values in the matrix.
6229    The result of this routine is dependent on the underlying matrix data
6230    structure, and may not even work for certain matrix types.  You MUST
6231    call MatRestoreArray() when you no longer need to access the array.
6232 
6233    Not Collective
6234 
6235    Input Parameter:
6236 .  mat - the matrix
6237 
6238    Output Parameter:
6239 .  v - the location of the values
6240 
6241 
6242    Fortran Note:
6243    This routine is used differently from Fortran, e.g.,
6244 .vb
6245         Mat         mat
6246         PetscScalar mat_array(1)
6247         PetscOffset i_mat
6248         PetscErrorCode ierr
6249         call MatGetArray(mat,mat_array,i_mat,ierr)
6250 
6251   C  Access first local entry in matrix; note that array is
6252   C  treated as one dimensional
6253         value = mat_array(i_mat + 1)
6254 
6255         [... other code ...]
6256         call MatRestoreArray(mat,mat_array,i_mat,ierr)
6257 .ve
6258 
6259    See the <a href="../../docs/manual.pdf#ch_fortran">Fortran chapter of the users manual</a> and
6260    src/mat/examples/tests for details.
6261 
6262    Level: advanced
6263 
6264    Concepts: matrices^access array
6265 
6266 .seealso: MatRestoreArray(), MatGetArrayF90(), MatGetRowIJ()
6267 @*/
6268 PetscErrorCode  MatGetArray(Mat mat,PetscScalar *v[])
6269 {
6270   PetscErrorCode ierr;
6271 
6272   PetscFunctionBegin;
6273   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6274   PetscValidType(mat,1);
6275   PetscValidPointer(v,2);
6276   if (!mat->ops->getarray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6277   MatCheckPreallocated(mat,1);
6278   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
6279   CHKMEMQ;
6280   PetscFunctionReturn(0);
6281 }
6282 
6283 #undef __FUNCT__
6284 #define __FUNCT__ "MatRestoreArray"
6285 /*@C
6286    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
6287 
6288    Not Collective
6289 
6290    Input Parameter:
6291 +  mat - the matrix
6292 -  v - the location of the values
6293 
6294    Fortran Note:
6295    This routine is used differently from Fortran, e.g.,
6296 .vb
6297         Mat         mat
6298         PetscScalar mat_array(1)
6299         PetscOffset i_mat
6300         PetscErrorCode ierr
6301         call MatGetArray(mat,mat_array,i_mat,ierr)
6302 
6303   C  Access first local entry in matrix; note that array is
6304   C  treated as one dimensional
6305         value = mat_array(i_mat + 1)
6306 
6307         [... other code ...]
6308         call MatRestoreArray(mat,mat_array,i_mat,ierr)
6309 .ve
6310 
6311    See the <a href="../../docs/manual.pdf#ch_fortran">Fortran chapter of the users manual</a>
6312    src/mat/examples/tests for details
6313 
6314    Level: advanced
6315 
6316 .seealso: MatGetArray(), MatRestoreArrayF90()
6317 @*/
6318 PetscErrorCode  MatRestoreArray(Mat mat,PetscScalar *v[])
6319 {
6320   PetscErrorCode ierr;
6321 
6322   PetscFunctionBegin;
6323   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6324   PetscValidType(mat,1);
6325   PetscValidPointer(v,2);
6326   CHKMEMQ;
6327   if (!mat->ops->restorearray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6328   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
6329   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6330 #if defined(PETSC_HAVE_CUSP)
6331   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
6332     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
6333   }
6334 #endif
6335   PetscFunctionReturn(0);
6336 }
6337 
6338 #undef __FUNCT__
6339 #define __FUNCT__ "MatGetSubMatrices"
6340 /*@C
6341    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
6342    points to an array of valid matrices, they may be reused to store the new
6343    submatrices.
6344 
6345    Collective on Mat
6346 
6347    Input Parameters:
6348 +  mat - the matrix
6349 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6350 .  irow, icol - index sets of rows and columns to extract (must be sorted)
6351 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6352 
6353    Output Parameter:
6354 .  submat - the array of submatrices
6355 
6356    Notes:
6357    MatGetSubMatrices() can extract ONLY sequential submatrices
6358    (from both sequential and parallel matrices). Use MatGetSubMatrix()
6359    to extract a parallel submatrix.
6360 
6361    Currently both row and column indices must be sorted to guarantee
6362    correctness with all matrix types.
6363 
6364    When extracting submatrices from a parallel matrix, each processor can
6365    form a different submatrix by setting the rows and columns of its
6366    individual index sets according to the local submatrix desired.
6367 
6368    When finished using the submatrices, the user should destroy
6369    them with MatDestroyMatrices().
6370 
6371    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6372    original matrix has not changed from that last call to MatGetSubMatrices().
6373 
6374    This routine creates the matrices in submat; you should NOT create them before
6375    calling it. It also allocates the array of matrix pointers submat.
6376 
6377    For BAIJ matrices the index sets must respect the block structure, that is if they
6378    request one row/column in a block, they must request all rows/columns that are in
6379    that block. For example, if the block size is 2 you cannot request just row 0 and
6380    column 0.
6381 
6382    Fortran Note:
6383    The Fortran interface is slightly different from that given below; it
6384    requires one to pass in  as submat a Mat (integer) array of size at least m.
6385 
6386    Level: advanced
6387 
6388    Concepts: matrices^accessing submatrices
6389    Concepts: submatrices
6390 
6391 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6392 @*/
6393 PetscErrorCode  MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6394 {
6395   PetscErrorCode ierr;
6396   PetscInt        i;
6397   PetscBool       eq;
6398 
6399   PetscFunctionBegin;
6400   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6401   PetscValidType(mat,1);
6402   if (n) {
6403     PetscValidPointer(irow,3);
6404     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6405     PetscValidPointer(icol,4);
6406     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6407   }
6408   PetscValidPointer(submat,6);
6409   if (n && scall == MAT_REUSE_MATRIX) {
6410     PetscValidPointer(*submat,6);
6411     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6412   }
6413   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6414   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6415   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6416   MatCheckPreallocated(mat,1);
6417 
6418   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6419   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6420   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6421   for (i=0; i<n; i++) {
6422     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6423       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6424       if (eq) {
6425 	if (mat->symmetric){
6426 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6427 	} else if (mat->hermitian) {
6428 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6429 	} else if (mat->structurally_symmetric) {
6430 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6431 	}
6432       }
6433     }
6434   }
6435   PetscFunctionReturn(0);
6436 }
6437 
6438 #undef __FUNCT__
6439 #define __FUNCT__ "MatGetSubMatricesParallel"
6440 PetscErrorCode  MatGetSubMatricesParallel(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6441 {
6442   PetscErrorCode ierr;
6443   PetscInt        i;
6444   PetscBool       eq;
6445 
6446   PetscFunctionBegin;
6447   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6448   PetscValidType(mat,1);
6449   if (n) {
6450     PetscValidPointer(irow,3);
6451     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6452     PetscValidPointer(icol,4);
6453     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6454   }
6455   PetscValidPointer(submat,6);
6456   if (n && scall == MAT_REUSE_MATRIX) {
6457     PetscValidPointer(*submat,6);
6458     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6459   }
6460   if (!mat->ops->getsubmatricesparallel) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6461   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6462   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6463   MatCheckPreallocated(mat,1);
6464 
6465   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6466   ierr = (*mat->ops->getsubmatricesparallel)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6467   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6468   for (i=0; i<n; i++) {
6469     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6470       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6471       if (eq) {
6472 	if (mat->symmetric){
6473 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6474 	} else if (mat->hermitian) {
6475 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6476 	} else if (mat->structurally_symmetric) {
6477 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6478 	}
6479       }
6480     }
6481   }
6482   PetscFunctionReturn(0);
6483 }
6484 
6485 #undef __FUNCT__
6486 #define __FUNCT__ "MatDestroyMatrices"
6487 /*@C
6488    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
6489 
6490    Collective on Mat
6491 
6492    Input Parameters:
6493 +  n - the number of local matrices
6494 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6495                        sequence of MatGetSubMatrices())
6496 
6497    Level: advanced
6498 
6499     Notes: Frees not only the matrices, but also the array that contains the matrices
6500            In Fortran will not free the array.
6501 
6502 .seealso: MatGetSubMatrices()
6503 @*/
6504 PetscErrorCode  MatDestroyMatrices(PetscInt n,Mat *mat[])
6505 {
6506   PetscErrorCode ierr;
6507   PetscInt       i;
6508 
6509   PetscFunctionBegin;
6510   if (!*mat) PetscFunctionReturn(0);
6511   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
6512   PetscValidPointer(mat,2);
6513   for (i=0; i<n; i++) {
6514     ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr);
6515   }
6516   /* memory is allocated even if n = 0 */
6517   ierr = PetscFree(*mat);CHKERRQ(ierr);
6518   *mat = PETSC_NULL;
6519   PetscFunctionReturn(0);
6520 }
6521 
6522 #undef __FUNCT__
6523 #define __FUNCT__ "MatGetSeqNonzeroStructure"
6524 /*@C
6525    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
6526 
6527    Collective on Mat
6528 
6529    Input Parameters:
6530 .  mat - the matrix
6531 
6532    Output Parameter:
6533 .  matstruct - the sequential matrix with the nonzero structure of mat
6534 
6535   Level: intermediate
6536 
6537 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
6538 @*/
6539 PetscErrorCode  MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
6540 {
6541   PetscErrorCode ierr;
6542 
6543   PetscFunctionBegin;
6544   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6545   PetscValidPointer(matstruct,2);
6546 
6547   PetscValidType(mat,1);
6548   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6549   MatCheckPreallocated(mat,1);
6550 
6551   if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
6552   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6553   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
6554   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6555   PetscFunctionReturn(0);
6556 }
6557 
6558 #undef __FUNCT__
6559 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
6560 /*@C
6561    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
6562 
6563    Collective on Mat
6564 
6565    Input Parameters:
6566 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6567                        sequence of MatGetSequentialNonzeroStructure())
6568 
6569    Level: advanced
6570 
6571     Notes: Frees not only the matrices, but also the array that contains the matrices
6572 
6573 .seealso: MatGetSeqNonzeroStructure()
6574 @*/
6575 PetscErrorCode  MatDestroySeqNonzeroStructure(Mat *mat)
6576 {
6577   PetscErrorCode ierr;
6578 
6579   PetscFunctionBegin;
6580   PetscValidPointer(mat,1);
6581   ierr = MatDestroy(mat);CHKERRQ(ierr);
6582   PetscFunctionReturn(0);
6583 }
6584 
6585 #undef __FUNCT__
6586 #define __FUNCT__ "MatIncreaseOverlap"
6587 /*@
6588    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6589    replaces the index sets by larger ones that represent submatrices with
6590    additional overlap.
6591 
6592    Collective on Mat
6593 
6594    Input Parameters:
6595 +  mat - the matrix
6596 .  n   - the number of index sets
6597 .  is  - the array of index sets (these index sets will changed during the call)
6598 -  ov  - the additional overlap requested
6599 
6600    Level: developer
6601 
6602    Concepts: overlap
6603    Concepts: ASM^computing overlap
6604 
6605 .seealso: MatGetSubMatrices()
6606 @*/
6607 PetscErrorCode  MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6608 {
6609   PetscErrorCode ierr;
6610 
6611   PetscFunctionBegin;
6612   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6613   PetscValidType(mat,1);
6614   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6615   if (n) {
6616     PetscValidPointer(is,3);
6617     PetscValidHeaderSpecific(*is,IS_CLASSID,3);
6618   }
6619   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6620   if (mat->factortype)     SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6621   MatCheckPreallocated(mat,1);
6622 
6623   if (!ov) PetscFunctionReturn(0);
6624   if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6625   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6626   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
6627   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6628   PetscFunctionReturn(0);
6629 }
6630 
6631 #undef __FUNCT__
6632 #define __FUNCT__ "MatGetBlockSize"
6633 /*@
6634    MatGetBlockSize - Returns the matrix block size; useful especially for the
6635    block row and block diagonal formats.
6636 
6637    Not Collective
6638 
6639    Input Parameter:
6640 .  mat - the matrix
6641 
6642    Output Parameter:
6643 .  bs - block size
6644 
6645    Notes:
6646    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6647 
6648    Level: intermediate
6649 
6650    Concepts: matrices^block size
6651 
6652 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
6653 @*/
6654 PetscErrorCode  MatGetBlockSize(Mat mat,PetscInt *bs)
6655 {
6656 
6657   PetscFunctionBegin;
6658   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6659   PetscValidType(mat,1);
6660   PetscValidIntPointer(bs,2);
6661   MatCheckPreallocated(mat,1);
6662   *bs = mat->rmap->bs;
6663   PetscFunctionReturn(0);
6664 }
6665 
6666 #undef __FUNCT__
6667 #define __FUNCT__ "MatGetBlockSizes"
6668 /*@
6669    MatGetBlockSizes - Returns the matrix block row and column sizes;
6670    useful especially for the block row and block diagonal formats.
6671 
6672    Not Collective
6673 
6674    Input Parameter:
6675 .  mat - the matrix
6676 
6677    Output Parameter:
6678 .  rbs - row block size
6679 .  cbs - coumn block size
6680 
6681    Notes:
6682    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6683 
6684    Level: intermediate
6685 
6686    Concepts: matrices^block size
6687 
6688 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6689 @*/
6690 PetscErrorCode  MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
6691 {
6692 
6693   PetscFunctionBegin;
6694   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6695   PetscValidType(mat,1);
6696   if(rbs) PetscValidIntPointer(rbs,2);
6697   if(cbs) PetscValidIntPointer(cbs,3);
6698   MatCheckPreallocated(mat,1);
6699   if(rbs) *rbs = mat->rmap->bs;
6700   if(cbs) *cbs = mat->cmap->bs;
6701   PetscFunctionReturn(0);
6702 }
6703 
6704 #undef __FUNCT__
6705 #define __FUNCT__ "MatSetBlockSize"
6706 /*@
6707    MatSetBlockSize - Sets the matrix block size.
6708 
6709    Logically Collective on Mat
6710 
6711    Input Parameters:
6712 +  mat - the matrix
6713 -  bs - block size
6714 
6715    Notes:
6716      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6717 
6718    Level: intermediate
6719 
6720    Concepts: matrices^block size
6721 
6722 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6723 @*/
6724 PetscErrorCode  MatSetBlockSize(Mat mat,PetscInt bs)
6725 {
6726   PetscErrorCode ierr;
6727 
6728   PetscFunctionBegin;
6729   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6730   PetscValidLogicalCollectiveInt(mat,bs,2);
6731   ierr = PetscLayoutSetBlockSize(mat->rmap,bs);CHKERRQ(ierr);
6732   ierr = PetscLayoutSetBlockSize(mat->cmap,bs);CHKERRQ(ierr);
6733   PetscFunctionReturn(0);
6734 }
6735 
6736 #undef __FUNCT__
6737 #define __FUNCT__ "MatSetBlockSizes"
6738 /*@
6739    MatSetBlockSizes - Sets the matrix block row and column sizes.
6740 
6741    Logically Collective on Mat
6742 
6743    Input Parameters:
6744 +  mat - the matrix
6745 -  rbs - row block size
6746 -  cbs - column block size
6747 
6748    Notes:
6749      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6750 
6751    Level: intermediate
6752 
6753    Concepts: matrices^block size
6754 
6755 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6756 @*/
6757 PetscErrorCode  MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
6758 {
6759   PetscErrorCode ierr;
6760 
6761   PetscFunctionBegin;
6762   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6763   PetscValidLogicalCollectiveInt(mat,rbs,2);
6764   ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr);
6765   ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr);
6766   PetscFunctionReturn(0);
6767 }
6768 
6769 #undef __FUNCT__
6770 #define __FUNCT__ "MatGetRowIJ"
6771 /*@C
6772     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
6773 
6774    Collective on Mat
6775 
6776     Input Parameters:
6777 +   mat - the matrix
6778 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
6779 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
6780 -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
6781                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6782                  always used.
6783 
6784     Output Parameters:
6785 +   n - number of rows in the (possibly compressed) matrix
6786 .   ia - the row pointers [of length n+1]
6787 .   ja - the column indices
6788 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
6789            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
6790 
6791     Level: developer
6792 
6793     Notes: You CANNOT change any of the ia[] or ja[] values.
6794 
6795            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
6796 
6797     Fortran Node
6798 
6799            In Fortran use
6800 $           PetscInt ia(1), ja(1)
6801 $           PetscOffset iia, jja
6802 $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
6803 $
6804 $          or
6805 $
6806 $           PetscScalar, pointer :: xx_v(:)
6807 $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
6808 
6809 
6810        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
6811 
6812 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray()
6813 @*/
6814 PetscErrorCode  MatGetRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6815 {
6816   PetscErrorCode ierr;
6817 
6818   PetscFunctionBegin;
6819   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6820   PetscValidType(mat,1);
6821   PetscValidIntPointer(n,4);
6822   if (ia) PetscValidIntPointer(ia,5);
6823   if (ja) PetscValidIntPointer(ja,6);
6824   PetscValidIntPointer(done,7);
6825   MatCheckPreallocated(mat,1);
6826   if (!mat->ops->getrowij) *done = PETSC_FALSE;
6827   else {
6828     *done = PETSC_TRUE;
6829     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6830     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6831     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6832   }
6833   PetscFunctionReturn(0);
6834 }
6835 
6836 #undef __FUNCT__
6837 #define __FUNCT__ "MatGetColumnIJ"
6838 /*@C
6839     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
6840 
6841     Collective on Mat
6842 
6843     Input Parameters:
6844 +   mat - the matrix
6845 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6846 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6847                 symmetrized
6848 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6849                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6850                  always used.
6851 
6852     Output Parameters:
6853 +   n - number of columns in the (possibly compressed) matrix
6854 .   ia - the column pointers
6855 .   ja - the row indices
6856 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
6857 
6858     Level: developer
6859 
6860 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6861 @*/
6862 PetscErrorCode  MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6863 {
6864   PetscErrorCode ierr;
6865 
6866   PetscFunctionBegin;
6867   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6868   PetscValidType(mat,1);
6869   PetscValidIntPointer(n,4);
6870   if (ia) PetscValidIntPointer(ia,5);
6871   if (ja) PetscValidIntPointer(ja,6);
6872   PetscValidIntPointer(done,7);
6873   MatCheckPreallocated(mat,1);
6874   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
6875   else {
6876     *done = PETSC_TRUE;
6877     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6878   }
6879   PetscFunctionReturn(0);
6880 }
6881 
6882 #undef __FUNCT__
6883 #define __FUNCT__ "MatRestoreRowIJ"
6884 /*@C
6885     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
6886     MatGetRowIJ().
6887 
6888     Collective on Mat
6889 
6890     Input Parameters:
6891 +   mat - the matrix
6892 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6893 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6894                 symmetrized
6895 -   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6896                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6897                  always used.
6898 
6899     Output Parameters:
6900 +   n - size of (possibly compressed) matrix
6901 .   ia - the row pointers
6902 .   ja - the column indices
6903 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6904 
6905     Level: developer
6906 
6907 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6908 @*/
6909 PetscErrorCode  MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6910 {
6911   PetscErrorCode ierr;
6912 
6913   PetscFunctionBegin;
6914   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6915   PetscValidType(mat,1);
6916   if (ia) PetscValidIntPointer(ia,5);
6917   if (ja) PetscValidIntPointer(ja,6);
6918   PetscValidIntPointer(done,7);
6919   MatCheckPreallocated(mat,1);
6920 
6921   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
6922   else {
6923     *done = PETSC_TRUE;
6924     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6925   }
6926   PetscFunctionReturn(0);
6927 }
6928 
6929 #undef __FUNCT__
6930 #define __FUNCT__ "MatRestoreColumnIJ"
6931 /*@C
6932     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
6933     MatGetColumnIJ().
6934 
6935     Collective on Mat
6936 
6937     Input Parameters:
6938 +   mat - the matrix
6939 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6940 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6941                 symmetrized
6942 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6943                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6944                  always used.
6945 
6946     Output Parameters:
6947 +   n - size of (possibly compressed) matrix
6948 .   ia - the column pointers
6949 .   ja - the row indices
6950 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6951 
6952     Level: developer
6953 
6954 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
6955 @*/
6956 PetscErrorCode  MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool  *done)
6957 {
6958   PetscErrorCode ierr;
6959 
6960   PetscFunctionBegin;
6961   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6962   PetscValidType(mat,1);
6963   if (ia) PetscValidIntPointer(ia,5);
6964   if (ja) PetscValidIntPointer(ja,6);
6965   PetscValidIntPointer(done,7);
6966   MatCheckPreallocated(mat,1);
6967 
6968   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
6969   else {
6970     *done = PETSC_TRUE;
6971     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6972   }
6973   PetscFunctionReturn(0);
6974 }
6975 
6976 #undef __FUNCT__
6977 #define __FUNCT__ "MatColoringPatch"
6978 /*@C
6979     MatColoringPatch -Used inside matrix coloring routines that
6980     use MatGetRowIJ() and/or MatGetColumnIJ().
6981 
6982     Collective on Mat
6983 
6984     Input Parameters:
6985 +   mat - the matrix
6986 .   ncolors - max color value
6987 .   n   - number of entries in colorarray
6988 -   colorarray - array indicating color for each column
6989 
6990     Output Parameters:
6991 .   iscoloring - coloring generated using colorarray information
6992 
6993     Level: developer
6994 
6995 .seealso: MatGetRowIJ(), MatGetColumnIJ()
6996 
6997 @*/
6998 PetscErrorCode  MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
6999 {
7000   PetscErrorCode ierr;
7001 
7002   PetscFunctionBegin;
7003   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7004   PetscValidType(mat,1);
7005   PetscValidIntPointer(colorarray,4);
7006   PetscValidPointer(iscoloring,5);
7007   MatCheckPreallocated(mat,1);
7008 
7009   if (!mat->ops->coloringpatch){
7010     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
7011   } else {
7012     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
7013   }
7014   PetscFunctionReturn(0);
7015 }
7016 
7017 
7018 #undef __FUNCT__
7019 #define __FUNCT__ "MatSetUnfactored"
7020 /*@
7021    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
7022 
7023    Logically Collective on Mat
7024 
7025    Input Parameter:
7026 .  mat - the factored matrix to be reset
7027 
7028    Notes:
7029    This routine should be used only with factored matrices formed by in-place
7030    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
7031    format).  This option can save memory, for example, when solving nonlinear
7032    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
7033    ILU(0) preconditioner.
7034 
7035    Note that one can specify in-place ILU(0) factorization by calling
7036 .vb
7037      PCType(pc,PCILU);
7038      PCFactorSeUseInPlace(pc);
7039 .ve
7040    or by using the options -pc_type ilu -pc_factor_in_place
7041 
7042    In-place factorization ILU(0) can also be used as a local
7043    solver for the blocks within the block Jacobi or additive Schwarz
7044    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
7045    of these preconditioners in the <a href="../../docs/manual.pdf#ch_pc">PC chapter of the users manual</a> for details on setting
7046    local solver options.
7047 
7048    Most users should employ the simplified KSP interface for linear solvers
7049    instead of working directly with matrix algebra routines such as this.
7050    See, e.g., KSPCreate().
7051 
7052    Level: developer
7053 
7054 .seealso: PCFactorSetUseInPlace()
7055 
7056    Concepts: matrices^unfactored
7057 
7058 @*/
7059 PetscErrorCode  MatSetUnfactored(Mat mat)
7060 {
7061   PetscErrorCode ierr;
7062 
7063   PetscFunctionBegin;
7064   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7065   PetscValidType(mat,1);
7066   MatCheckPreallocated(mat,1);
7067   mat->factortype = MAT_FACTOR_NONE;
7068   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
7069   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
7070   PetscFunctionReturn(0);
7071 }
7072 
7073 /*MC
7074     MatGetArrayF90 - Accesses a matrix array from Fortran90.
7075 
7076     Synopsis:
7077     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)
7078 
7079     Not collective
7080 
7081     Input Parameter:
7082 .   x - matrix
7083 
7084     Output Parameters:
7085 +   xx_v - the Fortran90 pointer to the array
7086 -   ierr - error code
7087 
7088     Example of Usage:
7089 .vb
7090       PetscScalar, pointer xx_v(:,:)
7091       ....
7092       call MatGetArrayF90(x,xx_v,ierr)
7093       a = xx_v(3)
7094       call MatRestoreArrayF90(x,xx_v,ierr)
7095 .ve
7096 
7097     Notes:
7098     Not yet supported for all F90 compilers
7099 
7100     Level: advanced
7101 
7102 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
7103 
7104     Concepts: matrices^accessing array
7105 
7106 M*/
7107 
7108 /*MC
7109     MatRestoreArrayF90 - Restores a matrix array that has been
7110     accessed with MatGetArrayF90().
7111 
7112     Synopsis:
7113     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
7114 
7115     Not collective
7116 
7117     Input Parameters:
7118 +   x - matrix
7119 -   xx_v - the Fortran90 pointer to the array
7120 
7121     Output Parameter:
7122 .   ierr - error code
7123 
7124     Example of Usage:
7125 .vb
7126        PetscScalar, pointer xx_v(:)
7127        ....
7128        call MatGetArrayF90(x,xx_v,ierr)
7129        a = xx_v(3)
7130        call MatRestoreArrayF90(x,xx_v,ierr)
7131 .ve
7132 
7133     Notes:
7134     Not yet supported for all F90 compilers
7135 
7136     Level: advanced
7137 
7138 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
7139 
7140 M*/
7141 
7142 
7143 #undef __FUNCT__
7144 #define __FUNCT__ "MatGetSubMatrix"
7145 /*@
7146     MatGetSubMatrix - Gets a single submatrix on the same number of processors
7147                       as the original matrix.
7148 
7149     Collective on Mat
7150 
7151     Input Parameters:
7152 +   mat - the original matrix
7153 .   isrow - parallel IS containing the rows this processor should obtain
7154 .   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.
7155 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7156 
7157     Output Parameter:
7158 .   newmat - the new submatrix, of the same type as the old
7159 
7160     Level: advanced
7161 
7162     Notes:
7163     The submatrix will be able to be multiplied with vectors using the same layout as iscol.
7164 
7165     The rows in isrow will be sorted into the same order as the original matrix on each process.
7166 
7167       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7168    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
7169    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7170    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7171    you are finished using it.
7172 
7173     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7174     the input matrix.
7175 
7176     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran).
7177 
7178    Example usage:
7179    Consider the following 8x8 matrix with 34 non-zero values, that is
7180    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7181    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7182    as follows:
7183 
7184 .vb
7185             1  2  0  |  0  3  0  |  0  4
7186     Proc0   0  5  6  |  7  0  0  |  8  0
7187             9  0 10  | 11  0  0  | 12  0
7188     -------------------------------------
7189            13  0 14  | 15 16 17  |  0  0
7190     Proc1   0 18  0  | 19 20 21  |  0  0
7191             0  0  0  | 22 23  0  | 24  0
7192     -------------------------------------
7193     Proc2  25 26 27  |  0  0 28  | 29  0
7194            30  0  0  | 31 32 33  |  0 34
7195 .ve
7196 
7197     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is
7198 
7199 .vb
7200             2  0  |  0  3  0  |  0
7201     Proc0   5  6  |  7  0  0  |  8
7202     -------------------------------
7203     Proc1  18  0  | 19 20 21  |  0
7204     -------------------------------
7205     Proc2  26 27  |  0  0 28  | 29
7206             0  0  | 31 32 33  |  0
7207 .ve
7208 
7209 
7210     Concepts: matrices^submatrices
7211 
7212 .seealso: MatGetSubMatrices()
7213 @*/
7214 PetscErrorCode  MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7215 {
7216   PetscErrorCode ierr;
7217   PetscMPIInt    size;
7218   Mat            *local;
7219   IS             iscoltmp;
7220 
7221   PetscFunctionBegin;
7222   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7223   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
7224   if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
7225   PetscValidPointer(newmat,5);
7226   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5);
7227   PetscValidType(mat,1);
7228   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7229   MatCheckPreallocated(mat,1);
7230   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7231 
7232   if (!iscol) {
7233     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr);
7234   } else {
7235     iscoltmp = iscol;
7236   }
7237 
7238   /* if original matrix is on just one processor then use submatrix generated */
7239   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7240     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
7241     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7242     PetscFunctionReturn(0);
7243   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
7244     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
7245     *newmat = *local;
7246     ierr    = PetscFree(local);CHKERRQ(ierr);
7247     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7248     PetscFunctionReturn(0);
7249   } else if (!mat->ops->getsubmatrix) {
7250     /* Create a new matrix type that implements the operation using the full matrix */
7251     switch (cll) {
7252       case MAT_INITIAL_MATRIX:
7253         ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr);
7254         break;
7255       case MAT_REUSE_MATRIX:
7256         ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr);
7257         break;
7258       default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
7259     }
7260     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7261     PetscFunctionReturn(0);
7262   }
7263 
7264   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7265   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr);
7266   if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7267   if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);}
7268   PetscFunctionReturn(0);
7269 }
7270 
7271 #undef __FUNCT__
7272 #define __FUNCT__ "MatStashSetInitialSize"
7273 /*@
7274    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
7275    used during the assembly process to store values that belong to
7276    other processors.
7277 
7278    Not Collective
7279 
7280    Input Parameters:
7281 +  mat   - the matrix
7282 .  size  - the initial size of the stash.
7283 -  bsize - the initial size of the block-stash(if used).
7284 
7285    Options Database Keys:
7286 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
7287 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
7288 
7289    Level: intermediate
7290 
7291    Notes:
7292      The block-stash is used for values set with MatSetValuesBlocked() while
7293      the stash is used for values set with MatSetValues()
7294 
7295      Run with the option -info and look for output of the form
7296      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
7297      to determine the appropriate value, MM, to use for size and
7298      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
7299      to determine the value, BMM to use for bsize
7300 
7301    Concepts: stash^setting matrix size
7302    Concepts: matrices^stash
7303 
7304 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()
7305 
7306 @*/
7307 PetscErrorCode  MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
7308 {
7309   PetscErrorCode ierr;
7310 
7311   PetscFunctionBegin;
7312   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7313   PetscValidType(mat,1);
7314   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
7315   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
7316   PetscFunctionReturn(0);
7317 }
7318 
7319 #undef __FUNCT__
7320 #define __FUNCT__ "MatInterpolateAdd"
7321 /*@
7322    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
7323      the matrix
7324 
7325    Neighbor-wise Collective on Mat
7326 
7327    Input Parameters:
7328 +  mat   - the matrix
7329 .  x,y - the vectors
7330 -  w - where the result is stored
7331 
7332    Level: intermediate
7333 
7334    Notes:
7335     w may be the same vector as y.
7336 
7337     This allows one to use either the restriction or interpolation (its transpose)
7338     matrix to do the interpolation
7339 
7340     Concepts: interpolation
7341 
7342 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7343 
7344 @*/
7345 PetscErrorCode  MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
7346 {
7347   PetscErrorCode ierr;
7348   PetscInt       M,N,Ny;
7349 
7350   PetscFunctionBegin;
7351   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7352   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7353   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7354   PetscValidHeaderSpecific(w,VEC_CLASSID,4);
7355   PetscValidType(A,1);
7356   MatCheckPreallocated(A,1);
7357   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7358   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7359   if (M == Ny) {
7360     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
7361   } else {
7362     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
7363   }
7364   PetscFunctionReturn(0);
7365 }
7366 
7367 #undef __FUNCT__
7368 #define __FUNCT__ "MatInterpolate"
7369 /*@
7370    MatInterpolate - y = A*x or A'*x depending on the shape of
7371      the matrix
7372 
7373    Neighbor-wise Collective on Mat
7374 
7375    Input Parameters:
7376 +  mat   - the matrix
7377 -  x,y - the vectors
7378 
7379    Level: intermediate
7380 
7381    Notes:
7382     This allows one to use either the restriction or interpolation (its transpose)
7383     matrix to do the interpolation
7384 
7385    Concepts: matrices^interpolation
7386 
7387 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7388 
7389 @*/
7390 PetscErrorCode  MatInterpolate(Mat A,Vec x,Vec y)
7391 {
7392   PetscErrorCode ierr;
7393   PetscInt       M,N,Ny;
7394 
7395   PetscFunctionBegin;
7396   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7397   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7398   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7399   PetscValidType(A,1);
7400   MatCheckPreallocated(A,1);
7401   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7402   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7403   if (M == Ny) {
7404     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7405   } else {
7406     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7407   }
7408   PetscFunctionReturn(0);
7409 }
7410 
7411 #undef __FUNCT__
7412 #define __FUNCT__ "MatRestrict"
7413 /*@
7414    MatRestrict - y = A*x or A'*x
7415 
7416    Neighbor-wise Collective on Mat
7417 
7418    Input Parameters:
7419 +  mat   - the matrix
7420 -  x,y - the vectors
7421 
7422    Level: intermediate
7423 
7424    Notes:
7425     This allows one to use either the restriction or interpolation (its transpose)
7426     matrix to do the restriction
7427 
7428    Concepts: matrices^restriction
7429 
7430 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
7431 
7432 @*/
7433 PetscErrorCode  MatRestrict(Mat A,Vec x,Vec y)
7434 {
7435   PetscErrorCode ierr;
7436   PetscInt       M,N,Ny;
7437 
7438   PetscFunctionBegin;
7439   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7440   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7441   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7442   PetscValidType(A,1);
7443   MatCheckPreallocated(A,1);
7444 
7445   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7446   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7447   if (M == Ny) {
7448     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7449   } else {
7450     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7451   }
7452   PetscFunctionReturn(0);
7453 }
7454 
7455 #undef __FUNCT__
7456 #define __FUNCT__ "MatGetNullSpace"
7457 /*@
7458    MatGetNullSpace - retrieves the null space to a matrix.
7459 
7460    Logically Collective on Mat and MatNullSpace
7461 
7462    Input Parameters:
7463 +  mat - the matrix
7464 -  nullsp - the null space object
7465 
7466    Level: developer
7467 
7468    Notes:
7469       This null space is used by solvers. Overwrites any previous null space that may have been attached
7470 
7471    Concepts: null space^attaching to matrix
7472 
7473 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7474 @*/
7475 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
7476 {
7477   PetscFunctionBegin;
7478   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7479   PetscValidType(mat,1);
7480   PetscValidPointer(nullsp,2);
7481   *nullsp = mat->nullsp;
7482   PetscFunctionReturn(0);
7483 }
7484 
7485 #undef __FUNCT__
7486 #define __FUNCT__ "MatSetNullSpace"
7487 /*@
7488    MatSetNullSpace - attaches a null space to a matrix.
7489         This null space will be removed from the resulting vector whenever
7490         MatMult() is called
7491 
7492    Logically Collective on Mat and MatNullSpace
7493 
7494    Input Parameters:
7495 +  mat - the matrix
7496 -  nullsp - the null space object
7497 
7498    Level: advanced
7499 
7500    Notes:
7501       This null space is used by solvers. Overwrites any previous null space that may have been attached
7502 
7503    Concepts: null space^attaching to matrix
7504 
7505 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7506 @*/
7507 PetscErrorCode  MatSetNullSpace(Mat mat,MatNullSpace nullsp)
7508 {
7509   PetscErrorCode ierr;
7510 
7511   PetscFunctionBegin;
7512   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7513   PetscValidType(mat,1);
7514   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7515   MatCheckPreallocated(mat,1);
7516   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7517   ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr);
7518   mat->nullsp = nullsp;
7519   PetscFunctionReturn(0);
7520 }
7521 
7522 #undef __FUNCT__
7523 #define __FUNCT__ "MatSetNearNullSpace"
7524 /*@
7525    MatSetNearNullSpace - attaches a null space to a matrix.
7526         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.
7527 
7528    Logically Collective on Mat and MatNullSpace
7529 
7530    Input Parameters:
7531 +  mat - the matrix
7532 -  nullsp - the null space object
7533 
7534    Level: advanced
7535 
7536    Notes:
7537       Overwrites any previous near null space that may have been attached
7538 
7539    Concepts: null space^attaching to matrix
7540 
7541 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace()
7542 @*/
7543 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
7544 {
7545   PetscErrorCode ierr;
7546 
7547   PetscFunctionBegin;
7548   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7549   PetscValidType(mat,1);
7550   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7551   MatCheckPreallocated(mat,1);
7552   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7553   ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr);
7554   mat->nearnullsp = nullsp;
7555   PetscFunctionReturn(0);
7556 }
7557 
7558 #undef __FUNCT__
7559 #define __FUNCT__ "MatGetNearNullSpace"
7560 /*@
7561    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()
7562 
7563    Not Collective
7564 
7565    Input Parameters:
7566 .  mat - the matrix
7567 
7568    Output Parameters:
7569 .  nullsp - the null space object, PETSC_NULL if not set
7570 
7571    Level: developer
7572 
7573    Concepts: null space^attaching to matrix
7574 
7575 .seealso: MatSetNearNullSpace(), MatGetNullSpace()
7576 @*/
7577 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
7578 {
7579 
7580   PetscFunctionBegin;
7581   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7582   PetscValidType(mat,1);
7583   PetscValidPointer(nullsp,2);
7584   MatCheckPreallocated(mat,1);
7585   *nullsp = mat->nearnullsp;
7586   PetscFunctionReturn(0);
7587 }
7588 
7589 #undef __FUNCT__
7590 #define __FUNCT__ "MatICCFactor"
7591 /*@C
7592    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
7593 
7594    Collective on Mat
7595 
7596    Input Parameters:
7597 +  mat - the matrix
7598 .  row - row/column permutation
7599 .  fill - expected fill factor >= 1.0
7600 -  level - level of fill, for ICC(k)
7601 
7602    Notes:
7603    Probably really in-place only when level of fill is zero, otherwise allocates
7604    new space to store factored matrix and deletes previous memory.
7605 
7606    Most users should employ the simplified KSP interface for linear solvers
7607    instead of working directly with matrix algebra routines such as this.
7608    See, e.g., KSPCreate().
7609 
7610    Level: developer
7611 
7612    Concepts: matrices^incomplete Cholesky factorization
7613    Concepts: Cholesky factorization
7614 
7615 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
7616 
7617     Developer Note: fortran interface is not autogenerated as the f90
7618     interface defintion cannot be generated correctly [due to MatFactorInfo]
7619 
7620 @*/
7621 PetscErrorCode  MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
7622 {
7623   PetscErrorCode ierr;
7624 
7625   PetscFunctionBegin;
7626   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7627   PetscValidType(mat,1);
7628   if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2);
7629   PetscValidPointer(info,3);
7630   if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square");
7631   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7632   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7633   if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7634   MatCheckPreallocated(mat,1);
7635   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
7636   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7637   PetscFunctionReturn(0);
7638 }
7639 
7640 #undef __FUNCT__
7641 #define __FUNCT__ "MatSetValuesAdic"
7642 /*@
7643    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
7644 
7645    Not Collective
7646 
7647    Input Parameters:
7648 +  mat - the matrix
7649 -  v - the values compute with ADIC
7650 
7651    Level: developer
7652 
7653    Notes:
7654      Must call MatSetColoring() before using this routine. Also this matrix must already
7655      have its nonzero pattern determined.
7656 
7657 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7658           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
7659 @*/
7660 PetscErrorCode  MatSetValuesAdic(Mat mat,void *v)
7661 {
7662   PetscErrorCode ierr;
7663 
7664   PetscFunctionBegin;
7665   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7666   PetscValidType(mat,1);
7667   PetscValidPointer(mat,2);
7668 
7669   if (!mat->assembled) {
7670     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7671   }
7672   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7673   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7674   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
7675   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7676   ierr = MatView_Private(mat);CHKERRQ(ierr);
7677   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7678   PetscFunctionReturn(0);
7679 }
7680 
7681 
7682 #undef __FUNCT__
7683 #define __FUNCT__ "MatSetColoring"
7684 /*@
7685    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
7686 
7687    Not Collective
7688 
7689    Input Parameters:
7690 +  mat - the matrix
7691 -  coloring - the coloring
7692 
7693    Level: developer
7694 
7695 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7696           MatSetValues(), MatSetValuesAdic()
7697 @*/
7698 PetscErrorCode  MatSetColoring(Mat mat,ISColoring coloring)
7699 {
7700   PetscErrorCode ierr;
7701 
7702   PetscFunctionBegin;
7703   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7704   PetscValidType(mat,1);
7705   PetscValidPointer(coloring,2);
7706 
7707   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7708   if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7709   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
7710   PetscFunctionReturn(0);
7711 }
7712 
7713 #undef __FUNCT__
7714 #define __FUNCT__ "MatSetValuesAdifor"
7715 /*@
7716    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
7717 
7718    Not Collective
7719 
7720    Input Parameters:
7721 +  mat - the matrix
7722 .  nl - leading dimension of v
7723 -  v - the values compute with ADIFOR
7724 
7725    Level: developer
7726 
7727    Notes:
7728      Must call MatSetColoring() before using this routine. Also this matrix must already
7729      have its nonzero pattern determined.
7730 
7731 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7732           MatSetValues(), MatSetColoring()
7733 @*/
7734 PetscErrorCode  MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
7735 {
7736   PetscErrorCode ierr;
7737 
7738   PetscFunctionBegin;
7739   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7740   PetscValidType(mat,1);
7741   PetscValidPointer(v,3);
7742 
7743   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7744   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7745   if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7746   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
7747   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7748   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7749   PetscFunctionReturn(0);
7750 }
7751 
7752 #undef __FUNCT__
7753 #define __FUNCT__ "MatDiagonalScaleLocal"
7754 /*@
7755    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
7756          ghosted ones.
7757 
7758    Not Collective
7759 
7760    Input Parameters:
7761 +  mat - the matrix
7762 -  diag = the diagonal values, including ghost ones
7763 
7764    Level: developer
7765 
7766    Notes: Works only for MPIAIJ and MPIBAIJ matrices
7767 
7768 .seealso: MatDiagonalScale()
7769 @*/
7770 PetscErrorCode  MatDiagonalScaleLocal(Mat mat,Vec diag)
7771 {
7772   PetscErrorCode ierr;
7773   PetscMPIInt    size;
7774 
7775   PetscFunctionBegin;
7776   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7777   PetscValidHeaderSpecific(diag,VEC_CLASSID,2);
7778   PetscValidType(mat,1);
7779 
7780   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7781   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7782   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7783   if (size == 1) {
7784     PetscInt n,m;
7785     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
7786     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
7787     if (m == n) {
7788       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
7789     } else {
7790       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
7791     }
7792   } else {
7793     ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr);
7794   }
7795   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7796   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7797   PetscFunctionReturn(0);
7798 }
7799 
7800 #undef __FUNCT__
7801 #define __FUNCT__ "MatGetInertia"
7802 /*@
7803    MatGetInertia - Gets the inertia from a factored matrix
7804 
7805    Collective on Mat
7806 
7807    Input Parameter:
7808 .  mat - the matrix
7809 
7810    Output Parameters:
7811 +   nneg - number of negative eigenvalues
7812 .   nzero - number of zero eigenvalues
7813 -   npos - number of positive eigenvalues
7814 
7815    Level: advanced
7816 
7817    Notes: Matrix must have been factored by MatCholeskyFactor()
7818 
7819 
7820 @*/
7821 PetscErrorCode  MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
7822 {
7823   PetscErrorCode ierr;
7824 
7825   PetscFunctionBegin;
7826   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7827   PetscValidType(mat,1);
7828   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7829   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
7830   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7831   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
7832   PetscFunctionReturn(0);
7833 }
7834 
7835 /* ----------------------------------------------------------------*/
7836 #undef __FUNCT__
7837 #define __FUNCT__ "MatSolves"
7838 /*@C
7839    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
7840 
7841    Neighbor-wise Collective on Mat and Vecs
7842 
7843    Input Parameters:
7844 +  mat - the factored matrix
7845 -  b - the right-hand-side vectors
7846 
7847    Output Parameter:
7848 .  x - the result vectors
7849 
7850    Notes:
7851    The vectors b and x cannot be the same.  I.e., one cannot
7852    call MatSolves(A,x,x).
7853 
7854    Notes:
7855    Most users should employ the simplified KSP interface for linear solvers
7856    instead of working directly with matrix algebra routines such as this.
7857    See, e.g., KSPCreate().
7858 
7859    Level: developer
7860 
7861    Concepts: matrices^triangular solves
7862 
7863 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
7864 @*/
7865 PetscErrorCode  MatSolves(Mat mat,Vecs b,Vecs x)
7866 {
7867   PetscErrorCode ierr;
7868 
7869   PetscFunctionBegin;
7870   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7871   PetscValidType(mat,1);
7872   if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors");
7873   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7874   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
7875 
7876   if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7877   MatCheckPreallocated(mat,1);
7878   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7879   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
7880   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7881   PetscFunctionReturn(0);
7882 }
7883 
7884 #undef __FUNCT__
7885 #define __FUNCT__ "MatIsSymmetric"
7886 /*@
7887    MatIsSymmetric - Test whether a matrix is symmetric
7888 
7889    Collective on Mat
7890 
7891    Input Parameter:
7892 +  A - the matrix to test
7893 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
7894 
7895    Output Parameters:
7896 .  flg - the result
7897 
7898    Notes: For real numbers MatIsSymmetric() and MatIsHermitian() return identical results
7899 
7900    Level: intermediate
7901 
7902    Concepts: matrix^symmetry
7903 
7904 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7905 @*/
7906 PetscErrorCode  MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
7907 {
7908   PetscErrorCode ierr;
7909 
7910   PetscFunctionBegin;
7911   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7912   PetscValidPointer(flg,2);
7913 
7914   if (!A->symmetric_set) {
7915     if (!A->ops->issymmetric) {
7916       const MatType mattype;
7917       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7918       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7919     }
7920     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7921     if (!tol) {
7922       A->symmetric_set = PETSC_TRUE;
7923       A->symmetric = *flg;
7924       if (A->symmetric) {
7925 	A->structurally_symmetric_set = PETSC_TRUE;
7926 	A->structurally_symmetric     = PETSC_TRUE;
7927       }
7928     }
7929   } else if (A->symmetric) {
7930     *flg = PETSC_TRUE;
7931   } else if (!tol) {
7932     *flg = PETSC_FALSE;
7933   } else {
7934     if (!A->ops->issymmetric) {
7935       const MatType mattype;
7936       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7937       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7938     }
7939     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7940   }
7941   PetscFunctionReturn(0);
7942 }
7943 
7944 #undef __FUNCT__
7945 #define __FUNCT__ "MatIsHermitian"
7946 /*@
7947    MatIsHermitian - Test whether a matrix is Hermitian
7948 
7949    Collective on Mat
7950 
7951    Input Parameter:
7952 +  A - the matrix to test
7953 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
7954 
7955    Output Parameters:
7956 .  flg - the result
7957 
7958    Level: intermediate
7959 
7960    Concepts: matrix^symmetry
7961 
7962 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
7963           MatIsSymmetricKnown(), MatIsSymmetric()
7964 @*/
7965 PetscErrorCode  MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
7966 {
7967   PetscErrorCode ierr;
7968 
7969   PetscFunctionBegin;
7970   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7971   PetscValidPointer(flg,2);
7972 
7973   if (!A->hermitian_set) {
7974     if (!A->ops->ishermitian) {
7975       const MatType mattype;
7976       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7977       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7978     }
7979     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7980     if (!tol) {
7981       A->hermitian_set = PETSC_TRUE;
7982       A->hermitian = *flg;
7983       if (A->hermitian) {
7984 	A->structurally_symmetric_set = PETSC_TRUE;
7985 	A->structurally_symmetric     = PETSC_TRUE;
7986       }
7987     }
7988   } else if (A->hermitian) {
7989     *flg = PETSC_TRUE;
7990   } else if (!tol) {
7991     *flg = PETSC_FALSE;
7992   } else {
7993     if (!A->ops->ishermitian) {
7994       const MatType mattype;
7995       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7996       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7997     }
7998     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7999   }
8000   PetscFunctionReturn(0);
8001 }
8002 
8003 #undef __FUNCT__
8004 #define __FUNCT__ "MatIsSymmetricKnown"
8005 /*@
8006    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
8007 
8008    Not Collective
8009 
8010    Input Parameter:
8011 .  A - the matrix to check
8012 
8013    Output Parameters:
8014 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
8015 -  flg - the result
8016 
8017    Level: advanced
8018 
8019    Concepts: matrix^symmetry
8020 
8021    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
8022          if you want it explicitly checked
8023 
8024 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8025 @*/
8026 PetscErrorCode  MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8027 {
8028   PetscFunctionBegin;
8029   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8030   PetscValidPointer(set,2);
8031   PetscValidPointer(flg,3);
8032   if (A->symmetric_set) {
8033     *set = PETSC_TRUE;
8034     *flg = A->symmetric;
8035   } else {
8036     *set = PETSC_FALSE;
8037   }
8038   PetscFunctionReturn(0);
8039 }
8040 
8041 #undef __FUNCT__
8042 #define __FUNCT__ "MatIsHermitianKnown"
8043 /*@
8044    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
8045 
8046    Not Collective
8047 
8048    Input Parameter:
8049 .  A - the matrix to check
8050 
8051    Output Parameters:
8052 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8053 -  flg - the result
8054 
8055    Level: advanced
8056 
8057    Concepts: matrix^symmetry
8058 
8059    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8060          if you want it explicitly checked
8061 
8062 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8063 @*/
8064 PetscErrorCode  MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8065 {
8066   PetscFunctionBegin;
8067   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8068   PetscValidPointer(set,2);
8069   PetscValidPointer(flg,3);
8070   if (A->hermitian_set) {
8071     *set = PETSC_TRUE;
8072     *flg = A->hermitian;
8073   } else {
8074     *set = PETSC_FALSE;
8075   }
8076   PetscFunctionReturn(0);
8077 }
8078 
8079 #undef __FUNCT__
8080 #define __FUNCT__ "MatIsStructurallySymmetric"
8081 /*@
8082    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
8083 
8084    Collective on Mat
8085 
8086    Input Parameter:
8087 .  A - the matrix to test
8088 
8089    Output Parameters:
8090 .  flg - the result
8091 
8092    Level: intermediate
8093 
8094    Concepts: matrix^symmetry
8095 
8096 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8097 @*/
8098 PetscErrorCode  MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8099 {
8100   PetscErrorCode ierr;
8101 
8102   PetscFunctionBegin;
8103   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8104   PetscValidPointer(flg,2);
8105   if (!A->structurally_symmetric_set) {
8106     if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8107     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
8108     A->structurally_symmetric_set = PETSC_TRUE;
8109   }
8110   *flg = A->structurally_symmetric;
8111   PetscFunctionReturn(0);
8112 }
8113 
8114 #undef __FUNCT__
8115 #define __FUNCT__ "MatStashGetInfo"
8116 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
8117 /*@
8118    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8119        to be communicated to other processors during the MatAssemblyBegin/End() process
8120 
8121     Not collective
8122 
8123    Input Parameter:
8124 .   vec - the vector
8125 
8126    Output Parameters:
8127 +   nstash   - the size of the stash
8128 .   reallocs - the number of additional mallocs incurred.
8129 .   bnstash   - the size of the block stash
8130 -   breallocs - the number of additional mallocs incurred.in the block stash
8131 
8132    Level: advanced
8133 
8134 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
8135 
8136 @*/
8137 PetscErrorCode  MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8138 {
8139   PetscErrorCode ierr;
8140   PetscFunctionBegin;
8141   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
8142   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
8143   PetscFunctionReturn(0);
8144 }
8145 
8146 #undef __FUNCT__
8147 #define __FUNCT__ "MatGetVecs"
8148 /*@C
8149    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
8150      parallel layout
8151 
8152    Collective on Mat
8153 
8154    Input Parameter:
8155 .  mat - the matrix
8156 
8157    Output Parameter:
8158 +   right - (optional) vector that the matrix can be multiplied against
8159 -   left - (optional) vector that the matrix vector product can be stored in
8160 
8161   Level: advanced
8162 
8163 .seealso: MatCreate()
8164 @*/
8165 PetscErrorCode  MatGetVecs(Mat mat,Vec *right,Vec *left)
8166 {
8167   PetscErrorCode ierr;
8168 
8169   PetscFunctionBegin;
8170   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8171   PetscValidType(mat,1);
8172   MatCheckPreallocated(mat,1);
8173   if (mat->ops->getvecs) {
8174     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
8175   } else {
8176     PetscMPIInt size;
8177     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
8178     if (right) {
8179       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
8180       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8181       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
8182       ierr = VecSetType(*right,VECSTANDARD);CHKERRQ(ierr);
8183       ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr);
8184     }
8185     if (left) {
8186       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
8187       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8188       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
8189       ierr = VecSetType(*left,VECSTANDARD);CHKERRQ(ierr);
8190       ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr);
8191     }
8192   }
8193   PetscFunctionReturn(0);
8194 }
8195 
8196 #undef __FUNCT__
8197 #define __FUNCT__ "MatFactorInfoInitialize"
8198 /*@C
8199    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8200      with default values.
8201 
8202    Not Collective
8203 
8204    Input Parameters:
8205 .    info - the MatFactorInfo data structure
8206 
8207 
8208    Notes: The solvers are generally used through the KSP and PC objects, for example
8209           PCLU, PCILU, PCCHOLESKY, PCICC
8210 
8211    Level: developer
8212 
8213 .seealso: MatFactorInfo
8214 
8215     Developer Note: fortran interface is not autogenerated as the f90
8216     interface defintion cannot be generated correctly [due to MatFactorInfo]
8217 
8218 @*/
8219 
8220 PetscErrorCode  MatFactorInfoInitialize(MatFactorInfo *info)
8221 {
8222   PetscErrorCode ierr;
8223 
8224   PetscFunctionBegin;
8225   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
8226   PetscFunctionReturn(0);
8227 }
8228 
8229 #undef __FUNCT__
8230 #define __FUNCT__ "MatPtAP"
8231 /*@
8232    MatPtAP - Creates the matrix product C = P^T * A * P
8233 
8234    Neighbor-wise Collective on Mat
8235 
8236    Input Parameters:
8237 +  A - the matrix
8238 .  P - the projection matrix
8239 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8240 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P))
8241 
8242    Output Parameters:
8243 .  C - the product matrix
8244 
8245    Notes:
8246    C will be created and must be destroyed by the user with MatDestroy().
8247 
8248    This routine is currently only implemented for pairs of AIJ matrices and classes
8249    which inherit from AIJ.
8250 
8251    Level: intermediate
8252 
8253 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
8254 @*/
8255 PetscErrorCode  MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8256 {
8257   PetscErrorCode ierr;
8258 
8259   PetscFunctionBegin;
8260   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8261   PetscValidType(A,1);
8262   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8263   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8264   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8265   PetscValidType(P,2);
8266   MatCheckPreallocated(P,2);
8267   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8268   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8269   PetscValidPointer(C,3);
8270   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);
8271   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8272   MatCheckPreallocated(A,1);
8273 
8274   if (!A->ops->ptap) {
8275     const MatType mattype;
8276     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8277     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype);
8278   }
8279   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8280   ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
8281   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8282   PetscFunctionReturn(0);
8283 }
8284 
8285 #undef __FUNCT__
8286 #define __FUNCT__ "MatPtAPNumeric"
8287 /*@
8288    MatPtAPNumeric - Computes the matrix product C = P^T * A * P
8289 
8290    Neighbor-wise Collective on Mat
8291 
8292    Input Parameters:
8293 +  A - the matrix
8294 -  P - the projection matrix
8295 
8296    Output Parameters:
8297 .  C - the product matrix
8298 
8299    Notes:
8300    C must have been created by calling MatPtAPSymbolic and must be destroyed by
8301    the user using MatDeatroy().
8302 
8303    This routine is currently only implemented for pairs of AIJ matrices and classes
8304    which inherit from AIJ.  C will be of type MATAIJ.
8305 
8306    Level: intermediate
8307 
8308 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
8309 @*/
8310 PetscErrorCode  MatPtAPNumeric(Mat A,Mat P,Mat C)
8311 {
8312   PetscErrorCode ierr;
8313 
8314   PetscFunctionBegin;
8315   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8316   PetscValidType(A,1);
8317   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8318   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8319   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8320   PetscValidType(P,2);
8321   MatCheckPreallocated(P,2);
8322   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8323   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8324   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8325   PetscValidType(C,3);
8326   MatCheckPreallocated(C,3);
8327   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8328   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);
8329   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);
8330   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);
8331   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);
8332   MatCheckPreallocated(A,1);
8333 
8334   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8335   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
8336   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8337   PetscFunctionReturn(0);
8338 }
8339 
8340 #undef __FUNCT__
8341 #define __FUNCT__ "MatPtAPSymbolic"
8342 /*@
8343    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P
8344 
8345    Neighbor-wise Collective on Mat
8346 
8347    Input Parameters:
8348 +  A - the matrix
8349 -  P - the projection matrix
8350 
8351    Output Parameters:
8352 .  C - the (i,j) structure of the product matrix
8353 
8354    Notes:
8355    C will be created and must be destroyed by the user with MatDestroy().
8356 
8357    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8358    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8359    this (i,j) structure by calling MatPtAPNumeric().
8360 
8361    Level: intermediate
8362 
8363 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
8364 @*/
8365 PetscErrorCode  MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
8366 {
8367   PetscErrorCode ierr;
8368 
8369   PetscFunctionBegin;
8370   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8371   PetscValidType(A,1);
8372   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8373   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8374   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8375   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8376   PetscValidType(P,2);
8377   MatCheckPreallocated(P,2);
8378   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8379   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8380   PetscValidPointer(C,3);
8381 
8382   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);
8383   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);
8384   MatCheckPreallocated(A,1);
8385   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8386   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
8387   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8388 
8389   /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */
8390 
8391   PetscFunctionReturn(0);
8392 }
8393 
8394 #undef __FUNCT__
8395 #define __FUNCT__ "MatRARt"
8396 /*@
8397    MatRARt - Creates the matrix product C = R * A * R^T
8398 
8399    Neighbor-wise Collective on Mat
8400 
8401    Input Parameters:
8402 +  A - the matrix
8403 .  R - the projection matrix
8404 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8405 -  fill - expected fill as ratio of nnz(C)/nnz(A)
8406 
8407    Output Parameters:
8408 .  C - the product matrix
8409 
8410    Notes:
8411    C will be created and must be destroyed by the user with MatDestroy().
8412 
8413    This routine is currently only implemented for pairs of AIJ matrices and classes
8414    which inherit from AIJ.
8415 
8416    Level: intermediate
8417 
8418 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
8419 @*/
8420 PetscErrorCode  MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
8421 {
8422   PetscErrorCode ierr;
8423 
8424   PetscFunctionBegin;
8425   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8426   PetscValidType(A,1);
8427   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8428   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8429   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8430   PetscValidType(R,2);
8431   MatCheckPreallocated(R,2);
8432   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8433   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8434   PetscValidPointer(C,3);
8435   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);
8436   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8437   MatCheckPreallocated(A,1);
8438 
8439   if (!A->ops->rart) {
8440     const MatType mattype;
8441     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8442     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support RARt",mattype);
8443   }
8444   ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8445   ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr);
8446   ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8447   PetscFunctionReturn(0);
8448 }
8449 
8450 #undef __FUNCT__
8451 #define __FUNCT__ "MatRARtNumeric"
8452 /*@
8453    MatRARtNumeric - Computes the matrix product C = R * A * R^T
8454 
8455    Neighbor-wise Collective on Mat
8456 
8457    Input Parameters:
8458 +  A - the matrix
8459 -  R - the projection matrix
8460 
8461    Output Parameters:
8462 .  C - the product matrix
8463 
8464    Notes:
8465    C must have been created by calling MatRARtSymbolic and must be destroyed by
8466    the user using MatDeatroy().
8467 
8468    This routine is currently only implemented for pairs of AIJ matrices and classes
8469    which inherit from AIJ.  C will be of type MATAIJ.
8470 
8471    Level: intermediate
8472 
8473 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
8474 @*/
8475 PetscErrorCode  MatRARtNumeric(Mat A,Mat R,Mat C)
8476 {
8477   PetscErrorCode ierr;
8478 
8479   PetscFunctionBegin;
8480   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8481   PetscValidType(A,1);
8482   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8483   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8484   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8485   PetscValidType(R,2);
8486   MatCheckPreallocated(R,2);
8487   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8488   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8489   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8490   PetscValidType(C,3);
8491   MatCheckPreallocated(C,3);
8492   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8493   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);
8494   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);
8495   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);
8496   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);
8497   MatCheckPreallocated(A,1);
8498 
8499   ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8500   ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr);
8501   ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8502   PetscFunctionReturn(0);
8503 }
8504 
8505 #undef __FUNCT__
8506 #define __FUNCT__ "MatRARtSymbolic"
8507 /*@
8508    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T
8509 
8510    Neighbor-wise Collective on Mat
8511 
8512    Input Parameters:
8513 +  A - the matrix
8514 -  R - the projection matrix
8515 
8516    Output Parameters:
8517 .  C - the (i,j) structure of the product matrix
8518 
8519    Notes:
8520    C will be created and must be destroyed by the user with MatDestroy().
8521 
8522    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8523    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8524    this (i,j) structure by calling MatRARtNumeric().
8525 
8526    Level: intermediate
8527 
8528 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
8529 @*/
8530 PetscErrorCode  MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
8531 {
8532   PetscErrorCode ierr;
8533 
8534   PetscFunctionBegin;
8535   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8536   PetscValidType(A,1);
8537   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8538   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8539   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8540   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8541   PetscValidType(R,2);
8542   MatCheckPreallocated(R,2);
8543   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8544   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8545   PetscValidPointer(C,3);
8546 
8547   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);
8548   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);
8549   MatCheckPreallocated(A,1);
8550   ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8551   ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr);
8552   ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8553 
8554   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
8555   PetscFunctionReturn(0);
8556 }
8557 
8558 extern PetscErrorCode MatQueryOp(MPI_Comm comm, void (**function)(void), const char op[], PetscInt numArgs, ...);
8559 
8560 #undef __FUNCT__
8561 #define __FUNCT__ "MatMatMult"
8562 /*@
8563    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
8564 
8565    Neighbor-wise Collective on Mat
8566 
8567    Input Parameters:
8568 +  A - the left matrix
8569 .  B - the right matrix
8570 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8571 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
8572           if the result is a dense matrix this is irrelevent
8573 
8574    Output Parameters:
8575 .  C - the product matrix
8576 
8577    Notes:
8578    Unless scall is MAT_REUSE_MATRIX C will be created.
8579 
8580    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8581 
8582    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8583    actually needed.
8584 
8585    If you have many matrices with the same non-zero structure to multiply, you
8586    should either
8587 $   1) use MAT_REUSE_MATRIX in all calls but the first or
8588 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
8589 
8590    Level: intermediate
8591 
8592 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
8593 @*/
8594 PetscErrorCode  MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8595 {
8596   PetscErrorCode ierr;
8597   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8598   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8599   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
8600 
8601   PetscFunctionBegin;
8602   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8603   PetscValidType(A,1);
8604   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8605   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8606   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8607   PetscValidType(B,2);
8608   MatCheckPreallocated(B,2);
8609   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8610   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8611   PetscValidPointer(C,3);
8612   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);
8613   if (scall == MAT_REUSE_MATRIX){
8614     PetscValidPointer(*C,5);
8615     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
8616     ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8617     ierr = (*(*C)->ops->matmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8618     ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8619   }
8620   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8621   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8622   MatCheckPreallocated(A,1);
8623 
8624   fA = A->ops->matmult;
8625   fB = B->ops->matmult;
8626   if (fB == fA) {
8627     if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8628     mult = fB;
8629   } else {
8630     /* dispatch based on the type of A and B from their PetscObject's PetscFLists. */
8631     char  multname[256];
8632     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
8633     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8634     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
8635     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8636     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
8637     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
8638     if(!mult){
8639       /* dual dispatch using MatQueryOp */
8640       ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&mult), "MatMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8641       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);
8642     }
8643   }
8644   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8645   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
8646   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8647   PetscFunctionReturn(0);
8648 }
8649 
8650 #undef __FUNCT__
8651 #define __FUNCT__ "MatMatMultSymbolic"
8652 /*@
8653    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
8654    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
8655 
8656    Neighbor-wise Collective on Mat
8657 
8658    Input Parameters:
8659 +  A - the left matrix
8660 .  B - the right matrix
8661 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
8662       if C is a dense matrix this is irrelevent
8663 
8664    Output Parameters:
8665 .  C - the product matrix
8666 
8667    Notes:
8668    Unless scall is MAT_REUSE_MATRIX C will be created.
8669 
8670    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8671    actually needed.
8672 
8673    This routine is currently implemented for
8674     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
8675     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8676     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8677 
8678    Level: intermediate
8679 
8680    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
8681      We should incorporate them into PETSc.
8682 
8683 .seealso: MatMatMult(), MatMatMultNumeric()
8684 @*/
8685 PetscErrorCode  MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
8686 {
8687   PetscErrorCode ierr;
8688   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
8689   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
8690   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
8691 
8692   PetscFunctionBegin;
8693   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8694   PetscValidType(A,1);
8695   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8696   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8697 
8698   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8699   PetscValidType(B,2);
8700   MatCheckPreallocated(B,2);
8701   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8702   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8703   PetscValidPointer(C,3);
8704 
8705   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);
8706   if (fill == PETSC_DEFAULT) fill = 2.0;
8707   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8708   MatCheckPreallocated(A,1);
8709 
8710   Asymbolic = A->ops->matmultsymbolic;
8711   Bsymbolic = B->ops->matmultsymbolic;
8712   if (Asymbolic == Bsymbolic){
8713     if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
8714     symbolic = Bsymbolic;
8715   } else { /* dispatch based on the type of A and B */
8716     char  symbolicname[256];
8717     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
8718     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8719     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
8720     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8721     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
8722     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
8723     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);
8724   }
8725   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8726   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
8727   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8728   PetscFunctionReturn(0);
8729 }
8730 
8731 #undef __FUNCT__
8732 #define __FUNCT__ "MatMatMultNumeric"
8733 /*@
8734    MatMatMultNumeric - Performs the numeric matrix-matrix product.
8735    Call this routine after first calling MatMatMultSymbolic().
8736 
8737    Neighbor-wise Collective on Mat
8738 
8739    Input Parameters:
8740 +  A - the left matrix
8741 -  B - the right matrix
8742 
8743    Output Parameters:
8744 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
8745 
8746    Notes:
8747    C must have been created with MatMatMultSymbolic().
8748 
8749    This routine is currently implemented for
8750     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
8751     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8752     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8753 
8754    Level: intermediate
8755 
8756 .seealso: MatMatMult(), MatMatMultSymbolic()
8757 @*/
8758 PetscErrorCode  MatMatMultNumeric(Mat A,Mat B,Mat C)
8759 {
8760   PetscErrorCode ierr;
8761   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
8762   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
8763   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
8764 
8765   PetscFunctionBegin;
8766   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8767   PetscValidType(A,1);
8768   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8769   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8770 
8771   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8772   PetscValidType(B,2);
8773   MatCheckPreallocated(B,2);
8774   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8775   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8776 
8777   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8778   PetscValidType(C,3);
8779   MatCheckPreallocated(C,3);
8780   if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8781   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8782 
8783   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);
8784   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);
8785   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);
8786   MatCheckPreallocated(A,1);
8787 
8788   Anumeric = A->ops->matmultnumeric;
8789   Bnumeric = B->ops->matmultnumeric;
8790   if (Anumeric == Bnumeric){
8791     if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
8792     numeric = Bnumeric;
8793   } else {
8794     char  numericname[256];
8795     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
8796     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8797     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
8798     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8799     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
8800     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
8801     if (!numeric)
8802       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);
8803   }
8804   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8805   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
8806   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8807   PetscFunctionReturn(0);
8808 }
8809 
8810 #undef __FUNCT__
8811 #define __FUNCT__ "MatMatTransposeMult"
8812 /*@
8813    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.
8814 
8815    Neighbor-wise Collective on Mat
8816 
8817    Input Parameters:
8818 +  A - the left matrix
8819 .  B - the right matrix
8820 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8821 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8822 
8823    Output Parameters:
8824 .  C - the product matrix
8825 
8826    Notes:
8827    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8828 
8829    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8830 
8831   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8832    actually needed.
8833 
8834    This routine is currently only implemented for pairs of SeqAIJ matrices.  C will be of type MATSEQAIJ.
8835 
8836    Level: intermediate
8837 
8838 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
8839 @*/
8840 PetscErrorCode  MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8841 {
8842   PetscErrorCode ierr;
8843   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8844   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8845 
8846   PetscFunctionBegin;
8847   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8848   PetscValidType(A,1);
8849   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8850   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8851   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8852   PetscValidType(B,2);
8853   MatCheckPreallocated(B,2);
8854   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8855   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8856   PetscValidPointer(C,3);
8857   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);
8858   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8859   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8860   MatCheckPreallocated(A,1);
8861 
8862   fA = A->ops->mattransposemult;
8863   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
8864   fB = B->ops->mattransposemult;
8865   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
8866   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);
8867 
8868   if (scall == MAT_INITIAL_MATRIX){
8869     ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8870     ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr);
8871     ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8872   }
8873   ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8874   ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr);
8875   ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8876   PetscFunctionReturn(0);
8877 }
8878 
8879 #undef __FUNCT__
8880 #define __FUNCT__ "MatTransposeMatMult"
8881 /*@
8882    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.
8883 
8884    Neighbor-wise Collective on Mat
8885 
8886    Input Parameters:
8887 +  A - the left matrix
8888 .  B - the right matrix
8889 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8890 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8891 
8892    Output Parameters:
8893 .  C - the product matrix
8894 
8895    Notes:
8896    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8897 
8898    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8899 
8900   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8901    actually needed.
8902 
8903    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
8904    which inherit from SeqAIJ.  C will be of same type as the input matrices.
8905 
8906    Level: intermediate
8907 
8908 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
8909 @*/
8910 PetscErrorCode  MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8911 {
8912   PetscErrorCode ierr;
8913   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8914   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8915   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*);
8916 
8917   PetscFunctionBegin;
8918   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8919   PetscValidType(A,1);
8920   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8921   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8922   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8923   PetscValidType(B,2);
8924   MatCheckPreallocated(B,2);
8925   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8926   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8927   PetscValidPointer(C,3);
8928   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);
8929   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8930   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8931   MatCheckPreallocated(A,1);
8932 
8933   fA = A->ops->transposematmult;
8934   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
8935   fB = B->ops->transposematmult;
8936   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8937   if (fB==fA) {
8938     transposematmult = fA;
8939   }
8940   else {
8941     /* dual dispatch using MatQueryOp */
8942     ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&transposematmult), "MatTansposeMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8943     if(!transposematmult)
8944       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);
8945   }
8946   ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8947   ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8948   ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8949   PetscFunctionReturn(0);
8950 }
8951 
8952 #undef __FUNCT__
8953 #define __FUNCT__ "MatGetRedundantMatrix"
8954 /*@C
8955    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
8956 
8957    Collective on Mat
8958 
8959    Input Parameters:
8960 +  mat - the matrix
8961 .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
8962 .  subcomm - MPI communicator split from the communicator where mat resides in
8963 .  mlocal_red - number of local rows of the redundant matrix
8964 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8965 
8966    Output Parameter:
8967 .  matredundant - redundant matrix
8968 
8969    Notes:
8970    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
8971    original matrix has not changed from that last call to MatGetRedundantMatrix().
8972 
8973    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
8974    calling it.
8975 
8976    Only MPIAIJ matrix is supported.
8977 
8978    Level: advanced
8979 
8980    Concepts: subcommunicator
8981    Concepts: duplicate matrix
8982 
8983 .seealso: MatDestroy()
8984 @*/
8985 PetscErrorCode  MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
8986 {
8987   PetscErrorCode ierr;
8988 
8989   PetscFunctionBegin;
8990   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8991   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
8992     PetscValidPointer(*matredundant,6);
8993     PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6);
8994   }
8995   if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8996   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8997   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8998   MatCheckPreallocated(mat,1);
8999 
9000   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
9001   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
9002   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
9003   PetscFunctionReturn(0);
9004 }
9005 
9006 #undef __FUNCT__
9007 #define __FUNCT__ "MatGetMultiProcBlock"
9008 /*@C
9009    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
9010    a given 'mat' object. Each submatrix can span multiple procs.
9011 
9012    Collective on Mat
9013 
9014    Input Parameters:
9015 +  mat - the matrix
9016 .  subcomm - the subcommunicator obtained by com_split(comm)
9017 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9018 
9019    Output Parameter:
9020 .  subMat - 'parallel submatrices each spans a given subcomm
9021 
9022   Notes:
9023   The submatrix partition across processors is dicated by 'subComm' a
9024   communicator obtained by com_split(comm). The comm_split
9025   is not restriced to be grouped with consequitive original ranks.
9026 
9027   Due the comm_split() usage, the parallel layout of the submatrices
9028   map directly to the layout of the original matrix [wrt the local
9029   row,col partitioning]. So the original 'DiagonalMat' naturally maps
9030   into the 'DiagonalMat' of the subMat, hence it is used directly from
9031   the subMat. However the offDiagMat looses some columns - and this is
9032   reconstructed with MatSetValues()
9033 
9034   Level: advanced
9035 
9036   Concepts: subcommunicator
9037   Concepts: submatrices
9038 
9039 .seealso: MatGetSubMatrices()
9040 @*/
9041 PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat* subMat)
9042 {
9043   PetscErrorCode ierr;
9044   PetscMPIInt    commsize,subCommSize;
9045 
9046   PetscFunctionBegin;
9047   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&commsize);CHKERRQ(ierr);
9048   ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr);
9049   if (subCommSize > commsize) SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);
9050 
9051   ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9052   ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr);
9053   ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9054   PetscFunctionReturn(0);
9055 }
9056 
9057 #undef __FUNCT__
9058 #define __FUNCT__ "MatGetLocalSubMatrix"
9059 /*@
9060    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering
9061 
9062    Not Collective
9063 
9064    Input Arguments:
9065    mat - matrix to extract local submatrix from
9066    isrow - local row indices for submatrix
9067    iscol - local column indices for submatrix
9068 
9069    Output Arguments:
9070    submat - the submatrix
9071 
9072    Level: intermediate
9073 
9074    Notes:
9075    The submat should be returned with MatRestoreLocalSubMatrix().
9076 
9077    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
9078    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.
9079 
9080    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
9081    MatSetValuesBlockedLocal() will also be implemented.
9082 
9083 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef()
9084 @*/
9085 PetscErrorCode  MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9086 {
9087   PetscErrorCode ierr;
9088 
9089   PetscFunctionBegin;
9090   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9091   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9092   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9093   PetscCheckSameComm(isrow,2,iscol,3);
9094   PetscValidPointer(submat,4);
9095 
9096   if (mat->ops->getlocalsubmatrix) {
9097     ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9098   } else {
9099     ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr);
9100   }
9101   PetscFunctionReturn(0);
9102 }
9103 
9104 #undef __FUNCT__
9105 #define __FUNCT__ "MatRestoreLocalSubMatrix"
9106 /*@
9107    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering
9108 
9109    Not Collective
9110 
9111    Input Arguments:
9112    mat - matrix to extract local submatrix from
9113    isrow - local row indices for submatrix
9114    iscol - local column indices for submatrix
9115    submat - the submatrix
9116 
9117    Level: intermediate
9118 
9119 .seealso: MatGetLocalSubMatrix()
9120 @*/
9121 PetscErrorCode  MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9122 {
9123   PetscErrorCode ierr;
9124 
9125   PetscFunctionBegin;
9126   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9127   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9128   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9129   PetscCheckSameComm(isrow,2,iscol,3);
9130   PetscValidPointer(submat,4);
9131   if (*submat) {PetscValidHeaderSpecific(*submat,MAT_CLASSID,4);}
9132 
9133   if (mat->ops->restorelocalsubmatrix) {
9134     ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9135   } else {
9136     ierr = MatDestroy(submat);CHKERRQ(ierr);
9137   }
9138   *submat = PETSC_NULL;
9139   PetscFunctionReturn(0);
9140 }
9141 
9142 /* --------------------------------------------------------*/
9143 #undef __FUNCT__
9144 #define __FUNCT__ "MatFindZeroDiagonals"
9145 /*@
9146    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no entry in the matrix
9147 
9148    Collective on Mat
9149 
9150    Input Parameter:
9151 .  mat - the matrix
9152 
9153    Output Parameter:
9154 .  is - if any rows have zero diagonals this contains the list of them
9155 
9156    Level: developer
9157 
9158    Concepts: matrix-vector product
9159 
9160 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9161 @*/
9162 PetscErrorCode  MatFindZeroDiagonals(Mat mat,IS *is)
9163 {
9164   PetscErrorCode ierr;
9165 
9166   PetscFunctionBegin;
9167   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9168   PetscValidType(mat,1);
9169   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9170   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9171 
9172   if (!mat->ops->findzerodiagonals) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"This matrix type does not have a find zero diagonals defined");
9173   ierr = (*mat->ops->findzerodiagonals)(mat,is);CHKERRQ(ierr);
9174   PetscFunctionReturn(0);
9175 }
9176 
9177 #undef __FUNCT__
9178 #define __FUNCT__ "MatInvertBlockDiagonal"
9179 /*@C
9180   MatInvertBlockDiagonal - Inverts the block diagonal entries.
9181 
9182   Collective on Mat
9183 
9184   Input Parameters:
9185 . mat - the matrix
9186 
9187   Output Parameters:
9188 . values - the block inverses in column major order (FORTRAN-like)
9189 
9190    Note:
9191    This routine is not available from Fortran.
9192 
9193   Level: advanced
9194 @*/
9195 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
9196 {
9197   PetscErrorCode ierr;
9198 
9199   PetscFunctionBegin;
9200   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9201   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9202   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9203   if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
9204   ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr);
9205   PetscFunctionReturn(0);
9206 }
9207 
9208 #undef __FUNCT__
9209 #define __FUNCT__ "MatTransposeColoringDestroy"
9210 /*@C
9211     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
9212     via MatTransposeColoringCreate().
9213 
9214     Collective on MatTransposeColoring
9215 
9216     Input Parameter:
9217 .   c - coloring context
9218 
9219     Level: intermediate
9220 
9221 .seealso: MatTransposeColoringCreate()
9222 @*/
9223 PetscErrorCode  MatTransposeColoringDestroy(MatTransposeColoring *c)
9224 {
9225   PetscErrorCode       ierr;
9226   MatTransposeColoring matcolor=*c;
9227 
9228   PetscFunctionBegin;
9229   if (!matcolor) PetscFunctionReturn(0);
9230   if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);}
9231 
9232   ierr = PetscFree(matcolor->ncolumns);CHKERRQ(ierr);
9233   ierr = PetscFree(matcolor->nrows);CHKERRQ(ierr);
9234   ierr = PetscFree(matcolor->colorforrow);CHKERRQ(ierr);
9235   ierr = PetscFree2(matcolor->rows,matcolor->columnsforspidx);CHKERRQ(ierr);
9236   ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr);
9237   ierr = PetscFree(matcolor->columns);CHKERRQ(ierr);
9238   ierr = PetscHeaderDestroy(c);CHKERRQ(ierr);
9239   PetscFunctionReturn(0);
9240 }
9241 
9242 #undef __FUNCT__
9243 #define __FUNCT__ "MatTransColoringApplySpToDen"
9244 /*@C
9245     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
9246     a MatTransposeColoring context has been created, computes a dense B^T by Apply
9247     MatTransposeColoring to sparse B.
9248 
9249     Collective on MatTransposeColoring
9250 
9251     Input Parameters:
9252 +   B - sparse matrix B
9253 .   Btdense - symbolic dense matrix B^T
9254 -   coloring - coloring context created with MatTransposeColoringCreate()
9255 
9256     Output Parameter:
9257 .   Btdense - dense matrix B^T
9258 
9259     Options Database Keys:
9260 +    -mat_transpose_coloring_view - Activates basic viewing or coloring
9261 .    -mat_transpose_coloring_view_draw - Activates drawing of coloring
9262 -    -mat_transpose_coloring_view_info - Activates viewing of coloring info
9263 
9264     Level: intermediate
9265 
9266 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy()
9267 
9268 .keywords: coloring
9269 @*/
9270 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
9271 {
9272   PetscErrorCode ierr;
9273 
9274   PetscFunctionBegin;
9275   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
9276   PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2);
9277   PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3);
9278 
9279   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
9280   ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr);
9281   PetscFunctionReturn(0);
9282 }
9283 
9284 #undef __FUNCT__
9285 #define __FUNCT__ "MatTransColoringApplyDenToSp"
9286 /*@C
9287     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
9288     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
9289     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
9290     Csp from Cden.
9291 
9292     Collective on MatTransposeColoring
9293 
9294     Input Parameters:
9295 +   coloring - coloring context created with MatTransposeColoringCreate()
9296 -   Cden - matrix product of a sparse matrix and a dense matrix Btdense
9297 
9298     Output Parameter:
9299 .   Csp - sparse matrix
9300 
9301     Options Database Keys:
9302 +    -mat_multtranspose_coloring_view - Activates basic viewing or coloring
9303 .    -mat_multtranspose_coloring_view_draw - Activates drawing of coloring
9304 -    -mat_multtranspose_coloring_view_info - Activates viewing of coloring info
9305 
9306     Level: intermediate
9307 
9308 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()
9309 
9310 .keywords: coloring
9311 @*/
9312 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
9313 {
9314   PetscErrorCode ierr;
9315 
9316   PetscFunctionBegin;
9317   PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1);
9318   PetscValidHeaderSpecific(Cden,MAT_CLASSID,2);
9319   PetscValidHeaderSpecific(Csp,MAT_CLASSID,3);
9320 
9321   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
9322   ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr);
9323   PetscFunctionReturn(0);
9324 }
9325 
9326 #undef __FUNCT__
9327 #define __FUNCT__ "MatTransposeColoringCreate"
9328 /*@C
9329    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.
9330 
9331    Collective on Mat
9332 
9333    Input Parameters:
9334 +  mat - the matrix product C
9335 -  iscoloring - the coloring of the matrix; usually obtained with MatGetColoring() or DMCreateColoring()
9336 
9337     Output Parameter:
9338 .   color - the new coloring context
9339 
9340     Level: intermediate
9341 
9342 .seealso: MatTransposeColoringDestroy(), MatTransposeColoringSetFromOptions(), MatTransColoringApplySpToDen(),
9343            MatTransColoringApplyDen()ToSp, MatTransposeColoringView(),
9344 @*/
9345 PetscErrorCode  MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
9346 {
9347   MatTransposeColoring  c;
9348   MPI_Comm              comm;
9349   PetscErrorCode        ierr;
9350 
9351   PetscFunctionBegin;
9352   ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9353   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
9354   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);
9355 
9356   c->ctype = iscoloring->ctype;
9357   if (mat->ops->transposecoloringcreate) {
9358     ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr);
9359   } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Code not yet written for this matrix type");
9360 
9361   *color = c;
9362   ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9363   PetscFunctionReturn(0);
9364 }
9365