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