xref: /petsc/src/mat/interface/matrix.c (revision b8ee780938924f5b7405dfe16a29962cb98102f1)
1 /*$Id: matrix.c,v 1.389 2001/01/19 23:20:24 balay Exp bsmith $*/
2 
3 /*
4    This is where the abstract matrix operations are defined
5 */
6 
7 #include "src/mat/matimpl.h"        /*I "petscmat.h" I*/
8 #include "src/vec/vecimpl.h"
9 
10 #undef __FUNC__
11 #define __FUNC__ "MatGetRow"
12 /*@C
13    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
14    for each row that you get to ensure that your application does
15    not bleed memory.
16 
17    Not Collective
18 
19    Input Parameters:
20 +  mat - the matrix
21 -  row - the row to get
22 
23    Output Parameters:
24 +  ncols -  the number of nonzeros in the row
25 .  cols - if not NULL, the column numbers
26 -  vals - if not NULL, the values
27 
28    Notes:
29    This routine is provided for people who need to have direct access
30    to the structure of a matrix.  We hope that we provide enough
31    high-level matrix routines that few users will need it.
32 
33    MatGetRow() always returns 0-based column indices, regardless of
34    whether the internal representation is 0-based (default) or 1-based.
35 
36    For better efficiency, set cols and/or vals to PETSC_NULL if you do
37    not wish to extract these quantities.
38 
39    The user can only examine the values extracted with MatGetRow();
40    the values cannot be altered.  To change the matrix entries, one
41    must use MatSetValues().
42 
43    You can only have one call to MatGetRow() outstanding for a particular
44    matrix at a time, per processor. MatGetRow() can only obtained rows
45    associated with the given processor, it cannot get rows from the
46    other processors; for that we suggest using MatGetSubMatrices(), then
47    MatGetRow() on the submatrix. The row indix passed to MatGetRows()
48    is in the global number of rows.
49 
50    Fortran Notes:
51    The calling sequence from Fortran is
52 .vb
53    MatGetRow(matrix,row,ncols,cols,values,ierr)
54          Mat     matrix (input)
55          integer row    (input)
56          integer ncols  (output)
57          integer cols(maxcols) (output)
58          double precision (or double complex) values(maxcols) output
59 .ve
60    where maxcols >= maximum nonzeros in any row of the matrix.
61 
62    Caution:
63    Do not try to change the contents of the output arrays (cols and vals).
64    In some cases, this may corrupt the matrix.
65 
66    Level: advanced
67 
68    Concepts: matrices^row access
69 
70 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal()
71 @*/
72 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
73 {
74   int   ierr;
75 
76   PetscFunctionBegin;
77   PetscValidHeaderSpecific(mat,MAT_COOKIE);
78   PetscValidIntPointer(ncols);
79   PetscValidType(mat);
80   MatPreallocated(mat);
81   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
82   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
83   if (!mat->ops->getrow) SETERRQ(PETSC_ERR_SUP,"");
84   ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
85   ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
86   ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr);
87   PetscFunctionReturn(0);
88 }
89 
90 #undef __FUNC__
91 #define __FUNC__ "MatRestoreRow"
92 /*@C
93    MatRestoreRow - Frees any temporary space allocated by MatGetRow().
94 
95    Not Collective
96 
97    Input Parameters:
98 +  mat - the matrix
99 .  row - the row to get
100 .  ncols, cols - the number of nonzeros and their columns
101 -  vals - if nonzero the column values
102 
103    Notes:
104    This routine should be called after you have finished examining the entries.
105 
106    Fortran Notes:
107    The calling sequence from Fortran is
108 .vb
109    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
110       Mat     matrix (input)
111       integer row    (input)
112       integer ncols  (output)
113       integer cols(maxcols) (output)
114       double precision (or double complex) values(maxcols) output
115 .ve
116    Where maxcols >= maximum nonzeros in any row of the matrix.
117 
118    In Fortran MatRestoreRow() MUST be called after MatGetRow()
119    before another call to MatGetRow() can be made.
120 
121    Level: advanced
122 
123 .seealso:  MatGetRow()
124 @*/
125 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals)
126 {
127   int ierr;
128 
129   PetscFunctionBegin;
130   PetscValidHeaderSpecific(mat,MAT_COOKIE);
131   PetscValidIntPointer(ncols);
132   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
133   if (!mat->ops->restorerow) PetscFunctionReturn(0);
134   ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr);
135   PetscFunctionReturn(0);
136 }
137 
138 #undef __FUNC__
139 #define __FUNC__ "MatView"
140 /*@C
141    MatView - Visualizes a matrix object.
142 
143    Collective on Mat
144 
145    Input Parameters:
146 +  mat - the matrix
147 -  ptr - visualization context
148 
149   Notes:
150   The available visualization contexts include
151 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
152 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
153         output where only the first processor opens
154         the file.  All other processors send their
155         data to the first processor to print.
156 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
157 
158    The user can open alternative visualization contexts with
159 +    PetscViewerASCIIOpen() - Outputs matrix to a specified file
160 .    PetscViewerBinaryOpen() - Outputs matrix in binary to a
161          specified file; corresponding input uses MatLoad()
162 .    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
163          an X window display
164 -    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
165          Currently only the sequential dense and AIJ
166          matrix types support the Socket viewer.
167 
168    The user can call PetscViewerSetFormat() to specify the output
169    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
170    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
171 +    PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents
172 .    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
173 .    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
174 .    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
175          format common among all matrix types
176 .    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
177          format (which is in many cases the same as the default)
178 .    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
179          size and structure (not the matrix entries)
180 -    PETSC_VIEWER_ASCII_INFO_LONG - prints more detailed information about
181          the matrix structure
182 
183    Level: beginner
184 
185    Concepts: matrices^viewing
186    Concepts: matrices^plotting
187    Concepts: matrices^printing
188 
189 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
190           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
191 @*/
192 int MatView(Mat mat,PetscViewer viewer)
193 {
194   int               ierr,rows,cols;
195   PetscTruth        isascii;
196   char              *cstr;
197   PetscViewerFormat format;
198 
199   PetscFunctionBegin;
200   PetscValidHeaderSpecific(mat,MAT_COOKIE);
201   PetscValidType(mat);
202   MatPreallocated(mat);
203   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mat->comm);
204   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
205   PetscCheckSameComm(mat,viewer);
206   if (!mat->assembled) SETERRQ(1,"Must call MatAssemblyBegin/End() before viewing matrix");
207 
208   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
209   if (isascii) {
210     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
211     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) {
212       ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr);
213       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
214       ierr = MatGetType(mat,&cstr);CHKERRQ(ierr);
215       ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
216       ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr);
217       if (mat->ops->getinfo) {
218         MatInfo info;
219         ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr);
220         ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n",
221                           (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr);
222       }
223     }
224   }
225   if (mat->ops->view) {
226     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
227     ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);
228     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
229   } else if (!isascii) {
230     SETERRQ1(1,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
231   }
232   if (isascii) {
233     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
234     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) {
235       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
236     }
237   }
238   PetscFunctionReturn(0);
239 }
240 
241 #undef __FUNC__
242 #define __FUNC__ "MatScaleSystem"
243 /*@C
244    MatScaleSystem - Scale a vector solution and right hand side to
245    match the scaling of a scaled matrix.
246 
247    Collective on Mat
248 
249    Input Parameter:
250 +  mat - the matrix
251 .  x - solution vector (or PETSC_NULL)
252 +  b - right hand side vector (or PETSC_NULL)
253 
254 
255    Notes:
256    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
257    internally scaled, so this does nothing. For MPIROWBS it
258    permutes and diagonally scales.
259 
260    The SLES methods automatically call this routine when required
261    (via PCPreSolve()) so it is rarely used directly.
262 
263    Level: Developer
264 
265    Concepts: matrices^scaling
266 
267 .seealso: MatUseScaledForm(), MatUnScaleSystem()
268 @*/
269 int MatScaleSystem(Mat mat,Vec x,Vec b)
270 {
271   int ierr;
272 
273   PetscFunctionBegin;
274   PetscValidHeaderSpecific(mat,MAT_COOKIE);
275   PetscValidType(mat);
276   MatPreallocated(mat);
277   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
278   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);}
279 
280   if (mat->ops->scalesystem) {
281     ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr);
282   }
283   PetscFunctionReturn(0);
284 }
285 
286 #undef __FUNC__
287 #define __FUNC__ "MatUnScaleSystem"
288 /*@C
289    MatUnScaleSystem - Unscales a vector solution and right hand side to
290    match the original scaling of a scaled matrix.
291 
292    Collective on Mat
293 
294    Input Parameter:
295 +  mat - the matrix
296 .  x - solution vector (or PETSC_NULL)
297 +  b - right hand side vector (or PETSC_NULL)
298 
299 
300    Notes:
301    For AIJ, BAIJ, and BDiag matrix formats, the matrices are not
302    internally scaled, so this does nothing. For MPIROWBS it
303    permutes and diagonally scales.
304 
305    The SLES methods automatically call this routine when required
306    (via PCPreSolve()) so it is rarely used directly.
307 
308    Level: Developer
309 
310 .seealso: MatUseScaledForm(), MatScaleSystem()
311 @*/
312 int MatUnScaleSystem(Mat mat,Vec x,Vec b)
313 {
314   int ierr;
315 
316   PetscFunctionBegin;
317   PetscValidHeaderSpecific(mat,MAT_COOKIE);
318   PetscValidType(mat);
319   MatPreallocated(mat);
320   if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);}
321   if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);}
322   if (mat->ops->unscalesystem) {
323     ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr);
324   }
325   PetscFunctionReturn(0);
326 }
327 
328 #undef __FUNC__
329 #define __FUNC__ "MatUseScaledForm"
330 /*@C
331    MatUseScaledForm - For matrix storage formats that scale the
332    matrix (for example MPIRowBS matrices are diagonally scaled on
333    assembly) indicates matrix operations (MatMult() etc) are
334    applied using the scaled matrix.
335 
336    Collective on Mat
337 
338    Input Parameter:
339 +  mat - the matrix
340 -  scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for
341             applying the original matrix
342 
343    Notes:
344    For scaled matrix formats, applying the original, unscaled matrix
345    will be slightly more expensive
346 
347    Level: Developer
348 
349 .seealso: MatScaleSystem(), MatUnScaleSystem()
350 @*/
351 int MatUseScaledForm(Mat mat,PetscTruth scaled)
352 {
353   int ierr;
354 
355   PetscFunctionBegin;
356   PetscValidHeaderSpecific(mat,MAT_COOKIE);
357   PetscValidType(mat);
358   MatPreallocated(mat);
359   if (mat->ops->usescaledform) {
360     ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr);
361   }
362   PetscFunctionReturn(0);
363 }
364 
365 #undef __FUNC__
366 #define __FUNC__ "MatDestroy"
367 /*@C
368    MatDestroy - Frees space taken by a matrix.
369 
370    Collective on Mat
371 
372    Input Parameter:
373 .  A - the matrix
374 
375    Level: beginner
376 
377 @*/
378 int MatDestroy(Mat A)
379 {
380   int ierr;
381 
382   PetscFunctionBegin;
383   PetscValidHeaderSpecific(A,MAT_COOKIE);
384   PetscValidType(A);
385   MatPreallocated(A);
386   if (--A->refct > 0) PetscFunctionReturn(0);
387 
388   /* if memory was published with AMS then destroy it */
389   ierr = PetscObjectDepublish(A);CHKERRQ(ierr);
390   if (A->mapping) {
391     ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr);
392   }
393   if (A->bmapping) {
394     ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr);
395   }
396   if (A->rmap) {
397     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
398   }
399   if (A->cmap) {
400     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
401   }
402 
403   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
404   PetscLogObjectDestroy(A);
405   PetscHeaderDestroy(A);
406   PetscFunctionReturn(0);
407 }
408 
409 #undef __FUNC__
410 #define __FUNC__ "MatValid"
411 /*@
412    MatValid - Checks whether a matrix object is valid.
413 
414    Collective on Mat
415 
416    Input Parameter:
417 .  m - the matrix to check
418 
419    Output Parameter:
420    flg - flag indicating matrix status, either
421    PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise.
422 
423    Level: developer
424 
425    Concepts: matrices^validity
426 @*/
427 int MatValid(Mat m,PetscTruth *flg)
428 {
429   PetscFunctionBegin;
430   PetscValidIntPointer(flg);
431   if (!m)                           *flg = PETSC_FALSE;
432   else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE;
433   else                              *flg = PETSC_TRUE;
434   PetscFunctionReturn(0);
435 }
436 
437 #undef __FUNC__
438 #define __FUNC__ "MatSetValues"
439 /*@
440    MatSetValues - Inserts or adds a block of values into a matrix.
441    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
442    MUST be called after all calls to MatSetValues() have been completed.
443 
444    Not Collective
445 
446    Input Parameters:
447 +  mat - the matrix
448 .  v - a logically two-dimensional array of values
449 .  m, idxm - the number of rows and their global indices
450 .  n, idxn - the number of columns and their global indices
451 -  addv - either ADD_VALUES or INSERT_VALUES, where
452    ADD_VALUES adds values to any existing entries, and
453    INSERT_VALUES replaces existing entries with new values
454 
455    Notes:
456    By default the values, v, are row-oriented and unsorted.
457    See MatSetOption() for other options.
458 
459    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
460    options cannot be mixed without intervening calls to the assembly
461    routines.
462 
463    MatSetValues() uses 0-based row and column numbers in Fortran
464    as well as in C.
465 
466    Negative indices may be passed in idxm and idxn, these rows and columns are
467    simply ignored. This allows easily inserting element stiffness matrices
468    with homogeneous Dirchlet boundary conditions that you don't want represented
469    in the matrix.
470 
471    Efficiency Alert:
472    The routine MatSetValuesBlocked() may offer much better efficiency
473    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).
474 
475    Level: beginner
476 
477    Concepts: matrices^putting entries in
478 
479 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
480 @*/
481 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
482 {
483   int ierr;
484 
485   PetscFunctionBegin;
486   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
487   PetscValidHeaderSpecific(mat,MAT_COOKIE);
488   PetscValidType(mat);
489   MatPreallocated(mat);
490   PetscValidIntPointer(idxm);
491   PetscValidIntPointer(idxn);
492   PetscValidScalarPointer(v);
493   if (mat->insertmode == NOT_SET_VALUES) {
494     mat->insertmode = addv;
495   }
496 #if defined(PETSC_USE_BOPT_g)
497   else if (mat->insertmode != addv) {
498     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
499   }
500   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
501 #endif
502 
503   if (mat->assembled) {
504     mat->was_assembled = PETSC_TRUE;
505     mat->assembled     = PETSC_FALSE;
506   }
507   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
508   if (!mat->ops->setvalues) SETERRQ(PETSC_ERR_SUP,"Not supported for this matrix type");
509   ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
510   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
511   PetscFunctionReturn(0);
512 }
513 
514 #undef __FUNC__
515 #define __FUNC__ "MatSetValuesBlocked"
516 /*@
517    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.
518 
519    Not Collective
520 
521    Input Parameters:
522 +  mat - the matrix
523 .  v - a logically two-dimensional array of values
524 .  m, idxm - the number of block rows and their global block indices
525 .  n, idxn - the number of block columns and their global block indices
526 -  addv - either ADD_VALUES or INSERT_VALUES, where
527    ADD_VALUES adds values to any existing entries, and
528    INSERT_VALUES replaces existing entries with new values
529 
530    Notes:
531    By default the values, v, are row-oriented and unsorted. So the layout of
532    v is the same as for MatSetValues(). See MatSetOption() for other options.
533 
534    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
535    options cannot be mixed without intervening calls to the assembly
536    routines.
537 
538    MatSetValuesBlocked() uses 0-based row and column numbers in Fortran
539    as well as in C.
540 
541    Negative indices may be passed in idxm and idxn, these rows and columns are
542    simply ignored. This allows easily inserting element stiffness matrices
543    with homogeneous Dirchlet boundary conditions that you don't want represented
544    in the matrix.
545 
546    Each time an entry is set within a sparse matrix via MatSetValues(),
547    internal searching must be done to determine where to place the the
548    data in the matrix storage space.  By instead inserting blocks of
549    entries via MatSetValuesBlocked(), the overhead of matrix assembly is
550    reduced.
551 
552    Restrictions:
553    MatSetValuesBlocked() is currently supported only for the block AIJ
554    matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via
555    MatCreateSeqBAIJ() and MatCreateMPIBAIJ()).
556 
557    Level: intermediate
558 
559    Concepts: matrices^putting entries in blocked
560 
561 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal()
562 @*/
563 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv)
564 {
565   int ierr;
566 
567   PetscFunctionBegin;
568   if (!m || !n) PetscFunctionReturn(0); /* no values to insert */
569   PetscValidHeaderSpecific(mat,MAT_COOKIE);
570   PetscValidType(mat);
571   MatPreallocated(mat);
572   PetscValidIntPointer(idxm);
573   PetscValidIntPointer(idxn);
574   PetscValidScalarPointer(v);
575   if (mat->insertmode == NOT_SET_VALUES) {
576     mat->insertmode = addv;
577   }
578 #if defined(PETSC_USE_BOPT_g)
579   else if (mat->insertmode != addv) {
580     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
581   }
582   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
583 #endif
584 
585   if (mat->assembled) {
586     mat->was_assembled = PETSC_TRUE;
587     mat->assembled     = PETSC_FALSE;
588   }
589   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
590   if (!mat->ops->setvaluesblocked) SETERRQ(PETSC_ERR_SUP,"Not supported for this matrix type");
591   ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr);
592   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
593   PetscFunctionReturn(0);
594 }
595 
596 /*MC
597    MatSetValue - Set a single entry into a matrix.
598 
599    Synopsis:
600    void MatSetValue(Mat m,int row,int col,Scalar value,InsertMode mode);
601 
602    Not collective
603 
604    Input Parameters:
605 +  m - the matrix
606 .  row - the row location of the entry
607 .  col - the column location of the entry
608 .  value - the value to insert
609 -  mode - either INSERT_VALUES or ADD_VALUES
610 
611    Notes:
612    For efficiency one should use MatSetValues() and set several or many
613    values simultaneously if possible.
614 
615    Note that VecSetValue() does NOT return an error code (since this
616    is checked internally).
617 
618    Level: beginner
619 
620 .seealso: MatSetValues()
621 M*/
622 
623 #undef __FUNC__
624 #define __FUNC__ "MatGetValues"
625 /*@
626    MatGetValues - Gets a block of values from a matrix.
627 
628    Not Collective; currently only returns a local block
629 
630    Input Parameters:
631 +  mat - the matrix
632 .  v - a logically two-dimensional array for storing the values
633 .  m, idxm - the number of rows and their global indices
634 -  n, idxn - the number of columns and their global indices
635 
636    Notes:
637    The user must allocate space (m*n Scalars) for the values, v.
638    The values, v, are then returned in a row-oriented format,
639    analogous to that used by default in MatSetValues().
640 
641    MatGetValues() uses 0-based row and column numbers in
642    Fortran as well as in C.
643 
644    MatGetValues() requires that the matrix has been assembled
645    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
646    MatSetValues() and MatGetValues() CANNOT be made in succession
647    without intermediate matrix assembly.
648 
649    Level: advanced
650 
651    Concepts: matrices^accessing values
652 
653 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues()
654 @*/
655 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
656 {
657   int ierr;
658 
659   PetscFunctionBegin;
660   PetscValidHeaderSpecific(mat,MAT_COOKIE);
661   PetscValidType(mat);
662   MatPreallocated(mat);
663   PetscValidIntPointer(idxm);
664   PetscValidIntPointer(idxn);
665   PetscValidScalarPointer(v);
666   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
667   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
668   if (!mat->ops->getvalues) SETERRQ(PETSC_ERR_SUP,"");
669 
670   ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
671   ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr);
672   ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr);
673   PetscFunctionReturn(0);
674 }
675 
676 #undef __FUNC__
677 #define __FUNC__ "MatSetLocalToGlobalMapping"
678 /*@
679    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
680    the routine MatSetValuesLocal() to allow users to insert matrix entries
681    using a local (per-processor) numbering.
682 
683    Not Collective
684 
685    Input Parameters:
686 +  x - the matrix
687 -  mapping - mapping created with ISLocalToGlobalMappingCreate()
688              or ISLocalToGlobalMappingCreateIS()
689 
690    Level: intermediate
691 
692    Concepts: matrices^local to global mapping
693    Concepts: local to global mapping^for matrices
694 
695 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
696 @*/
697 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping)
698 {
699   int ierr;
700   PetscFunctionBegin;
701   PetscValidHeaderSpecific(x,MAT_COOKIE);
702   PetscValidType(x);
703   MatPreallocated(x);
704   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
705   if (x->mapping) {
706     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
707   }
708 
709   if (x->ops->setlocaltoglobalmapping) {
710     ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr);
711   } else {
712     x->mapping = mapping;
713     ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
714   }
715   PetscFunctionReturn(0);
716 }
717 
718 #undef __FUNC__
719 #define __FUNC__ "MatSetLocalToGlobalMappingBlock"
720 /*@
721    MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use
722    by the routine MatSetValuesBlockedLocal() to allow users to insert matrix
723    entries using a local (per-processor) numbering.
724 
725    Not Collective
726 
727    Input Parameters:
728 +  x - the matrix
729 -  mapping - mapping created with ISLocalToGlobalMappingCreate() or
730              ISLocalToGlobalMappingCreateIS()
731 
732    Level: intermediate
733 
734    Concepts: matrices^local to global mapping blocked
735    Concepts: local to global mapping^for matrices, blocked
736 
737 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(),
738            MatSetValuesBlocked(), MatSetValuesLocal()
739 @*/
740 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping)
741 {
742   int ierr;
743   PetscFunctionBegin;
744   PetscValidHeaderSpecific(x,MAT_COOKIE);
745   PetscValidType(x);
746   MatPreallocated(x);
747   PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE);
748   if (x->bmapping) {
749     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix");
750   }
751 
752   x->bmapping = mapping;
753   ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr);
754   PetscFunctionReturn(0);
755 }
756 
757 #undef __FUNC__
758 #define __FUNC__ "MatSetValuesLocal"
759 /*@
760    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
761    using a local ordering of the nodes.
762 
763    Not Collective
764 
765    Input Parameters:
766 +  x - the matrix
767 .  nrow, irow - number of rows and their local indices
768 .  ncol, icol - number of columns and their local indices
769 .  y -  a logically two-dimensional array of values
770 -  addv - either INSERT_VALUES or ADD_VALUES, where
771    ADD_VALUES adds values to any existing entries, and
772    INSERT_VALUES replaces existing entries with new values
773 
774    Notes:
775    Before calling MatSetValuesLocal(), the user must first set the
776    local-to-global mapping by calling MatSetLocalToGlobalMapping().
777 
778    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
779    options cannot be mixed without intervening calls to the assembly
780    routines.
781 
782    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
783    MUST be called after all calls to MatSetValuesLocal() have been completed.
784 
785    Level: intermediate
786 
787    Concepts: matrices^putting entries in with local numbering
788 
789 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
790            MatSetValueLocal()
791 @*/
792 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv)
793 {
794   int ierr,irowm[2048],icolm[2048];
795 
796   PetscFunctionBegin;
797   PetscValidHeaderSpecific(mat,MAT_COOKIE);
798   PetscValidType(mat);
799   MatPreallocated(mat);
800   PetscValidIntPointer(irow);
801   PetscValidIntPointer(icol);
802   PetscValidScalarPointer(y);
803 
804   if (mat->insertmode == NOT_SET_VALUES) {
805     mat->insertmode = addv;
806   }
807 #if defined(PETSC_USE_BOPT_g)
808   else if (mat->insertmode != addv) {
809     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
810   }
811   if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) {
812     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
813   }
814   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
815 #endif
816 
817   if (mat->assembled) {
818     mat->was_assembled = PETSC_TRUE;
819     mat->assembled     = PETSC_FALSE;
820   }
821   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
822   if (!mat->ops->setvalueslocal) {
823     ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr);
824     ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr);
825     ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
826   } else {
827     ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr);
828   }
829   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
830   PetscFunctionReturn(0);
831 }
832 
833 #undef __FUNC__
834 #define __FUNC__ "MatSetValuesBlockedLocal"
835 /*@
836    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
837    using a local ordering of the nodes a block at a time.
838 
839    Not Collective
840 
841    Input Parameters:
842 +  x - the matrix
843 .  nrow, irow - number of rows and their local indices
844 .  ncol, icol - number of columns and their local indices
845 .  y -  a logically two-dimensional array of values
846 -  addv - either INSERT_VALUES or ADD_VALUES, where
847    ADD_VALUES adds values to any existing entries, and
848    INSERT_VALUES replaces existing entries with new values
849 
850    Notes:
851    Before calling MatSetValuesBlockedLocal(), the user must first set the
852    local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(),
853    where the mapping MUST be set for matrix blocks, not for matrix elements.
854 
855    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
856    options cannot be mixed without intervening calls to the assembly
857    routines.
858 
859    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
860    MUST be called after all calls to MatSetValuesBlockedLocal() have been completed.
861 
862    Level: intermediate
863 
864    Concepts: matrices^putting blocked values in with local numbering
865 
866 .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked()
867 @*/
868 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv)
869 {
870   int ierr,irowm[2048],icolm[2048];
871 
872   PetscFunctionBegin;
873   PetscValidHeaderSpecific(mat,MAT_COOKIE);
874   PetscValidType(mat);
875   MatPreallocated(mat);
876   PetscValidIntPointer(irow);
877   PetscValidIntPointer(icol);
878   PetscValidScalarPointer(y);
879   if (mat->insertmode == NOT_SET_VALUES) {
880     mat->insertmode = addv;
881   }
882 #if defined(PETSC_USE_BOPT_g)
883   else if (mat->insertmode != addv) {
884     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
885   }
886   if (!mat->bmapping) {
887     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()");
888   }
889   if (nrow > 2048 || ncol > 2048) {
890     SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol);
891   }
892   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
893 #endif
894 
895   if (mat->assembled) {
896     mat->was_assembled = PETSC_TRUE;
897     mat->assembled     = PETSC_FALSE;
898   }
899   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
900   ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr);
901   ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr);
902   ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr);
903   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
904   PetscFunctionReturn(0);
905 }
906 
907 /* --------------------------------------------------------*/
908 #undef __FUNC__
909 #define __FUNC__ "MatMult"
910 /*@
911    MatMult - Computes the matrix-vector product, y = Ax.
912 
913    Collective on Mat and Vec
914 
915    Input Parameters:
916 +  mat - the matrix
917 -  x   - the vector to be multilplied
918 
919    Output Parameters:
920 .  y - the result
921 
922    Notes:
923    The vectors x and y cannot be the same.  I.e., one cannot
924    call MatMult(A,y,y).
925 
926    Level: beginner
927 
928    Concepts: matrix-vector product
929 
930 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
931 @*/
932 int MatMult(Mat mat,Vec x,Vec y)
933 {
934   int ierr;
935 
936   PetscFunctionBegin;
937   PetscValidHeaderSpecific(mat,MAT_COOKIE);
938   PetscValidType(mat);
939   MatPreallocated(mat);
940   PetscValidHeaderSpecific(x,VEC_COOKIE);
941   PetscValidHeaderSpecific(y,VEC_COOKIE);
942 
943   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
944   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
945   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
946   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
947   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
948   if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n);
949 
950   if (mat->nullsp) {
951     ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr);
952   }
953 
954   ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
955   ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr);
956   ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr);
957 
958   if (mat->nullsp) {
959     ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr);
960   }
961   PetscFunctionReturn(0);
962 }
963 
964 #undef __FUNC__
965 #define __FUNC__ "MatMultTranspose"
966 /*@
967    MatMultTranspose - Computes matrix transpose times a vector.
968 
969    Collective on Mat and Vec
970 
971    Input Parameters:
972 +  mat - the matrix
973 -  x   - the vector to be multilplied
974 
975    Output Parameters:
976 .  y - the result
977 
978    Notes:
979    The vectors x and y cannot be the same.  I.e., one cannot
980    call MatMultTranspose(A,y,y).
981 
982    Level: beginner
983 
984    Concepts: matrix vector product^transpose
985 
986 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd()
987 @*/
988 int MatMultTranspose(Mat mat,Vec x,Vec y)
989 {
990   int ierr;
991 
992   PetscFunctionBegin;
993   PetscValidHeaderSpecific(mat,MAT_COOKIE);
994   PetscValidType(mat);
995   MatPreallocated(mat);
996   PetscValidHeaderSpecific(x,VEC_COOKIE);
997   PetscValidHeaderSpecific(y,VEC_COOKIE);
998 
999   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1000   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1001   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1002   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1003   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
1004 
1005   ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1006   ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr);
1007   ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1008   PetscFunctionReturn(0);
1009 }
1010 
1011 #undef __FUNC__
1012 #define __FUNC__ "MatMultAdd"
1013 /*@
1014     MatMultAdd -  Computes v3 = v2 + A * v1.
1015 
1016     Collective on Mat and Vec
1017 
1018     Input Parameters:
1019 +   mat - the matrix
1020 -   v1, v2 - the vectors
1021 
1022     Output Parameters:
1023 .   v3 - the result
1024 
1025     Notes:
1026     The vectors v1 and v3 cannot be the same.  I.e., one cannot
1027     call MatMultAdd(A,v1,v2,v1).
1028 
1029     Level: beginner
1030 
1031     Concepts: matrix vector product^addition
1032 
1033 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
1034 @*/
1035 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1036 {
1037   int ierr;
1038 
1039   PetscFunctionBegin;
1040   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1041   PetscValidType(mat);
1042   MatPreallocated(mat);
1043   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1044   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1045   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1046 
1047   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1048   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1049   if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N);
1050   if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N);
1051   if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N);
1052   if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n);
1053   if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n);
1054   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1055 
1056   ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1057   ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1058   ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1059   PetscFunctionReturn(0);
1060 }
1061 
1062 #undef __FUNC__
1063 #define __FUNC__ "MatMultTransposeAdd"
1064 /*@
1065    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.
1066 
1067    Collective on Mat and Vec
1068 
1069    Input Parameters:
1070 +  mat - the matrix
1071 -  v1, v2 - the vectors
1072 
1073    Output Parameters:
1074 .  v3 - the result
1075 
1076    Notes:
1077    The vectors v1 and v3 cannot be the same.  I.e., one cannot
1078    call MatMultTransposeAdd(A,v1,v2,v1).
1079 
1080    Level: beginner
1081 
1082    Concepts: matrix vector product^transpose and addition
1083 
1084 .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
1085 @*/
1086 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1087 {
1088   int ierr;
1089 
1090   PetscFunctionBegin;
1091   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1092   PetscValidType(mat);
1093   MatPreallocated(mat);
1094   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1095   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1096   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1097 
1098   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1099   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1100   if (!mat->ops->multtransposeadd) SETERRQ(PETSC_ERR_SUP,"");
1101   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1102   if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N);
1103   if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N);
1104   if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N);
1105 
1106   ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1107   ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1108   ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1109   PetscFunctionReturn(0);
1110 }
1111 /* ------------------------------------------------------------*/
1112 #undef __FUNC__
1113 #define __FUNC__ "MatGetInfo"
1114 /*@C
1115    MatGetInfo - Returns information about matrix storage (number of
1116    nonzeros, memory, etc.).
1117 
1118    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used
1119    as the flag
1120 
1121    Input Parameters:
1122 .  mat - the matrix
1123 
1124    Output Parameters:
1125 +  flag - flag indicating the type of parameters to be returned
1126    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
1127    MAT_GLOBAL_SUM - sum over all processors)
1128 -  info - matrix information context
1129 
1130    Notes:
1131    The MatInfo context contains a variety of matrix data, including
1132    number of nonzeros allocated and used, number of mallocs during
1133    matrix assembly, etc.  Additional information for factored matrices
1134    is provided (such as the fill ratio, number of mallocs during
1135    factorization, etc.).  Much of this info is printed to STDOUT
1136    when using the runtime options
1137 $       -log_info -mat_view_info
1138 
1139    Example for C/C++ Users:
1140    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
1141    data within the MatInfo context.  For example,
1142 .vb
1143       MatInfo info;
1144       Mat     A;
1145       double  mal, nz_a, nz_u;
1146 
1147       MatGetInfo(A,MAT_LOCAL,&info);
1148       mal  = info.mallocs;
1149       nz_a = info.nz_allocated;
1150 .ve
1151 
1152    Example for Fortran Users:
1153    Fortran users should declare info as a double precision
1154    array of dimension MAT_INFO_SIZE, and then extract the parameters
1155    of interest.  See the file ${PETSC_DIR}/include/finclude/petscmat.h
1156    a complete list of parameter names.
1157 .vb
1158       double  precision info(MAT_INFO_SIZE)
1159       double  precision mal, nz_a
1160       Mat     A
1161       integer ierr
1162 
1163       call MatGetInfo(A,MAT_LOCAL,info,ierr)
1164       mal = info(MAT_INFO_MALLOCS)
1165       nz_a = info(MAT_INFO_NZ_ALLOCATED)
1166 .ve
1167 
1168     Level: intermediate
1169 
1170     Concepts: matrices^getting information on
1171 
1172 @*/
1173 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
1174 {
1175   int ierr;
1176 
1177   PetscFunctionBegin;
1178   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1179   PetscValidType(mat);
1180   MatPreallocated(mat);
1181   PetscValidPointer(info);
1182   if (!mat->ops->getinfo) SETERRQ(PETSC_ERR_SUP,"");
1183   ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr);
1184   PetscFunctionReturn(0);
1185 }
1186 
1187 /* ----------------------------------------------------------*/
1188 #undef __FUNC__
1189 #define __FUNC__ "MatILUDTFactor"
1190 /*@C
1191    MatILUDTFactor - Performs a drop tolerance ILU factorization.
1192 
1193    Collective on Mat
1194 
1195    Input Parameters:
1196 +  mat - the matrix
1197 .  info - information about the factorization to be done
1198 .  row - row permutation
1199 -  col - column permutation
1200 
1201    Output Parameters:
1202 .  fact - the factored matrix
1203 
1204    Level: developer
1205 
1206    Notes:
1207    Most users should employ the simplified SLES interface for linear solvers
1208    instead of working directly with matrix algebra routines such as this.
1209    See, e.g., SLESCreate().
1210 
1211    This is currently only supported for the SeqAIJ matrix format using code
1212    from Yousef Saad's SPARSEKIT2  package (translated to C with f2c) and/or
1213    Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright
1214    and thus can be distributed with PETSc.
1215 
1216     Concepts: matrices^ILUDT factorization
1217 
1218 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1219 @*/
1220 int MatILUDTFactor(Mat mat,MatILUInfo *info,IS row,IS col,Mat *fact)
1221 {
1222   int ierr;
1223 
1224   PetscFunctionBegin;
1225   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1226   PetscValidType(mat);
1227   MatPreallocated(mat);
1228   PetscValidPointer(fact);
1229   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1230   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1231   if (!mat->ops->iludtfactor) SETERRQ(PETSC_ERR_SUP,"");
1232 
1233   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1234   ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr);
1235   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1236 
1237   PetscFunctionReturn(0);
1238 }
1239 
1240 #undef __FUNC__
1241 #define __FUNC__ "MatLUFactor"
1242 /*@
1243    MatLUFactor - Performs in-place LU factorization of matrix.
1244 
1245    Collective on Mat
1246 
1247    Input Parameters:
1248 +  mat - the matrix
1249 .  row - row permutation
1250 .  col - column permutation
1251 -  info - options for factorization, includes
1252 $          fill - expected fill as ratio of original fill.
1253 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1254 $                   Run with the option -log_info to determine an optimal value to use
1255 
1256    Notes:
1257    Most users should employ the simplified SLES interface for linear solvers
1258    instead of working directly with matrix algebra routines such as this.
1259    See, e.g., SLESCreate().
1260 
1261    This changes the state of the matrix to a factored matrix; it cannot be used
1262    for example with MatSetValues() unless one first calls MatSetUnfactored().
1263 
1264    Level: developer
1265 
1266    Concepts: matrices^LU factorization
1267 
1268 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
1269           MatGetOrdering(), MatSetUnfactored()
1270 
1271 @*/
1272 int MatLUFactor(Mat mat,IS row,IS col,MatLUInfo *info)
1273 {
1274   int ierr;
1275 
1276   PetscFunctionBegin;
1277   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1278   PetscValidType(mat);
1279   MatPreallocated(mat);
1280   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1281   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1282   if (!mat->ops->lufactor) SETERRQ(PETSC_ERR_SUP,"");
1283 
1284   ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1285   ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr);
1286   ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1287   PetscFunctionReturn(0);
1288 }
1289 
1290 #undef __FUNC__
1291 #define __FUNC__ "MatILUFactor"
1292 /*@
1293    MatILUFactor - Performs in-place ILU factorization of matrix.
1294 
1295    Collective on Mat
1296 
1297    Input Parameters:
1298 +  mat - the matrix
1299 .  row - row permutation
1300 .  col - column permutation
1301 -  info - structure containing
1302 $      levels - number of levels of fill.
1303 $      expected fill - as ratio of original fill.
1304 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
1305                 missing diagonal entries)
1306 
1307    Notes:
1308    Probably really in-place only when level of fill is zero, otherwise allocates
1309    new space to store factored matrix and deletes previous memory.
1310 
1311    Most users should employ the simplified SLES interface for linear solvers
1312    instead of working directly with matrix algebra routines such as this.
1313    See, e.g., SLESCreate().
1314 
1315    Level: developer
1316 
1317    Concepts: matrices^ILU factorization
1318 
1319 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
1320 @*/
1321 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info)
1322 {
1323   int ierr;
1324 
1325   PetscFunctionBegin;
1326   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1327   PetscValidType(mat);
1328   MatPreallocated(mat);
1329   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
1330   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1331   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1332   if (!mat->ops->ilufactor) SETERRQ(PETSC_ERR_SUP,"");
1333 
1334   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1335   ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr);
1336   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1337   PetscFunctionReturn(0);
1338 }
1339 
1340 #undef __FUNC__
1341 #define __FUNC__ "MatLUFactorSymbolic"
1342 /*@
1343    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
1344    Call this routine before calling MatLUFactorNumeric().
1345 
1346    Collective on Mat
1347 
1348    Input Parameters:
1349 +  mat - the matrix
1350 .  row, col - row and column permutations
1351 -  info - options for factorization, includes
1352 $          fill - expected fill as ratio of original fill.
1353 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1354 $                   Run with the option -log_info to determine an optimal value to use
1355 
1356    Output Parameter:
1357 .  fact - new matrix that has been symbolically factored
1358 
1359    Notes:
1360    See the users manual for additional information about
1361    choosing the fill factor for better efficiency.
1362 
1363    Most users should employ the simplified SLES interface for linear solvers
1364    instead of working directly with matrix algebra routines such as this.
1365    See, e.g., SLESCreate().
1366 
1367    Level: developer
1368 
1369    Concepts: matrices^LU symbolic factorization
1370 
1371 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor()
1372 @*/
1373 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatLUInfo *info,Mat *fact)
1374 {
1375   int ierr;
1376 
1377   PetscFunctionBegin;
1378   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1379   PetscValidType(mat);
1380   MatPreallocated(mat);
1381   PetscValidPointer(fact);
1382   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1383   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1384   if (!mat->ops->lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"");
1385 
1386   ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1387   ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
1388   ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1389   PetscFunctionReturn(0);
1390 }
1391 
1392 #undef __FUNC__
1393 #define __FUNC__ "MatLUFactorNumeric"
1394 /*@
1395    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
1396    Call this routine after first calling MatLUFactorSymbolic().
1397 
1398    Collective on Mat
1399 
1400    Input Parameters:
1401 +  mat - the matrix
1402 -  fact - the matrix generated for the factor, from MatLUFactorSymbolic()
1403 
1404    Notes:
1405    See MatLUFactor() for in-place factorization.  See
1406    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
1407 
1408    Most users should employ the simplified SLES interface for linear solvers
1409    instead of working directly with matrix algebra routines such as this.
1410    See, e.g., SLESCreate().
1411 
1412    Level: developer
1413 
1414    Concepts: matrices^LU numeric factorization
1415 
1416 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
1417 @*/
1418 int MatLUFactorNumeric(Mat mat,Mat *fact)
1419 {
1420   int        ierr;
1421   PetscTruth flg;
1422 
1423   PetscFunctionBegin;
1424   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1425   PetscValidType(mat);
1426   MatPreallocated(mat);
1427   PetscValidPointer(fact);
1428   PetscValidHeaderSpecific(*fact,MAT_COOKIE);
1429   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1430   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1431     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d",
1432             mat->M,(*fact)->M,mat->N,(*fact)->N);
1433   }
1434   if (!(*fact)->ops->lufactornumeric) SETERRQ(PETSC_ERR_SUP,"");
1435 
1436   ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1437   ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr);
1438   ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1439   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr);
1440   if (flg) {
1441     ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr);
1442     if (flg) {
1443       ierr = PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1444     }
1445     ierr = MatView(*fact,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1446     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1447     if (flg) {
1448       ierr = PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1449     }
1450   }
1451   PetscFunctionReturn(0);
1452 }
1453 
1454 #undef __FUNC__
1455 #define __FUNC__ "MatCholeskyFactor"
1456 /*@
1457    MatCholeskyFactor - Performs in-place Cholesky factorization of a
1458    symmetric matrix.
1459 
1460    Collective on Mat
1461 
1462    Input Parameters:
1463 +  mat - the matrix
1464 .  perm - row and column permutations
1465 -  f - expected fill as ratio of original fill
1466 
1467    Notes:
1468    See MatLUFactor() for the nonsymmetric case.  See also
1469    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
1470 
1471    Most users should employ the simplified SLES interface for linear solvers
1472    instead of working directly with matrix algebra routines such as this.
1473    See, e.g., SLESCreate().
1474 
1475    Level: developer
1476 
1477    Concepts: matrices^Cholesky factorization
1478 
1479 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
1480           MatGetOrdering()
1481 
1482 @*/
1483 int MatCholeskyFactor(Mat mat,IS perm,PetscReal f)
1484 {
1485   int ierr;
1486 
1487   PetscFunctionBegin;
1488   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1489   PetscValidType(mat);
1490   MatPreallocated(mat);
1491   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1492   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1493   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1494   if (!mat->ops->choleskyfactor) SETERRQ(PETSC_ERR_SUP,"");
1495 
1496   ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1497   ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr);
1498   ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1499   PetscFunctionReturn(0);
1500 }
1501 
1502 #undef __FUNC__
1503 #define __FUNC__ "MatCholeskyFactorSymbolic"
1504 /*@
1505    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
1506    of a symmetric matrix.
1507 
1508    Collective on Mat
1509 
1510    Input Parameters:
1511 +  mat - the matrix
1512 .  perm - row and column permutations
1513 -  f - expected fill as ratio of original
1514 
1515    Output Parameter:
1516 .  fact - the factored matrix
1517 
1518    Notes:
1519    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
1520    MatCholeskyFactor() and MatCholeskyFactorNumeric().
1521 
1522    Most users should employ the simplified SLES interface for linear solvers
1523    instead of working directly with matrix algebra routines such as this.
1524    See, e.g., SLESCreate().
1525 
1526    Level: developer
1527 
1528    Concepts: matrices^Cholesky symbolic factorization
1529 
1530 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
1531           MatGetOrdering()
1532 
1533 @*/
1534 int MatCholeskyFactorSymbolic(Mat mat,IS perm,PetscReal f,Mat *fact)
1535 {
1536   int ierr;
1537 
1538   PetscFunctionBegin;
1539   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1540   PetscValidType(mat);
1541   MatPreallocated(mat);
1542   PetscValidPointer(fact);
1543   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1544   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1545   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1546   if (!mat->ops->choleskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,"");
1547 
1548   ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1549   ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr);
1550   ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1551   PetscFunctionReturn(0);
1552 }
1553 
1554 #undef __FUNC__
1555 #define __FUNC__ "MatCholeskyFactorNumeric"
1556 /*@
1557    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
1558    of a symmetric matrix. Call this routine after first calling
1559    MatCholeskyFactorSymbolic().
1560 
1561    Collective on Mat
1562 
1563    Input Parameter:
1564 .  mat - the initial matrix
1565 
1566    Output Parameter:
1567 .  fact - the factored matrix
1568 
1569    Notes:
1570    Most users should employ the simplified SLES interface for linear solvers
1571    instead of working directly with matrix algebra routines such as this.
1572    See, e.g., SLESCreate().
1573 
1574    Level: developer
1575 
1576    Concepts: matrices^Cholesky numeric factorization
1577 
1578 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
1579 @*/
1580 int MatCholeskyFactorNumeric(Mat mat,Mat *fact)
1581 {
1582   int ierr;
1583 
1584   PetscFunctionBegin;
1585   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1586   PetscValidType(mat);
1587   MatPreallocated(mat);
1588   PetscValidPointer(fact);
1589   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1590   if (!(*fact)->ops->choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"");
1591   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1592     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d",
1593             mat->M,(*fact)->M,mat->N,(*fact)->N);
1594   }
1595 
1596   ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1597   ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr);
1598   ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1599   PetscFunctionReturn(0);
1600 }
1601 
1602 /* ----------------------------------------------------------------*/
1603 #undef __FUNC__
1604 #define __FUNC__ "MatSolve"
1605 /*@
1606    MatSolve - Solves A x = b, given a factored matrix.
1607 
1608    Collective on Mat and Vec
1609 
1610    Input Parameters:
1611 +  mat - the factored matrix
1612 -  b - the right-hand-side vector
1613 
1614    Output Parameter:
1615 .  x - the result vector
1616 
1617    Notes:
1618    The vectors b and x cannot be the same.  I.e., one cannot
1619    call MatSolve(A,x,x).
1620 
1621    Notes:
1622    Most users should employ the simplified SLES interface for linear solvers
1623    instead of working directly with matrix algebra routines such as this.
1624    See, e.g., SLESCreate().
1625 
1626    Level: developer
1627 
1628    Concepts: matrices^triangular solves
1629 
1630 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
1631 @*/
1632 int MatSolve(Mat mat,Vec b,Vec x)
1633 {
1634   int ierr;
1635 
1636   PetscFunctionBegin;
1637   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1638   PetscValidType(mat);
1639   MatPreallocated(mat);
1640   PetscValidHeaderSpecific(b,VEC_COOKIE);
1641   PetscValidHeaderSpecific(x,VEC_COOKIE);
1642   PetscCheckSameComm(mat,b);
1643   PetscCheckSameComm(mat,x);
1644   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1645   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1646   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1647   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1648   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1649   if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0);
1650 
1651   if (!mat->ops->solve) SETERRQ(PETSC_ERR_SUP,"");
1652   ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1653   ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr);
1654   ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1655   PetscFunctionReturn(0);
1656 }
1657 
1658 #undef __FUNC__
1659 #define __FUNC__ "MatForwardSolve"
1660 /* @
1661    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU.
1662 
1663    Collective on Mat and Vec
1664 
1665    Input Parameters:
1666 +  mat - the factored matrix
1667 -  b - the right-hand-side vector
1668 
1669    Output Parameter:
1670 .  x - the result vector
1671 
1672    Notes:
1673    MatSolve() should be used for most applications, as it performs
1674    a forward solve followed by a backward solve.
1675 
1676    The vectors b and x cannot be the same.  I.e., one cannot
1677    call MatForwardSolve(A,x,x).
1678 
1679    Most users should employ the simplified SLES interface for linear solvers
1680    instead of working directly with matrix algebra routines such as this.
1681    See, e.g., SLESCreate().
1682 
1683    Level: developer
1684 
1685    Concepts: matrices^forward solves
1686 
1687 .seealso: MatSolve(), MatBackwardSolve()
1688 @ */
1689 int MatForwardSolve(Mat mat,Vec b,Vec x)
1690 {
1691   int ierr;
1692 
1693   PetscFunctionBegin;
1694   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1695   PetscValidType(mat);
1696   MatPreallocated(mat);
1697   PetscValidHeaderSpecific(b,VEC_COOKIE);
1698   PetscValidHeaderSpecific(x,VEC_COOKIE);
1699   PetscCheckSameComm(mat,b);
1700   PetscCheckSameComm(mat,x);
1701   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1702   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1703   if (!mat->ops->forwardsolve) SETERRQ(PETSC_ERR_SUP,"");
1704   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1705   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1706   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1707 
1708   ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1709   ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr);
1710   ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1711   PetscFunctionReturn(0);
1712 }
1713 
1714 #undef __FUNC__
1715 #define __FUNC__ "MatBackwardSolve"
1716 /* @
1717    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
1718 
1719    Collective on Mat and Vec
1720 
1721    Input Parameters:
1722 +  mat - the factored matrix
1723 -  b - the right-hand-side vector
1724 
1725    Output Parameter:
1726 .  x - the result vector
1727 
1728    Notes:
1729    MatSolve() should be used for most applications, as it performs
1730    a forward solve followed by a backward solve.
1731 
1732    The vectors b and x cannot be the same.  I.e., one cannot
1733    call MatBackwardSolve(A,x,x).
1734 
1735    Most users should employ the simplified SLES interface for linear solvers
1736    instead of working directly with matrix algebra routines such as this.
1737    See, e.g., SLESCreate().
1738 
1739    Level: developer
1740 
1741    Concepts: matrices^backward solves
1742 
1743 .seealso: MatSolve(), MatForwardSolve()
1744 @ */
1745 int MatBackwardSolve(Mat mat,Vec b,Vec x)
1746 {
1747   int ierr;
1748 
1749   PetscFunctionBegin;
1750   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1751   PetscValidType(mat);
1752   MatPreallocated(mat);
1753   PetscValidHeaderSpecific(b,VEC_COOKIE);
1754   PetscValidHeaderSpecific(x,VEC_COOKIE);
1755   PetscCheckSameComm(mat,b);
1756   PetscCheckSameComm(mat,x);
1757   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1758   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1759   if (!mat->ops->backwardsolve) SETERRQ(PETSC_ERR_SUP,"");
1760   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1761   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1762   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1763 
1764   ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
1765   ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr);
1766   ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
1767   PetscFunctionReturn(0);
1768 }
1769 
1770 #undef __FUNC__
1771 #define __FUNC__ "MatSolveAdd"
1772 /*@
1773    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
1774 
1775    Collective on Mat and Vec
1776 
1777    Input Parameters:
1778 +  mat - the factored matrix
1779 .  b - the right-hand-side vector
1780 -  y - the vector to be added to
1781 
1782    Output Parameter:
1783 .  x - the result vector
1784 
1785    Notes:
1786    The vectors b and x cannot be the same.  I.e., one cannot
1787    call MatSolveAdd(A,x,y,x).
1788 
1789    Most users should employ the simplified SLES interface for linear solvers
1790    instead of working directly with matrix algebra routines such as this.
1791    See, e.g., SLESCreate().
1792 
1793    Level: developer
1794 
1795    Concepts: matrices^triangular solves
1796 
1797 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
1798 @*/
1799 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
1800 {
1801   Scalar one = 1.0;
1802   Vec    tmp;
1803   int    ierr;
1804 
1805   PetscFunctionBegin;
1806   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1807   PetscValidType(mat);
1808   MatPreallocated(mat);
1809   PetscValidHeaderSpecific(y,VEC_COOKIE);
1810   PetscValidHeaderSpecific(b,VEC_COOKIE);
1811   PetscValidHeaderSpecific(x,VEC_COOKIE);
1812   PetscCheckSameComm(mat,b);
1813   PetscCheckSameComm(mat,y);
1814   PetscCheckSameComm(mat,x);
1815   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1816   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1817   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1818   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1819   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1820   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1821   if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
1822 
1823   ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
1824   if (mat->ops->solveadd)  {
1825     ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr);
1826   } else {
1827     /* do the solve then the add manually */
1828     if (x != y) {
1829       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1830       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
1831     } else {
1832       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
1833       PetscLogObjectParent(mat,tmp);
1834       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
1835       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
1836       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
1837       ierr = VecDestroy(tmp);CHKERRQ(ierr);
1838     }
1839   }
1840   ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
1841   PetscFunctionReturn(0);
1842 }
1843 
1844 #undef __FUNC__
1845 #define __FUNC__ "MatSolveTranspose"
1846 /*@
1847    MatSolveTranspose - Solves A' x = b, given a factored matrix.
1848 
1849    Collective on Mat and Vec
1850 
1851    Input Parameters:
1852 +  mat - the factored matrix
1853 -  b - the right-hand-side vector
1854 
1855    Output Parameter:
1856 .  x - the result vector
1857 
1858    Notes:
1859    The vectors b and x cannot be the same.  I.e., one cannot
1860    call MatSolveTranspose(A,x,x).
1861 
1862    Most users should employ the simplified SLES interface for linear solvers
1863    instead of working directly with matrix algebra routines such as this.
1864    See, e.g., SLESCreate().
1865 
1866    Level: developer
1867 
1868    Concepts: matrices^triangular solves
1869 
1870 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
1871 @*/
1872 int MatSolveTranspose(Mat mat,Vec b,Vec x)
1873 {
1874   int ierr;
1875 
1876   PetscFunctionBegin;
1877   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1878   PetscValidType(mat);
1879   MatPreallocated(mat);
1880   PetscValidHeaderSpecific(b,VEC_COOKIE);
1881   PetscValidHeaderSpecific(x,VEC_COOKIE);
1882   PetscCheckSameComm(mat,b);
1883   PetscCheckSameComm(mat,x);
1884   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1885   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1886   if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name);
1887   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1888   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
1889 
1890   ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
1891   ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr);
1892   ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
1893   PetscFunctionReturn(0);
1894 }
1895 
1896 #undef __FUNC__
1897 #define __FUNC__ "MatSolveTransposeAdd"
1898 /*@
1899    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
1900                       factored matrix.
1901 
1902    Collective on Mat and Vec
1903 
1904    Input Parameters:
1905 +  mat - the factored matrix
1906 .  b - the right-hand-side vector
1907 -  y - the vector to be added to
1908 
1909    Output Parameter:
1910 .  x - the result vector
1911 
1912    Notes:
1913    The vectors b and x cannot be the same.  I.e., one cannot
1914    call MatSolveTransposeAdd(A,x,y,x).
1915 
1916    Most users should employ the simplified SLES interface for linear solvers
1917    instead of working directly with matrix algebra routines such as this.
1918    See, e.g., SLESCreate().
1919 
1920    Level: developer
1921 
1922    Concepts: matrices^triangular solves
1923 
1924 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
1925 @*/
1926 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
1927 {
1928   Scalar one = 1.0;
1929   int    ierr;
1930   Vec    tmp;
1931 
1932   PetscFunctionBegin;
1933   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1934   PetscValidType(mat);
1935   MatPreallocated(mat);
1936   PetscValidHeaderSpecific(y,VEC_COOKIE);
1937   PetscValidHeaderSpecific(b,VEC_COOKIE);
1938   PetscValidHeaderSpecific(x,VEC_COOKIE);
1939   PetscCheckSameComm(mat,b);
1940   PetscCheckSameComm(mat,y);
1941   PetscCheckSameComm(mat,x);
1942   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1943   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1944   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
1945   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
1946   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
1947   if (x->n != y->n)   SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
1948 
1949   ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
1950   if (mat->ops->solvetransposeadd) {
1951     ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr);
1952   } else {
1953     /* do the solve then the add manually */
1954     if (x != y) {
1955       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
1956       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
1957     } else {
1958       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
1959       PetscLogObjectParent(mat,tmp);
1960       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
1961       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
1962       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
1963       ierr = VecDestroy(tmp);CHKERRQ(ierr);
1964     }
1965   }
1966   ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
1967   PetscFunctionReturn(0);
1968 }
1969 /* ----------------------------------------------------------------*/
1970 
1971 #undef __FUNC__
1972 #define __FUNC__ "MatRelax"
1973 /*@
1974    MatRelax - Computes one relaxation sweep.
1975 
1976    Collective on Mat and Vec
1977 
1978    Input Parameters:
1979 +  mat - the matrix
1980 .  b - the right hand side
1981 .  omega - the relaxation factor
1982 .  flag - flag indicating the type of SOR (see below)
1983 .  shift -  diagonal shift
1984 -  its - the number of iterations
1985 
1986    Output Parameters:
1987 .  x - the solution (can contain an initial guess)
1988 
1989    SOR Flags:
1990 .     SOR_FORWARD_SWEEP - forward SOR
1991 .     SOR_BACKWARD_SWEEP - backward SOR
1992 .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
1993 .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
1994 .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
1995 .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
1996 .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
1997          upper/lower triangular part of matrix to
1998          vector (with omega)
1999 .     SOR_ZERO_INITIAL_GUESS - zero initial guess
2000 
2001    Notes:
2002    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
2003    SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings
2004    on each processor.
2005 
2006    Application programmers will not generally use MatRelax() directly,
2007    but instead will employ the SLES/PC interface.
2008 
2009    Notes for Advanced Users:
2010    The flags are implemented as bitwise inclusive or operations.
2011    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
2012    to specify a zero initial guess for SSOR.
2013 
2014    Most users should employ the simplified SLES interface for linear solvers
2015    instead of working directly with matrix algebra routines such as this.
2016    See, e.g., SLESCreate().
2017 
2018    Level: developer
2019 
2020    Concepts: matrices^relaxation
2021    Concepts: matrices^SOR
2022    Concepts: matrices^Gauss-Seidel
2023 
2024 @*/
2025 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,Vec x)
2026 {
2027   int ierr;
2028 
2029   PetscFunctionBegin;
2030   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2031   PetscValidType(mat);
2032   MatPreallocated(mat);
2033   PetscValidHeaderSpecific(b,VEC_COOKIE);
2034   PetscValidHeaderSpecific(x,VEC_COOKIE);
2035   PetscCheckSameComm(mat,b);
2036   PetscCheckSameComm(mat,x);
2037   if (!mat->ops->relax) SETERRQ(PETSC_ERR_SUP,"");
2038   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2039   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2040   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
2041   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
2042   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
2043 
2044   ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2045   ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,x);CHKERRQ(ierr);
2046   ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2047   PetscFunctionReturn(0);
2048 }
2049 
2050 #undef __FUNC__
2051 #define __FUNC__ "MatCopy_Basic"
2052 /*
2053       Default matrix copy routine.
2054 */
2055 int MatCopy_Basic(Mat A,Mat B,MatStructure str)
2056 {
2057   int    ierr,i,rstart,rend,nz,*cwork;
2058   Scalar *vwork;
2059 
2060   PetscFunctionBegin;
2061   ierr = MatZeroEntries(B);CHKERRQ(ierr);
2062   ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr);
2063   for (i=rstart; i<rend; i++) {
2064     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2065     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2066     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2067   }
2068   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2069   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2070   PetscFunctionReturn(0);
2071 }
2072 
2073 #undef __FUNC__
2074 #define __FUNC__ "MatCopy"
2075 /*@C
2076    MatCopy - Copys a matrix to another matrix.
2077 
2078    Collective on Mat
2079 
2080    Input Parameters:
2081 +  A - the matrix
2082 -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN
2083 
2084    Output Parameter:
2085 .  B - where the copy is put
2086 
2087    Notes:
2088    If you use SAME_NONZERO_PATTERN then the zero matrices had better have the
2089    same nonzero pattern or the routine will crash.
2090 
2091    MatCopy() copies the matrix entries of a matrix to another existing
2092    matrix (after first zeroing the second matrix).  A related routine is
2093    MatConvert(), which first creates a new matrix and then copies the data.
2094 
2095    Level: intermediate
2096 
2097    Concepts: matrices^copying
2098 
2099 .seealso: MatConvert()
2100 @*/
2101 int MatCopy(Mat A,Mat B,MatStructure str)
2102 {
2103   int ierr;
2104 
2105   PetscFunctionBegin;
2106   PetscValidHeaderSpecific(A,MAT_COOKIE);
2107   PetscValidHeaderSpecific(B,MAT_COOKIE);
2108   PetscValidType(A);
2109   MatPreallocated(A);
2110   PetscValidType(B);
2111   MatPreallocated(B);
2112   PetscCheckSameComm(A,B);
2113   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2114   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2115   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d",A->M,B->M,
2116                                              A->N,B->N);
2117 
2118   ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2119   if (A->ops->copy) {
2120     ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr);
2121   } else { /* generic conversion */
2122     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2123   }
2124   ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2125   PetscFunctionReturn(0);
2126 }
2127 
2128 #include "petscsys.h"
2129 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE;
2130 PetscFList      MatConvertList              = 0;
2131 
2132 #undef __FUNC__
2133 #define __FUNC__ "MatConvertRegister"
2134 /*@C
2135     MatConvertRegister - Allows one to register a routine that reads matrices
2136         from a binary file for a particular matrix type.
2137 
2138   Not Collective
2139 
2140   Input Parameters:
2141 +   type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ.
2142 -   Converter - the function that reads the matrix from the binary file.
2143 
2144   Level: developer
2145 
2146 .seealso: MatConvertRegisterAll(), MatConvert()
2147 
2148 @*/
2149 int MatConvertRegister(char *sname,char *path,char *name,int (*function)(Mat,MatType,Mat*))
2150 {
2151   int  ierr;
2152   char fullname[256];
2153 
2154   PetscFunctionBegin;
2155   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2156   ierr = PetscFListAdd(&MatConvertList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
2157   PetscFunctionReturn(0);
2158 }
2159 
2160 #undef __FUNC__
2161 #define __FUNC__ "MatConvert"
2162 /*@C
2163    MatConvert - Converts a matrix to another matrix, either of the same
2164    or different type.
2165 
2166    Collective on Mat
2167 
2168    Input Parameters:
2169 +  mat - the matrix
2170 -  newtype - new matrix type.  Use MATSAME to create a new matrix of the
2171    same type as the original matrix.
2172 
2173    Output Parameter:
2174 .  M - pointer to place new matrix
2175 
2176    Notes:
2177    MatConvert() first creates a new matrix and then copies the data from
2178    the first matrix.  A related routine is MatCopy(), which copies the matrix
2179    entries of one matrix to another already existing matrix context.
2180 
2181    Level: intermediate
2182 
2183    Concepts: matrices^converting between storage formats
2184 
2185 .seealso: MatCopy(), MatDuplicate()
2186 @*/
2187 int MatConvert(Mat mat,MatType newtype,Mat *M)
2188 {
2189   int        ierr;
2190   PetscTruth sametype,issame,flg;
2191   char       convname[256],mtype[256];
2192 
2193   PetscFunctionBegin;
2194   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2195   PetscValidType(mat);
2196   MatPreallocated(mat);
2197   PetscValidPointer(M);
2198   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2199   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2200 
2201   ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr);
2202   if (flg) {
2203     newtype = mtype;
2204   }
2205   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2206 
2207   ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr);
2208   ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr);
2209   if ((sametype || issame) && mat->ops->duplicate) {
2210     ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr);
2211   } else {
2212     int (*conv)(Mat,MatType,Mat*);
2213     ierr = PetscStrcpy(convname,"MatConvertTo_");CHKERRQ(ierr);
2214     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2215     ierr =  PetscFListFind(mat->comm,MatConvertList,convname,(int(**)(void*))&conv);CHKERRQ(ierr);
2216     if (conv) {
2217       ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2218     } else {
2219       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
2220       ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr);
2221       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
2222       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2223       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
2224       ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void**)&conv);CHKERRQ(ierr);
2225       if (conv) {
2226         ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2227       } else {
2228         if (mat->ops->convert) {
2229           ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr);
2230         } else {
2231           ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr);
2232         }
2233       }
2234     }
2235   }
2236   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2237   PetscFunctionReturn(0);
2238 }
2239 
2240 
2241 #undef __FUNC__
2242 #define __FUNC__ "MatDuplicate"
2243 /*@C
2244    MatDuplicate - Duplicates a matrix including the non-zero structure.
2245 
2246    Collective on Mat
2247 
2248    Input Parameters:
2249 +  mat - the matrix
2250 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
2251         values as well or not
2252 
2253    Output Parameter:
2254 .  M - pointer to place new matrix
2255 
2256    Level: intermediate
2257 
2258    Concepts: matrices^duplicating
2259 
2260 .seealso: MatCopy(), MatConvert()
2261 @*/
2262 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
2263 {
2264   int ierr;
2265 
2266   PetscFunctionBegin;
2267   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2268   PetscValidType(mat);
2269   MatPreallocated(mat);
2270   PetscValidPointer(M);
2271   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2272   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2273 
2274   *M  = 0;
2275   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2276   if (!mat->ops->duplicate) {
2277     SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type");
2278   }
2279   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
2280   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2281   PetscFunctionReturn(0);
2282 }
2283 
2284 #undef __FUNC__
2285 #define __FUNC__ "MatGetDiagonal"
2286 /*@
2287    MatGetDiagonal - Gets the diagonal of a matrix.
2288 
2289    Collective on Mat and Vec
2290 
2291    Input Parameters:
2292 +  mat - the matrix
2293 -  v - the vector for storing the diagonal
2294 
2295    Output Parameter:
2296 .  v - the diagonal of the matrix
2297 
2298    Notes:
2299    For the SeqAIJ matrix format, this routine may also be called
2300    on a LU factored matrix; in that case it routines the reciprocal of
2301    the diagonal entries in U. It returns the entries permuted by the
2302    row and column permutation used during the symbolic factorization.
2303 
2304    Level: intermediate
2305 
2306    Concepts: matrices^accessing diagonals
2307 
2308 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax()
2309 @*/
2310 int MatGetDiagonal(Mat mat,Vec v)
2311 {
2312   int ierr;
2313 
2314   PetscFunctionBegin;
2315   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2316   PetscValidType(mat);
2317   MatPreallocated(mat);
2318   PetscValidHeaderSpecific(v,VEC_COOKIE);
2319   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2320   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2321   if (!mat->ops->getdiagonal) SETERRQ(PETSC_ERR_SUP,"");
2322 
2323   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
2324   PetscFunctionReturn(0);
2325 }
2326 
2327 #undef __FUNC__
2328 #define __FUNC__ "MatGetRowMax"
2329 /*@
2330    MatGetRowMax - Gets the maximum value (in absolute value) of each
2331         row of the matrix
2332 
2333    Collective on Mat and Vec
2334 
2335    Input Parameters:
2336 .  mat - the matrix
2337 
2338    Output Parameter:
2339 .  v - the vector for storing the maximums
2340 
2341    Level: intermediate
2342 
2343    Concepts: matrices^getting row maximums
2344 
2345 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix()
2346 @*/
2347 int MatGetRowMax(Mat mat,Vec v)
2348 {
2349   int ierr;
2350 
2351   PetscFunctionBegin;
2352   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2353   PetscValidType(mat);
2354   MatPreallocated(mat);
2355   PetscValidHeaderSpecific(v,VEC_COOKIE);
2356   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2357   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2358   if (!mat->ops->getrowmax) SETERRQ(PETSC_ERR_SUP,"");
2359 
2360   ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr);
2361   PetscFunctionReturn(0);
2362 }
2363 
2364 #undef __FUNC__
2365 #define __FUNC__ "MatTranspose"
2366 /*@C
2367    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
2368 
2369    Collective on Mat
2370 
2371    Input Parameter:
2372 .  mat - the matrix to transpose
2373 
2374    Output Parameters:
2375 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
2376 
2377    Level: intermediate
2378 
2379    Concepts: matrices^transposing
2380 
2381 .seealso: MatMultTranspose(), MatMultTransposeAdd()
2382 @*/
2383 int MatTranspose(Mat mat,Mat *B)
2384 {
2385   int ierr;
2386 
2387   PetscFunctionBegin;
2388   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2389   PetscValidType(mat);
2390   MatPreallocated(mat);
2391   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2392   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2393   if (!mat->ops->transpose) SETERRQ(PETSC_ERR_SUP,"");
2394   ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr);
2395   PetscFunctionReturn(0);
2396 }
2397 
2398 #undef __FUNC__
2399 #define __FUNC__ "MatPermute"
2400 /*@C
2401    MatPermute - Creates a new matrix with rows and columns permuted from the
2402    original.
2403 
2404    Collective on Mat
2405 
2406    Input Parameters:
2407 +  mat - the matrix to permute
2408 .  row - row permutation, each processor supplies only the permutation for its rows
2409 -  col - column permutation, each processor needs the entire column permutation, that is
2410          this is the same size as the total number of columns in the matrix
2411 
2412    Output Parameters:
2413 .  B - the permuted matrix
2414 
2415    Level: advanced
2416 
2417    Concepts: matrices^permuting
2418 
2419 .seealso: MatGetOrdering()
2420 @*/
2421 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2422 {
2423   int ierr;
2424 
2425   PetscFunctionBegin;
2426   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2427   PetscValidType(mat);
2428   MatPreallocated(mat);
2429   PetscValidHeaderSpecific(row,IS_COOKIE);
2430   PetscValidHeaderSpecific(col,IS_COOKIE);
2431   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2432   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2433   if (!mat->ops->permute) SETERRQ(PETSC_ERR_SUP,"");
2434   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2435   PetscFunctionReturn(0);
2436 }
2437 
2438 #undef __FUNC__
2439 #define __FUNC__ "MatEqual"
2440 /*@
2441    MatEqual - Compares two matrices.
2442 
2443    Collective on Mat
2444 
2445    Input Parameters:
2446 +  A - the first matrix
2447 -  B - the second matrix
2448 
2449    Output Parameter:
2450 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2451 
2452    Level: intermediate
2453 
2454    Concepts: matrices^equality between
2455 @*/
2456 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2457 {
2458   int ierr;
2459 
2460   PetscFunctionBegin;
2461   PetscValidHeaderSpecific(A,MAT_COOKIE);
2462   PetscValidHeaderSpecific(B,MAT_COOKIE);
2463   PetscValidType(A);
2464   MatPreallocated(A);
2465   PetscValidType(B);
2466   MatPreallocated(B);
2467   PetscValidIntPointer(flg);
2468   PetscCheckSameComm(A,B);
2469   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2470   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2471   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d %d %d",
2472                                              A->M,B->M,A->N,B->N);
2473   if (!A->ops->equal) SETERRQ(PETSC_ERR_SUP,"");
2474   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2475   PetscFunctionReturn(0);
2476 }
2477 
2478 #undef __FUNC__
2479 #define __FUNC__ "MatDiagonalScale"
2480 /*@
2481    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2482    matrices that are stored as vectors.  Either of the two scaling
2483    matrices can be PETSC_NULL.
2484 
2485    Collective on Mat
2486 
2487    Input Parameters:
2488 +  mat - the matrix to be scaled
2489 .  l - the left scaling vector (or PETSC_NULL)
2490 -  r - the right scaling vector (or PETSC_NULL)
2491 
2492    Notes:
2493    MatDiagonalScale() computes A = LAR, where
2494    L = a diagonal matrix, R = a diagonal matrix
2495 
2496    Level: intermediate
2497 
2498    Concepts: matrices^diagonal scaling
2499    Concepts: diagonal scaling of matrices
2500 
2501 .seealso: MatScale()
2502 @*/
2503 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2504 {
2505   int ierr;
2506 
2507   PetscFunctionBegin;
2508   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2509   PetscValidType(mat);
2510   MatPreallocated(mat);
2511   if (!mat->ops->diagonalscale) SETERRQ(PETSC_ERR_SUP,"");
2512   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2513   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2514   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2515   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2516 
2517   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2518   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2519   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2520   PetscFunctionReturn(0);
2521 }
2522 
2523 #undef __FUNC__
2524 #define __FUNC__ "MatScale"
2525 /*@
2526     MatScale - Scales all elements of a matrix by a given number.
2527 
2528     Collective on Mat
2529 
2530     Input Parameters:
2531 +   mat - the matrix to be scaled
2532 -   a  - the scaling value
2533 
2534     Output Parameter:
2535 .   mat - the scaled matrix
2536 
2537     Level: intermediate
2538 
2539     Concepts: matrices^scaling all entries
2540 
2541 .seealso: MatDiagonalScale()
2542 @*/
2543 int MatScale(Scalar *a,Mat mat)
2544 {
2545   int ierr;
2546 
2547   PetscFunctionBegin;
2548   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2549   PetscValidType(mat);
2550   MatPreallocated(mat);
2551   PetscValidScalarPointer(a);
2552   if (!mat->ops->scale) SETERRQ(PETSC_ERR_SUP,"");
2553   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2554   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2555 
2556   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2557   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2558   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2559   PetscFunctionReturn(0);
2560 }
2561 
2562 #undef __FUNC__
2563 #define __FUNC__ "MatNorm"
2564 /*@
2565    MatNorm - Calculates various norms of a matrix.
2566 
2567    Collective on Mat
2568 
2569    Input Parameters:
2570 +  mat - the matrix
2571 -  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
2572 
2573    Output Parameters:
2574 .  norm - the resulting norm
2575 
2576    Level: intermediate
2577 
2578    Concepts: matrices^norm
2579    Concepts: norm^of matrix
2580 @*/
2581 int MatNorm(Mat mat,NormType type,PetscReal *norm)
2582 {
2583   int ierr;
2584 
2585   PetscFunctionBegin;
2586   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2587   PetscValidType(mat);
2588   MatPreallocated(mat);
2589   PetscValidScalarPointer(norm);
2590 
2591   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2592   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2593   if (!mat->ops->norm) SETERRQ(PETSC_ERR_SUP,"Not for this matrix type");
2594   ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr);
2595   PetscFunctionReturn(0);
2596 }
2597 
2598 /*
2599      This variable is used to prevent counting of MatAssemblyBegin() that
2600    are called from within a MatAssemblyEnd().
2601 */
2602 static int MatAssemblyEnd_InUse = 0;
2603 #undef __FUNC__
2604 #define __FUNC__ "MatAssemblyBegin"
2605 /*@
2606    MatAssemblyBegin - Begins assembling the matrix.  This routine should
2607    be called after completing all calls to MatSetValues().
2608 
2609    Collective on Mat
2610 
2611    Input Parameters:
2612 +  mat - the matrix
2613 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2614 
2615    Notes:
2616    MatSetValues() generally caches the values.  The matrix is ready to
2617    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2618    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2619    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2620    using the matrix.
2621 
2622    Level: beginner
2623 
2624    Concepts: matrices^assembling
2625 
2626 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
2627 @*/
2628 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
2629 {
2630   int ierr;
2631 
2632   PetscFunctionBegin;
2633   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2634   PetscValidType(mat);
2635   MatPreallocated(mat);
2636   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
2637   if (mat->assembled) {
2638     mat->was_assembled = PETSC_TRUE;
2639     mat->assembled     = PETSC_FALSE;
2640   }
2641   if (!MatAssemblyEnd_InUse) {
2642     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2643     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2644     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
2645   } else {
2646     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
2647   }
2648   PetscFunctionReturn(0);
2649 }
2650 
2651 #undef __FUNC__
2652 #define __FUNC__ "MatAssembed"
2653 /*@
2654    MatAssembled - Indicates if a matrix has been assembled and is ready for
2655      use; for example, in matrix-vector product.
2656 
2657    Collective on Mat
2658 
2659    Input Parameter:
2660 .  mat - the matrix
2661 
2662    Output Parameter:
2663 .  assembled - PETSC_TRUE or PETSC_FALSE
2664 
2665    Level: advanced
2666 
2667    Concepts: matrices^assembled?
2668 
2669 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
2670 @*/
2671 int MatAssembled(Mat mat,PetscTruth *assembled)
2672 {
2673   PetscFunctionBegin;
2674   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2675   PetscValidType(mat);
2676   MatPreallocated(mat);
2677   *assembled = mat->assembled;
2678   PetscFunctionReturn(0);
2679 }
2680 
2681 #undef __FUNC__
2682 #define __FUNC__ "MatView_Private"
2683 /*
2684     Processes command line options to determine if/how a matrix
2685   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
2686 */
2687 int MatView_Private(Mat mat)
2688 {
2689   int        ierr;
2690   PetscTruth flg;
2691 
2692   PetscFunctionBegin;
2693   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr);
2694   if (flg) {
2695     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
2696     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2697     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2698   }
2699   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
2700   if (flg) {
2701     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_LONG);CHKERRQ(ierr);
2702     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2703     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2704   }
2705   ierr = PetscOptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr);
2706   if (flg) {
2707     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2708   }
2709   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr);
2710   if (flg) {
2711     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
2712     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2713     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
2714   }
2715   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
2716   if (flg) {
2717     ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
2718     if (flg) {
2719       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
2720     }
2721     ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2722     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2723     if (flg) {
2724       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
2725     }
2726   }
2727   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr);
2728   if (flg) {
2729     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2730     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
2731   }
2732   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_binary",&flg);CHKERRQ(ierr);
2733   if (flg) {
2734     ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2735     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
2736   }
2737   PetscFunctionReturn(0);
2738 }
2739 
2740 #undef __FUNC__
2741 #define __FUNC__ "MatAssemblyEnd"
2742 /*@
2743    MatAssemblyEnd - Completes assembling the matrix.  This routine should
2744    be called after MatAssemblyBegin().
2745 
2746    Collective on Mat
2747 
2748    Input Parameters:
2749 +  mat - the matrix
2750 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2751 
2752    Options Database Keys:
2753 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
2754 .  -mat_view_info_detailed - Prints more detailed info
2755 .  -mat_view - Prints matrix in ASCII format
2756 .  -mat_view_matlab - Prints matrix in Matlab format
2757 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
2758 .  -display <name> - Sets display name (default is host)
2759 -  -draw_pause <sec> - Sets number of seconds to pause after display
2760 
2761    Notes:
2762    MatSetValues() generally caches the values.  The matrix is ready to
2763    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2764    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2765    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2766    using the matrix.
2767 
2768    Level: beginner
2769 
2770 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled()
2771 @*/
2772 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
2773 {
2774   int        ierr;
2775   static int inassm = 0;
2776 
2777   PetscFunctionBegin;
2778   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2779   PetscValidType(mat);
2780   MatPreallocated(mat);
2781 
2782   inassm++;
2783   MatAssemblyEnd_InUse++;
2784   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
2785     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2786     if (mat->ops->assemblyend) {
2787       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2788     }
2789     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
2790   } else {
2791     if (mat->ops->assemblyend) {
2792       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
2793     }
2794   }
2795 
2796   /* Flush assembly is not a true assembly */
2797   if (type != MAT_FLUSH_ASSEMBLY) {
2798     mat->assembled  = PETSC_TRUE; mat->num_ass++;
2799   }
2800   mat->insertmode = NOT_SET_VALUES;
2801   MatAssemblyEnd_InUse--;
2802 
2803   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
2804     ierr = MatView_Private(mat);CHKERRQ(ierr);
2805   }
2806   inassm--;
2807   PetscFunctionReturn(0);
2808 }
2809 
2810 
2811 #undef __FUNC__
2812 #define __FUNC__ "MatCompress"
2813 /*@
2814    MatCompress - Tries to store the matrix in as little space as
2815    possible.  May fail if memory is already fully used, since it
2816    tries to allocate new space.
2817 
2818    Collective on Mat
2819 
2820    Input Parameters:
2821 .  mat - the matrix
2822 
2823    Level: advanced
2824 
2825 @*/
2826 int MatCompress(Mat mat)
2827 {
2828   int ierr;
2829 
2830   PetscFunctionBegin;
2831   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2832   PetscValidType(mat);
2833   MatPreallocated(mat);
2834   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
2835   PetscFunctionReturn(0);
2836 }
2837 
2838 #undef __FUNC__
2839 #define __FUNC__ "MatSetOption"
2840 /*@
2841    MatSetOption - Sets a parameter option for a matrix. Some options
2842    may be specific to certain storage formats.  Some options
2843    determine how values will be inserted (or added). Sorted,
2844    row-oriented input will generally assemble the fastest. The default
2845    is row-oriented, nonsorted input.
2846 
2847    Collective on Mat
2848 
2849    Input Parameters:
2850 +  mat - the matrix
2851 -  option - the option, one of those listed below (and possibly others),
2852              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
2853 
2854    Options Describing Matrix Structure:
2855 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
2856 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
2857 
2858    Options For Use with MatSetValues():
2859    Insert a logically dense subblock, which can be
2860 +    MAT_ROW_ORIENTED - row-oriented
2861 .    MAT_COLUMN_ORIENTED - column-oriented
2862 .    MAT_ROWS_SORTED - sorted by row
2863 .    MAT_ROWS_UNSORTED - not sorted by row
2864 .    MAT_COLUMNS_SORTED - sorted by column
2865 -    MAT_COLUMNS_UNSORTED - not sorted by column
2866 
2867    Not these options reflect the data you pass in with MatSetValues(); it has
2868    nothing to do with how the data is stored internally in the matrix
2869    data structure.
2870 
2871    When (re)assembling a matrix, we can restrict the input for
2872    efficiency/debugging purposes.  These options include
2873 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
2874         allowed if they generate a new nonzero
2875 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
2876 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
2877          they generate a nonzero in a new diagonal (for block diagonal format only)
2878 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
2879 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
2880 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
2881 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
2882 
2883    Notes:
2884    Some options are relevant only for particular matrix types and
2885    are thus ignored by others.  Other options are not supported by
2886    certain matrix types and will generate an error message if set.
2887 
2888    If using a Fortran 77 module to compute a matrix, one may need to
2889    use the column-oriented option (or convert to the row-oriented
2890    format).
2891 
2892    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
2893    that would generate a new entry in the nonzero structure is instead
2894    ignored.  Thus, if memory has not alredy been allocated for this particular
2895    data, then the insertion is ignored. For dense matrices, in which
2896    the entire array is allocated, no entries are ever ignored.
2897 
2898    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
2899    that would generate a new entry in the nonzero structure instead produces
2900    an error. (Currently supported for AIJ and BAIJ formats only.)
2901    This is a useful flag when using SAME_NONZERO_PATTERN in calling
2902    SLESSetOperators() to ensure that the nonzero pattern truely does
2903    remain unchanged.
2904 
2905    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
2906    that would generate a new entry that has not been preallocated will
2907    instead produce an error. (Currently supported for AIJ and BAIJ formats
2908    only.) This is a useful flag when debugging matrix memory preallocation.
2909 
2910    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
2911    other processors should be dropped, rather than stashed.
2912    This is useful if you know that the "owning" processor is also
2913    always generating the correct matrix entries, so that PETSc need
2914    not transfer duplicate entries generated on another processor.
2915 
2916    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
2917    searches during matrix assembly. When this flag is set, the hash table
2918    is created during the first Matrix Assembly. This hash table is
2919    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
2920    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
2921    should be used with MAT_USE_HASH_TABLE flag. This option is currently
2922    supported by MATMPIBAIJ format only.
2923 
2924    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
2925    are kept in the nonzero structure
2926 
2927    MAT_IGNORE_ZERO_ENTRIES - when using ADD_VALUES for AIJ matrices this will stop
2928    zero values from creating a zero location in the matrix
2929 
2930    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
2931    ROWBS matrix types
2932 
2933    MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works
2934    with AIJ and ROWBS matrix types
2935 
2936    Level: intermediate
2937 
2938    Concepts: matrices^setting options
2939 
2940 @*/
2941 int MatSetOption(Mat mat,MatOption op)
2942 {
2943   int ierr;
2944 
2945   PetscFunctionBegin;
2946   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2947   PetscValidType(mat);
2948   MatPreallocated(mat);
2949   if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
2950   PetscFunctionReturn(0);
2951 }
2952 
2953 #undef __FUNC__
2954 #define __FUNC__ "MatZeroEntries"
2955 /*@
2956    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
2957    this routine retains the old nonzero structure.
2958 
2959    Collective on Mat
2960 
2961    Input Parameters:
2962 .  mat - the matrix
2963 
2964    Level: intermediate
2965 
2966    Concepts: matrices^zeroing
2967 
2968 .seealso: MatZeroRows()
2969 @*/
2970 int MatZeroEntries(Mat mat)
2971 {
2972   int ierr;
2973 
2974   PetscFunctionBegin;
2975   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2976   PetscValidType(mat);
2977   MatPreallocated(mat);
2978   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2979   if (!mat->ops->zeroentries) SETERRQ(PETSC_ERR_SUP,"");
2980 
2981   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
2982   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
2983   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
2984   PetscFunctionReturn(0);
2985 }
2986 
2987 #undef __FUNC__
2988 #define __FUNC__ "MatZeroRows"
2989 /*@C
2990    MatZeroRows - Zeros all entries (except possibly the main diagonal)
2991    of a set of rows of a matrix.
2992 
2993    Collective on Mat
2994 
2995    Input Parameters:
2996 +  mat - the matrix
2997 .  is - index set of rows to remove
2998 -  diag - pointer to value put in all diagonals of eliminated rows.
2999           Note that diag is not a pointer to an array, but merely a
3000           pointer to a single value.
3001 
3002    Notes:
3003    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
3004    but does not release memory.  For the dense and block diagonal
3005    formats this does not alter the nonzero structure.
3006 
3007    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3008    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3009    merely zeroed.
3010 
3011    The user can set a value in the diagonal entry (or for the AIJ and
3012    row formats can optionally remove the main diagonal entry from the
3013    nonzero structure as well, by passing a null pointer (PETSC_NULL
3014    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3015 
3016    For the parallel case, all processes that share the matrix (i.e.,
3017    those in the communicator used for matrix creation) MUST call this
3018    routine, regardless of whether any rows being zeroed are owned by
3019    them.
3020 
3021 
3022    Level: intermediate
3023 
3024    Concepts: matrices^zeroing rows
3025 
3026 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
3027 @*/
3028 int MatZeroRows(Mat mat,IS is,Scalar *diag)
3029 {
3030   int ierr;
3031 
3032   PetscFunctionBegin;
3033   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3034   PetscValidType(mat);
3035   MatPreallocated(mat);
3036   PetscValidHeaderSpecific(is,IS_COOKIE);
3037   if (diag) PetscValidScalarPointer(diag);
3038   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3039   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3040   if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,"");
3041 
3042   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
3043   ierr = MatView_Private(mat);CHKERRQ(ierr);
3044   PetscFunctionReturn(0);
3045 }
3046 
3047 #undef __FUNC__
3048 #define __FUNC__ "MatZeroRowsLocal"
3049 /*@C
3050    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
3051    of a set of rows of a matrix; using local numbering of rows.
3052 
3053    Collective on Mat
3054 
3055    Input Parameters:
3056 +  mat - the matrix
3057 .  is - index set of rows to remove
3058 -  diag - pointer to value put in all diagonals of eliminated rows.
3059           Note that diag is not a pointer to an array, but merely a
3060           pointer to a single value.
3061 
3062    Notes:
3063    Before calling MatZeroRowsLocal(), the user must first set the
3064    local-to-global mapping by calling MatSetLocalToGlobalMapping().
3065 
3066    For the AIJ matrix formats this removes the old nonzero structure,
3067    but does not release memory.  For the dense and block diagonal
3068    formats this does not alter the nonzero structure.
3069 
3070    The user can set a value in the diagonal entry (or for the AIJ and
3071    row formats can optionally remove the main diagonal entry from the
3072    nonzero structure as well, by passing a null pointer (PETSC_NULL
3073    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3074 
3075    Level: intermediate
3076 
3077    Concepts: matrices^zeroing
3078 
3079 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
3080 @*/
3081 int MatZeroRowsLocal(Mat mat,IS is,Scalar *diag)
3082 {
3083   int ierr;
3084   IS  newis;
3085 
3086   PetscFunctionBegin;
3087   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3088   PetscValidType(mat);
3089   MatPreallocated(mat);
3090   PetscValidHeaderSpecific(is,IS_COOKIE);
3091   if (diag) PetscValidScalarPointer(diag);
3092   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3093   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3094 
3095   if (mat->ops->zerorowslocal) {
3096     ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr);
3097   } else {
3098     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
3099     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
3100     ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
3101     ierr = ISDestroy(newis);CHKERRQ(ierr);
3102   }
3103   PetscFunctionReturn(0);
3104 }
3105 
3106 #undef __FUNC__
3107 #define __FUNC__ "MatGetSize"
3108 /*@
3109    MatGetSize - Returns the numbers of rows and columns in a matrix.
3110 
3111    Not Collective
3112 
3113    Input Parameter:
3114 .  mat - the matrix
3115 
3116    Output Parameters:
3117 +  m - the number of global rows
3118 -  n - the number of global columns
3119 
3120    Level: beginner
3121 
3122    Concepts: matrices^size
3123 
3124 .seealso: MatGetLocalSize()
3125 @*/
3126 int MatGetSize(Mat mat,int *m,int* n)
3127 {
3128   PetscFunctionBegin;
3129   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3130   if (m) *m = mat->M;
3131   if (n) *n = mat->N;
3132   PetscFunctionReturn(0);
3133 }
3134 
3135 #undef __FUNC__
3136 #define __FUNC__ "MatGetLocalSize"
3137 /*@
3138    MatGetLocalSize - Returns the number of rows and columns in a matrix
3139    stored locally.  This information may be implementation dependent, so
3140    use with care.
3141 
3142    Not Collective
3143 
3144    Input Parameters:
3145 .  mat - the matrix
3146 
3147    Output Parameters:
3148 +  m - the number of local rows
3149 -  n - the number of local columns
3150 
3151    Level: beginner
3152 
3153    Concepts: matrices^local size
3154 
3155 .seealso: MatGetSize()
3156 @*/
3157 int MatGetLocalSize(Mat mat,int *m,int* n)
3158 {
3159   PetscFunctionBegin;
3160   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3161   if (m) *m = mat->m;
3162   if (n) *n = mat->n;
3163   PetscFunctionReturn(0);
3164 }
3165 
3166 #undef __FUNC__
3167 #define __FUNC__ "MatGetOwnershipRange"
3168 /*@
3169    MatGetOwnershipRange - Returns the range of matrix rows owned by
3170    this processor, assuming that the matrix is laid out with the first
3171    n1 rows on the first processor, the next n2 rows on the second, etc.
3172    For certain parallel layouts this range may not be well defined.
3173 
3174    Not Collective
3175 
3176    Input Parameters:
3177 .  mat - the matrix
3178 
3179    Output Parameters:
3180 +  m - the global index of the first local row
3181 -  n - one more than the global index of the last local row
3182 
3183    Level: beginner
3184 
3185    Concepts: matrices^row ownership
3186 @*/
3187 int MatGetOwnershipRange(Mat mat,int *m,int* n)
3188 {
3189   int ierr;
3190 
3191   PetscFunctionBegin;
3192   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3193   PetscValidType(mat);
3194   MatPreallocated(mat);
3195   if (m) PetscValidIntPointer(m);
3196   if (n) PetscValidIntPointer(n);
3197   if (!mat->ops->getownershiprange) SETERRQ(PETSC_ERR_SUP,"");
3198   ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr);
3199   PetscFunctionReturn(0);
3200 }
3201 
3202 #undef __FUNC__
3203 #define __FUNC__ "MatILUFactorSymbolic"
3204 /*@
3205    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
3206    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
3207    to complete the factorization.
3208 
3209    Collective on Mat
3210 
3211    Input Parameters:
3212 +  mat - the matrix
3213 .  row - row permutation
3214 .  column - column permutation
3215 -  info - structure containing
3216 $      levels - number of levels of fill.
3217 $      expected fill - as ratio of original fill.
3218 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3219                 missing diagonal entries)
3220 
3221    Output Parameters:
3222 .  fact - new matrix that has been symbolically factored
3223 
3224    Notes:
3225    See the users manual for additional information about
3226    choosing the fill factor for better efficiency.
3227 
3228    Most users should employ the simplified SLES interface for linear solvers
3229    instead of working directly with matrix algebra routines such as this.
3230    See, e.g., SLESCreate().
3231 
3232    Level: developer
3233 
3234   Concepts: matrices^symbolic LU factorization
3235   Concepts: matrices^factorization
3236   Concepts: LU^symbolic factorization
3237 
3238 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3239           MatGetOrdering()
3240 
3241 @*/
3242 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact)
3243 {
3244   int ierr;
3245 
3246   PetscFunctionBegin;
3247   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3248   PetscValidType(mat);
3249   MatPreallocated(mat);
3250   PetscValidPointer(fact);
3251   if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",info->levels);
3252   if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3253   if (!mat->ops->ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"Only MatCreateMPIRowbs() matrices support parallel ILU");
3254   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3255   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3256 
3257   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3258   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3259   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3260   PetscFunctionReturn(0);
3261 }
3262 
3263 #undef __FUNC__
3264 #define __FUNC__ "MatIncompleteCholeskyFactorSymbolic"
3265 /*@
3266    MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete
3267    Cholesky factorization for a symmetric matrix.  Use
3268    MatCholeskyFactorNumeric() to complete the factorization.
3269 
3270    Collective on Mat
3271 
3272    Input Parameters:
3273 +  mat - the matrix
3274 .  perm - row and column permutation
3275 .  fill - levels of fill
3276 -  f - expected fill as ratio of original fill
3277 
3278    Output Parameter:
3279 .  fact - the factored matrix
3280 
3281    Notes:
3282    Currently only no-fill factorization is supported.
3283 
3284    Most users should employ the simplified SLES interface for linear solvers
3285    instead of working directly with matrix algebra routines such as this.
3286    See, e.g., SLESCreate().
3287 
3288    Level: developer
3289 
3290   Concepts: matrices^symbolic incomplete Cholesky factorization
3291   Concepts: matrices^factorization
3292   Concepts: Cholsky^symbolic factorization
3293 
3294 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
3295 @*/
3296 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,PetscReal f,int fill,Mat *fact)
3297 {
3298   int ierr;
3299 
3300   PetscFunctionBegin;
3301   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3302   PetscValidType(mat);
3303   MatPreallocated(mat);
3304   PetscValidPointer(fact);
3305   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3306   if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Fill negative %d",fill);
3307   if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",f);
3308   if (!mat->ops->incompletecholeskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,"Currently only MatCreateMPIRowbs() matrices support ICC in parallel");
3309   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3310 
3311   ierr = PetscLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3312   ierr = (*mat->ops->incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
3313   ierr = PetscLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3314   PetscFunctionReturn(0);
3315 }
3316 
3317 #undef __FUNC__
3318 #define __FUNC__ "MatGetArray"
3319 /*@C
3320    MatGetArray - Returns a pointer to the element values in the matrix.
3321    The result of this routine is dependent on the underlying matrix data
3322    structure, and may not even work for certain matrix types.  You MUST
3323    call MatRestoreArray() when you no longer need to access the array.
3324 
3325    Not Collective
3326 
3327    Input Parameter:
3328 .  mat - the matrix
3329 
3330    Output Parameter:
3331 .  v - the location of the values
3332 
3333    Currently returns an array only for the dense formats, giving access to
3334    the local portion of the matrix in the usual Fortran column-oriented format.
3335 
3336    Fortran Note:
3337    This routine is used differently from Fortran, e.g.,
3338 .vb
3339         Mat         mat
3340         Scalar      mat_array(1)
3341         PetscOffset i_mat
3342         int         ierr
3343         call MatGetArray(mat,mat_array,i_mat,ierr)
3344 
3345   C  Access first local entry in matrix; note that array is
3346   C  treated as one dimensional
3347         value = mat_array(i_mat + 1)
3348 
3349         [... other code ...]
3350         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3351 .ve
3352 
3353    See the Fortran chapter of the users manual and
3354    petsc/src/mat/examples/tests for details.
3355 
3356    Level: advanced
3357 
3358    Concepts: matrices^access array
3359 
3360 .seealso: MatRestoreArray(), MatGetArrayF90()
3361 @*/
3362 int MatGetArray(Mat mat,Scalar **v)
3363 {
3364   int ierr;
3365 
3366   PetscFunctionBegin;
3367   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3368   PetscValidType(mat);
3369   MatPreallocated(mat);
3370   PetscValidPointer(v);
3371   if (!mat->ops->getarray) SETERRQ(PETSC_ERR_SUP,"");
3372   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3373   PetscFunctionReturn(0);
3374 }
3375 
3376 #undef __FUNC__
3377 #define __FUNC__ "MatRestoreArray"
3378 /*@C
3379    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3380 
3381    Not Collective
3382 
3383    Input Parameter:
3384 +  mat - the matrix
3385 -  v - the location of the values
3386 
3387    Fortran Note:
3388    This routine is used differently from Fortran, e.g.,
3389 .vb
3390         Mat         mat
3391         Scalar      mat_array(1)
3392         PetscOffset i_mat
3393         int         ierr
3394         call MatGetArray(mat,mat_array,i_mat,ierr)
3395 
3396   C  Access first local entry in matrix; note that array is
3397   C  treated as one dimensional
3398         value = mat_array(i_mat + 1)
3399 
3400         [... other code ...]
3401         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3402 .ve
3403 
3404    See the Fortran chapter of the users manual and
3405    petsc/src/mat/examples/tests for details
3406 
3407    Level: advanced
3408 
3409 .seealso: MatGetArray(), MatRestoreArrayF90()
3410 @*/
3411 int MatRestoreArray(Mat mat,Scalar **v)
3412 {
3413   int ierr;
3414 
3415   PetscFunctionBegin;
3416   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3417   PetscValidType(mat);
3418   MatPreallocated(mat);
3419   PetscValidPointer(v);
3420   if (!mat->ops->restorearray) SETERRQ(PETSC_ERR_SUP,"");
3421   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3422   PetscFunctionReturn(0);
3423 }
3424 
3425 #undef __FUNC__
3426 #define __FUNC__ "MatGetSubMatrices"
3427 /*@C
3428    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3429    points to an array of valid matrices, they may be reused to store the new
3430    submatrices.
3431 
3432    Collective on Mat
3433 
3434    Input Parameters:
3435 +  mat - the matrix
3436 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3437 .  irow, icol - index sets of rows and columns to extract
3438 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3439 
3440    Output Parameter:
3441 .  submat - the array of submatrices
3442 
3443    Notes:
3444    MatGetSubMatrices() can extract only sequential submatrices
3445    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3446    to extract a parallel submatrix.
3447 
3448    When extracting submatrices from a parallel matrix, each processor can
3449    form a different submatrix by setting the rows and columns of its
3450    individual index sets according to the local submatrix desired.
3451 
3452    When finished using the submatrices, the user should destroy
3453    them with MatDestroySubMatrices().
3454 
3455    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3456    original matrix has not changed from that last call to MatGetSubMatrices().
3457 
3458    This routine creates the matrices submat; you should NOT create them before
3459    calling it.
3460 
3461    Fortran Note:
3462    The Fortran interface is slightly different from that given below; it
3463    requires one to pass in  as submat a Mat (integer) array of size at least m.
3464 
3465    Level: advanced
3466 
3467    Concepts: matrices^accessing submatrices
3468    Concepts: submatrices
3469 
3470 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3471 @*/
3472 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3473 {
3474   int ierr;
3475 
3476   PetscFunctionBegin;
3477   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3478   PetscValidType(mat);
3479   MatPreallocated(mat);
3480   if (!mat->ops->getsubmatrices) SETERRQ(PETSC_ERR_SUP,"");
3481   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3482 
3483   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3484   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3485   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3486 
3487   PetscFunctionReturn(0);
3488 }
3489 
3490 #undef __FUNC__
3491 #define __FUNC__ "MatDestroyMatrices"
3492 /*@C
3493    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3494 
3495    Collective on Mat
3496 
3497    Input Parameters:
3498 +  n - the number of local matrices
3499 -  mat - the matrices
3500 
3501    Level: advanced
3502 
3503 .seealso: MatGetSubMatrices()
3504 @*/
3505 int MatDestroyMatrices(int n,Mat **mat)
3506 {
3507   int ierr,i;
3508 
3509   PetscFunctionBegin;
3510   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n);
3511   PetscValidPointer(mat);
3512   for (i=0; i<n; i++) {
3513     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3514   }
3515   /* memory is allocated even if n = 0 */
3516   ierr = PetscFree(*mat);CHKERRQ(ierr);
3517   PetscFunctionReturn(0);
3518 }
3519 
3520 #undef __FUNC__
3521 #define __FUNC__ "MatIncreaseOverlap"
3522 /*@
3523    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3524    replaces the index sets by larger ones that represent submatrices with
3525    additional overlap.
3526 
3527    Collective on Mat
3528 
3529    Input Parameters:
3530 +  mat - the matrix
3531 .  n   - the number of index sets
3532 .  is  - the array of pointers to index sets
3533 -  ov  - the additional overlap requested
3534 
3535    Level: developer
3536 
3537    Concepts: overlap
3538    Concepts: ASM^computing overlap
3539 
3540 .seealso: MatGetSubMatrices()
3541 @*/
3542 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov)
3543 {
3544   int ierr;
3545 
3546   PetscFunctionBegin;
3547   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3548   PetscValidType(mat);
3549   MatPreallocated(mat);
3550   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3551   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3552 
3553   if (!ov) PetscFunctionReturn(0);
3554   if (!mat->ops->increaseoverlap) SETERRQ(PETSC_ERR_SUP,"");
3555   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3556   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
3557   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3558   PetscFunctionReturn(0);
3559 }
3560 
3561 #undef __FUNC__
3562 #define __FUNC__ "MatPrintHelp"
3563 /*@
3564    MatPrintHelp - Prints all the options for the matrix.
3565 
3566    Collective on Mat
3567 
3568    Input Parameter:
3569 .  mat - the matrix
3570 
3571    Options Database Keys:
3572 +  -help - Prints matrix options
3573 -  -h - Prints matrix options
3574 
3575    Level: developer
3576 
3577 .seealso: MatCreate(), MatCreateXXX()
3578 @*/
3579 int MatPrintHelp(Mat mat)
3580 {
3581   static PetscTruth called = PETSC_FALSE;
3582   int               ierr;
3583   MPI_Comm          comm;
3584 
3585   PetscFunctionBegin;
3586   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3587   PetscValidType(mat);
3588   MatPreallocated(mat);
3589 
3590   comm = mat->comm;
3591   if (!called) {
3592     ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr);
3593     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3594     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3595     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr);
3596     ierr = (*PetscHelpPrintf)(comm,"      -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr);
3597     ierr = (*PetscHelpPrintf)(comm,"      -display <name>: set alternate display\n");CHKERRQ(ierr);
3598     called = PETSC_TRUE;
3599   }
3600   if (mat->ops->printhelp) {
3601     ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
3602   }
3603   PetscFunctionReturn(0);
3604 }
3605 
3606 #undef __FUNC__
3607 #define __FUNC__ "MatGetBlockSize"
3608 /*@
3609    MatGetBlockSize - Returns the matrix block size; useful especially for the
3610    block row and block diagonal formats.
3611 
3612    Not Collective
3613 
3614    Input Parameter:
3615 .  mat - the matrix
3616 
3617    Output Parameter:
3618 .  bs - block size
3619 
3620    Notes:
3621    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
3622    Block row formats are MATSEQBAIJ, MATMPIBAIJ
3623 
3624    Level: intermediate
3625 
3626    Concepts: matrices^block size
3627 
3628 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
3629 @*/
3630 int MatGetBlockSize(Mat mat,int *bs)
3631 {
3632   int ierr;
3633 
3634   PetscFunctionBegin;
3635   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3636   PetscValidType(mat);
3637   MatPreallocated(mat);
3638   PetscValidIntPointer(bs);
3639   if (!mat->ops->getblocksize) SETERRQ(PETSC_ERR_SUP,"");
3640   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
3641   PetscFunctionReturn(0);
3642 }
3643 
3644 #undef __FUNC__
3645 #define __FUNC__ "MatGetRowIJ"
3646 /*@C
3647     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
3648 
3649    Collective on Mat
3650 
3651     Input Parameters:
3652 +   mat - the matrix
3653 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
3654 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3655                 symmetrized
3656 
3657     Output Parameters:
3658 +   n - number of rows in the (possibly compressed) matrix
3659 .   ia - the row pointers
3660 .   ja - the column indices
3661 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3662 
3663     Level: developer
3664 
3665 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3666 @*/
3667 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3668 {
3669   int ierr;
3670 
3671   PetscFunctionBegin;
3672   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3673   PetscValidType(mat);
3674   MatPreallocated(mat);
3675   if (ia) PetscValidIntPointer(ia);
3676   if (ja) PetscValidIntPointer(ja);
3677   PetscValidIntPointer(done);
3678   if (!mat->ops->getrowij) *done = PETSC_FALSE;
3679   else {
3680     *done = PETSC_TRUE;
3681     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3682   }
3683   PetscFunctionReturn(0);
3684 }
3685 
3686 #undef __FUNC__
3687 #define __FUNC__ "MatGetColumnIJ"
3688 /*@C
3689     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
3690 
3691     Collective on Mat
3692 
3693     Input Parameters:
3694 +   mat - the matrix
3695 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3696 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3697                 symmetrized
3698 
3699     Output Parameters:
3700 +   n - number of columns in the (possibly compressed) matrix
3701 .   ia - the column pointers
3702 .   ja - the row indices
3703 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
3704 
3705     Level: developer
3706 
3707 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3708 @*/
3709 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3710 {
3711   int ierr;
3712 
3713   PetscFunctionBegin;
3714   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3715   PetscValidType(mat);
3716   MatPreallocated(mat);
3717   if (ia) PetscValidIntPointer(ia);
3718   if (ja) PetscValidIntPointer(ja);
3719   PetscValidIntPointer(done);
3720 
3721   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
3722   else {
3723     *done = PETSC_TRUE;
3724     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3725   }
3726   PetscFunctionReturn(0);
3727 }
3728 
3729 #undef __FUNC__
3730 #define __FUNC__ "MatRestoreRowIJ"
3731 /*@C
3732     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
3733     MatGetRowIJ().
3734 
3735     Collective on Mat
3736 
3737     Input Parameters:
3738 +   mat - the matrix
3739 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3740 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3741                 symmetrized
3742 
3743     Output Parameters:
3744 +   n - size of (possibly compressed) matrix
3745 .   ia - the row pointers
3746 .   ja - the column indices
3747 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3748 
3749     Level: developer
3750 
3751 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
3752 @*/
3753 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3754 {
3755   int ierr;
3756 
3757   PetscFunctionBegin;
3758   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3759   PetscValidType(mat);
3760   MatPreallocated(mat);
3761   if (ia) PetscValidIntPointer(ia);
3762   if (ja) PetscValidIntPointer(ja);
3763   PetscValidIntPointer(done);
3764 
3765   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
3766   else {
3767     *done = PETSC_TRUE;
3768     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3769   }
3770   PetscFunctionReturn(0);
3771 }
3772 
3773 #undef __FUNC__
3774 #define __FUNC__ "MatRestoreColumnIJ"
3775 /*@C
3776     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
3777     MatGetColumnIJ().
3778 
3779     Collective on Mat
3780 
3781     Input Parameters:
3782 +   mat - the matrix
3783 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
3784 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
3785                 symmetrized
3786 
3787     Output Parameters:
3788 +   n - size of (possibly compressed) matrix
3789 .   ia - the column pointers
3790 .   ja - the row indices
3791 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
3792 
3793     Level: developer
3794 
3795 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
3796 @*/
3797 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
3798 {
3799   int ierr;
3800 
3801   PetscFunctionBegin;
3802   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3803   PetscValidType(mat);
3804   MatPreallocated(mat);
3805   if (ia) PetscValidIntPointer(ia);
3806   if (ja) PetscValidIntPointer(ja);
3807   PetscValidIntPointer(done);
3808 
3809   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
3810   else {
3811     *done = PETSC_TRUE;
3812     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
3813   }
3814   PetscFunctionReturn(0);
3815 }
3816 
3817 #undef __FUNC__
3818 #define __FUNC__ "MatColoringPatch"
3819 /*@C
3820     MatColoringPatch -Used inside matrix coloring routines that
3821     use MatGetRowIJ() and/or MatGetColumnIJ().
3822 
3823     Collective on Mat
3824 
3825     Input Parameters:
3826 +   mat - the matrix
3827 .   n   - number of colors
3828 -   colorarray - array indicating color for each column
3829 
3830     Output Parameters:
3831 .   iscoloring - coloring generated using colorarray information
3832 
3833     Level: developer
3834 
3835 .seealso: MatGetRowIJ(), MatGetColumnIJ()
3836 
3837 @*/
3838 int MatColoringPatch(Mat mat,int n,int *colorarray,ISColoring *iscoloring)
3839 {
3840   int ierr;
3841 
3842   PetscFunctionBegin;
3843   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3844   PetscValidType(mat);
3845   MatPreallocated(mat);
3846   PetscValidIntPointer(colorarray);
3847 
3848   if (!mat->ops->coloringpatch) {SETERRQ(PETSC_ERR_SUP,"");}
3849   else {
3850     ierr  = (*mat->ops->coloringpatch)(mat,n,colorarray,iscoloring);CHKERRQ(ierr);
3851   }
3852   PetscFunctionReturn(0);
3853 }
3854 
3855 
3856 #undef __FUNC__
3857 #define __FUNC__ "MatSetUnfactored"
3858 /*@
3859    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
3860 
3861    Collective on Mat
3862 
3863    Input Parameter:
3864 .  mat - the factored matrix to be reset
3865 
3866    Notes:
3867    This routine should be used only with factored matrices formed by in-place
3868    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
3869    format).  This option can save memory, for example, when solving nonlinear
3870    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
3871    ILU(0) preconditioner.
3872 
3873    Note that one can specify in-place ILU(0) factorization by calling
3874 .vb
3875      PCType(pc,PCILU);
3876      PCILUSeUseInPlace(pc);
3877 .ve
3878    or by using the options -pc_type ilu -pc_ilu_in_place
3879 
3880    In-place factorization ILU(0) can also be used as a local
3881    solver for the blocks within the block Jacobi or additive Schwarz
3882    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
3883    of these preconditioners in the users manual for details on setting
3884    local solver options.
3885 
3886    Most users should employ the simplified SLES interface for linear solvers
3887    instead of working directly with matrix algebra routines such as this.
3888    See, e.g., SLESCreate().
3889 
3890    Level: developer
3891 
3892 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
3893 
3894    Concepts: matrices^unfactored
3895 
3896 @*/
3897 int MatSetUnfactored(Mat mat)
3898 {
3899   int ierr;
3900 
3901   PetscFunctionBegin;
3902   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3903   PetscValidType(mat);
3904   MatPreallocated(mat);
3905   mat->factor = 0;
3906   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
3907   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
3908   PetscFunctionReturn(0);
3909 }
3910 
3911 /*MC
3912     MatGetArrayF90 - Accesses a matrix array from Fortran90.
3913 
3914     Synopsis:
3915     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
3916 
3917     Not collective
3918 
3919     Input Parameter:
3920 .   x - matrix
3921 
3922     Output Parameters:
3923 +   xx_v - the Fortran90 pointer to the array
3924 -   ierr - error code
3925 
3926     Example of Usage:
3927 .vb
3928       Scalar, pointer xx_v(:)
3929       ....
3930       call MatGetArrayF90(x,xx_v,ierr)
3931       a = xx_v(3)
3932       call MatRestoreArrayF90(x,xx_v,ierr)
3933 .ve
3934 
3935     Notes:
3936     Not yet supported for all F90 compilers
3937 
3938     Level: advanced
3939 
3940 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
3941 
3942     Concepts: matrices^accessing array
3943 
3944 M*/
3945 
3946 /*MC
3947     MatRestoreArrayF90 - Restores a matrix array that has been
3948     accessed with MatGetArrayF90().
3949 
3950     Synopsis:
3951     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
3952 
3953     Not collective
3954 
3955     Input Parameters:
3956 +   x - matrix
3957 -   xx_v - the Fortran90 pointer to the array
3958 
3959     Output Parameter:
3960 .   ierr - error code
3961 
3962     Example of Usage:
3963 .vb
3964        Scalar, pointer xx_v(:)
3965        ....
3966        call MatGetArrayF90(x,xx_v,ierr)
3967        a = xx_v(3)
3968        call MatRestoreArrayF90(x,xx_v,ierr)
3969 .ve
3970 
3971     Notes:
3972     Not yet supported for all F90 compilers
3973 
3974     Level: advanced
3975 
3976 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
3977 
3978 M*/
3979 
3980 
3981 #undef __FUNC__
3982 #define __FUNC__ "MatGetSubMatrix"
3983 /*@
3984     MatGetSubMatrix - Gets a single submatrix on the same number of processors
3985                       as the original matrix.
3986 
3987     Collective on Mat
3988 
3989     Input Parameters:
3990 +   mat - the original matrix
3991 .   isrow - rows this processor should obtain
3992 .   iscol - columns for all processors you wish to keep
3993 .   csize - number of columns "local" to this processor (does nothing for sequential
3994             matrices). This should match the result from VecGetLocalSize(x,...) if you
3995             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
3996 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3997 
3998     Output Parameter:
3999 .   newmat - the new submatrix, of the same type as the old
4000 
4001     Level: advanced
4002 
4003     Notes: the iscol argument MST be the same on each processor
4004 
4005       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
4006    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
4007    to this routine with a mat of the same nonzero structure will reuse the matrix
4008    generated the first time.
4009 
4010     Concepts: matrices^submatrices
4011 
4012 .seealso: MatGetSubMatrices()
4013 @*/
4014 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
4015 {
4016   int     ierr, size;
4017   Mat     *local;
4018 
4019   PetscFunctionBegin;
4020   PetscValidType(mat);
4021   MatPreallocated(mat);
4022   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4023 
4024   /* if original matrix is on just one processor then use submatrix generated */
4025   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
4026     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
4027     PetscFunctionReturn(0);
4028   } else if (!mat->ops->getsubmatrix && size == 1) {
4029     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
4030     *newmat = *local;
4031     ierr = PetscFree(local);CHKERRQ(ierr);
4032     PetscFunctionReturn(0);
4033   }
4034 
4035   if (!mat->ops->getsubmatrix) SETERRQ(PETSC_ERR_SUP,"Not currently implemented");
4036   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
4037   PetscFunctionReturn(0);
4038 }
4039 
4040 #undef __FUNC__
4041 #define __FUNC__ "MatGetMaps"
4042 /*@C
4043    MatGetMaps - Returns the maps associated with the matrix.
4044 
4045    Not Collective
4046 
4047    Input Parameter:
4048 .  mat - the matrix
4049 
4050    Output Parameters:
4051 +  rmap - the row (right) map
4052 -  cmap - the column (left) map
4053 
4054    Level: developer
4055 
4056    Concepts: maps^getting from matrix
4057 
4058 @*/
4059 int MatGetMaps(Mat mat,Map *rmap,Map *cmap)
4060 {
4061   int ierr;
4062 
4063   PetscFunctionBegin;
4064   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4065   PetscValidType(mat);
4066   MatPreallocated(mat);
4067   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
4068   PetscFunctionReturn(0);
4069 }
4070 
4071 /*
4072       Version that works for all PETSc matrices
4073 */
4074 #undef __FUNC__
4075 #define __FUNC__ "MatGetMaps_Petsc"
4076 int MatGetMaps_Petsc(Mat mat,Map *rmap,Map *cmap)
4077 {
4078   PetscFunctionBegin;
4079   if (rmap) *rmap = mat->rmap;
4080   if (cmap) *cmap = mat->cmap;
4081   PetscFunctionReturn(0);
4082 }
4083 
4084 #undef __FUNC__
4085 #define __FUNC__ "MatSetStashInitialSize"
4086 /*@
4087    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
4088    used during the assembly process to store values that belong to
4089    other processors.
4090 
4091    Not Collective
4092 
4093    Input Parameters:
4094 +  mat   - the matrix
4095 .  size  - the initial size of the stash.
4096 -  bsize - the initial size of the block-stash(if used).
4097 
4098    Options Database Keys:
4099 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
4100 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
4101 
4102    Level: intermediate
4103 
4104    Notes:
4105      The block-stash is used for values set with VecSetValuesBlocked() while
4106      the stash is used for values set with VecSetValues()
4107 
4108      Run with the option -log_info and look for output of the form
4109      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
4110      to determine the appropriate value, MM, to use for size and
4111      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
4112      to determine the value, BMM to use for bsize
4113 
4114    Concepts: stash^setting matrix size
4115    Concepts: matrices^stash
4116 
4117 @*/
4118 int MatSetStashInitialSize(Mat mat,int size, int bsize)
4119 {
4120   int ierr;
4121 
4122   PetscFunctionBegin;
4123   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4124   PetscValidType(mat);
4125   MatPreallocated(mat);
4126   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
4127   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
4128   PetscFunctionReturn(0);
4129 }
4130 
4131 #undef __FUNC__
4132 #define __FUNC__ "MatInterpolateAdd"
4133 /*@
4134    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
4135      the matrix
4136 
4137    Collective on Mat
4138 
4139    Input Parameters:
4140 +  mat   - the matrix
4141 .  x,y - the vectors
4142 -  w - where the result is stored
4143 
4144    Level: intermediate
4145 
4146    Notes:
4147     w may be the same vector as y.
4148 
4149     This allows one to use either the restriction or interpolation (its transpose)
4150     matrix to do the interpolation
4151 
4152     Concepts: interpolation
4153 
4154 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4155 
4156 @*/
4157 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
4158 {
4159   int M,N,ierr;
4160 
4161   PetscFunctionBegin;
4162   PetscValidType(A);
4163   MatPreallocated(A);
4164   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4165   if (N > M) {
4166     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
4167   } else {
4168     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
4169   }
4170   PetscFunctionReturn(0);
4171 }
4172 
4173 #undef __FUNC__
4174 #define __FUNC__ "MatInterpolate"
4175 /*@
4176    MatInterpolate - y = A*x or A'*x depending on the shape of
4177      the matrix
4178 
4179    Collective on Mat
4180 
4181    Input Parameters:
4182 +  mat   - the matrix
4183 -  x,y - the vectors
4184 
4185    Level: intermediate
4186 
4187    Notes:
4188     This allows one to use either the restriction or interpolation (its transpose)
4189     matrix to do the interpolation
4190 
4191    Concepts: matrices^interpolation
4192 
4193 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4194 
4195 @*/
4196 int MatInterpolate(Mat A,Vec x,Vec y)
4197 {
4198   int M,N,ierr;
4199 
4200   PetscFunctionBegin;
4201   PetscValidType(A);
4202   MatPreallocated(A);
4203   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4204   if (N > M) {
4205     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4206   } else {
4207     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4208   }
4209   PetscFunctionReturn(0);
4210 }
4211 
4212 #undef __FUNC__
4213 #define __FUNC__ "MatRestrict"
4214 /*@
4215    MatRestrict - y = A*x or A'*x
4216 
4217    Collective on Mat
4218 
4219    Input Parameters:
4220 +  mat   - the matrix
4221 -  x,y - the vectors
4222 
4223    Level: intermediate
4224 
4225    Notes:
4226     This allows one to use either the restriction or interpolation (its transpose)
4227     matrix to do the restriction
4228 
4229    Concepts: matrices^restriction
4230 
4231 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
4232 
4233 @*/
4234 int MatRestrict(Mat A,Vec x,Vec y)
4235 {
4236   int M,N,ierr;
4237 
4238   PetscFunctionBegin;
4239   PetscValidType(A);
4240   MatPreallocated(A);
4241   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4242   if (N > M) {
4243     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4244   } else {
4245     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4246   }
4247   PetscFunctionReturn(0);
4248 }
4249 
4250 #undef __FUNC__
4251 #define __FUNC__ "MatNullSpaceAttach"
4252 /*@C
4253    MatNullSpaceAttach - attaches a null space to a matrix.
4254         This null space will be removed from the resulting vector whenever
4255         MatMult() is called
4256 
4257    Collective on Mat
4258 
4259    Input Parameters:
4260 +  mat - the matrix
4261 -  nullsp - the null space object
4262 
4263    Level: developer
4264 
4265    Notes:
4266       Overwrites any previous null space that may have been attached
4267 
4268    Concepts: null space^attaching to matrix
4269 
4270 .seealso: MatCreate(), MatNullSpaceCreate()
4271 @*/
4272 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
4273 {
4274   int ierr = 0;
4275 
4276   PetscFunctionBegin;
4277   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4278   PetscValidType(mat);
4279   MatPreallocated(mat);
4280   PetscValidHeaderSpecific(nullsp,MATNULLSPACE_COOKIE);
4281 
4282   if (mat->nullsp) {
4283     ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr);
4284   }
4285   mat->nullsp = nullsp;
4286   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
4287   PetscFunctionReturn(0);
4288 }
4289 
4290 #undef __FUNC__
4291 #define __FUNC__ "MatIncompleteCholeskyFactor"
4292 /*@
4293    MatIncompleteCholeskyFactor - Performs in-place incomplete Cholesky factorization of matrix.
4294 
4295    Collective on Mat
4296 
4297    Input Parameters:
4298 +  mat - the matrix
4299 .  row - row/column permutation
4300 .  fill - expected fill factor >= 1.0
4301 -  level - level of fill, for ICC(k)
4302 
4303    Notes:
4304    Probably really in-place only when level of fill is zero, otherwise allocates
4305    new space to store factored matrix and deletes previous memory.
4306 
4307    Most users should employ the simplified SLES interface for linear solvers
4308    instead of working directly with matrix algebra routines such as this.
4309    See, e.g., SLESCreate().
4310 
4311    Level: developer
4312 
4313    Concepts: matrices^incomplete Cholesky factorization
4314    Concepts: Cholesky factorization
4315 
4316 .seealso: MatIncompleteCholeskyFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
4317 @*/
4318 int MatIncompleteCholeskyFactor(Mat mat,IS row,PetscReal fill,int level)
4319 {
4320   int ierr;
4321 
4322   PetscFunctionBegin;
4323   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4324   PetscValidType(mat);
4325   MatPreallocated(mat);
4326   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
4327   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4328   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4329   if (!mat->ops->incompletecholeskyfactor) SETERRQ(PETSC_ERR_SUP,"");
4330 
4331   ierr = (*mat->ops->incompletecholeskyfactor)(mat,row,fill,level);CHKERRQ(ierr);
4332   PetscFunctionReturn(0);
4333 }
4334