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