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