xref: /petsc/doc/manual/other.md (revision 7f296bb328fcd4c99f2da7bfe8ba7ed8a4ebceee)
1*7f296bb3SBarry Smith# Other PETSc Features
2*7f296bb3SBarry Smith
3*7f296bb3SBarry Smith## PETSc on a process subset
4*7f296bb3SBarry Smith
5*7f296bb3SBarry SmithUsers who wish to employ PETSc on only a subset of MPI processes
6*7f296bb3SBarry Smithwithin a larger parallel job, or who wish to use a “manager” process to
7*7f296bb3SBarry Smithcoordinate the work of “worker” PETSc processes, should specify an
8*7f296bb3SBarry Smithalternative communicator for `PETSC_COMM_WORLD` by directly setting
9*7f296bb3SBarry Smithits value, for example to use an existing MPI communicator `comm`,
10*7f296bb3SBarry Smith
11*7f296bb3SBarry Smith```
12*7f296bb3SBarry SmithPETSC_COMM_WORLD = comm; /* To use a previously-defined MPI_Comm */
13*7f296bb3SBarry Smith```
14*7f296bb3SBarry Smith
15*7f296bb3SBarry Smith*before* calling `PetscInitialize()`, but, obviously, after calling
16*7f296bb3SBarry Smith`MPI_Init()`.
17*7f296bb3SBarry Smith
18*7f296bb3SBarry Smith(sec_options)=
19*7f296bb3SBarry Smith
20*7f296bb3SBarry Smith## Runtime Options
21*7f296bb3SBarry Smith
22*7f296bb3SBarry SmithAllowing the user to modify parameters and options easily at runtime is
23*7f296bb3SBarry Smithvery desirable for many applications. PETSc provides a simple mechanism
24*7f296bb3SBarry Smithto enable such customization. To print a list of available options for a
25*7f296bb3SBarry Smithgiven program, simply specify the option `-help` at
26*7f296bb3SBarry Smithruntime, e.g.,
27*7f296bb3SBarry Smith
28*7f296bb3SBarry Smith```console
29*7f296bb3SBarry Smith$ mpiexec -n 1 ./ex1 -help
30*7f296bb3SBarry Smith```
31*7f296bb3SBarry Smith
32*7f296bb3SBarry SmithNote that all runtime options correspond to particular PETSc routines
33*7f296bb3SBarry Smiththat can be explicitly called from within a program to set compile-time
34*7f296bb3SBarry Smithdefaults. For many applications it is natural to use a combination of
35*7f296bb3SBarry Smithcompile-time and runtime choices. For example, when solving a linear
36*7f296bb3SBarry Smithsystem, one could explicitly specify use of the Krylov subspace
37*7f296bb3SBarry Smithsolver BiCGStab by calling
38*7f296bb3SBarry Smith
39*7f296bb3SBarry Smith```
40*7f296bb3SBarry SmithKSPSetType(ksp, KSPBCGS);
41*7f296bb3SBarry Smith```
42*7f296bb3SBarry Smith
43*7f296bb3SBarry SmithOne could then override this choice at runtime with the option
44*7f296bb3SBarry Smith
45*7f296bb3SBarry Smith```
46*7f296bb3SBarry Smith-ksp_type tfqmr
47*7f296bb3SBarry Smith```
48*7f296bb3SBarry Smith
49*7f296bb3SBarry Smithto select the Transpose-Free QMR algorithm. (See
50*7f296bb3SBarry Smith{any}`ch_ksp` for details.)
51*7f296bb3SBarry Smith
52*7f296bb3SBarry SmithThe remainder of this section discusses details of runtime options.
53*7f296bb3SBarry Smith
54*7f296bb3SBarry Smith(the_options_database_1)=
55*7f296bb3SBarry Smith
56*7f296bb3SBarry Smith### The Options Database
57*7f296bb3SBarry Smith
58*7f296bb3SBarry SmithEach PETSc process maintains a database of option names and values
59*7f296bb3SBarry Smith(stored as text strings). This database is generated with the command
60*7f296bb3SBarry Smith`PetscInitialize()`, which is listed below in its C/C++ and Fortran
61*7f296bb3SBarry Smithvariants, respectively:
62*7f296bb3SBarry Smith
63*7f296bb3SBarry Smith```
64*7f296bb3SBarry SmithPetscInitialize(int *argc, char ***args, const char *file, const char *help); // C
65*7f296bb3SBarry Smith```
66*7f296bb3SBarry Smith
67*7f296bb3SBarry Smith```fortran
68*7f296bb3SBarry Smithcall PetscInitialize(integer ierr) ! Fortran
69*7f296bb3SBarry Smith```
70*7f296bb3SBarry Smith
71*7f296bb3SBarry SmithThe arguments `argc` and `args` (in the C/C++ version only) are the
72*7f296bb3SBarry Smithaddresses of the usual command line arguments, while the `file` is a name
73*7f296bb3SBarry Smithof an optional file that can contain additional options. By default this file is
74*7f296bb3SBarry Smithcalled `.petscrc` in the user’s home directory. The user can also
75*7f296bb3SBarry Smithspecify options via the environmental variable `PETSC_OPTIONS`. The
76*7f296bb3SBarry Smithoptions are processed in the following order:
77*7f296bb3SBarry Smith
78*7f296bb3SBarry Smith1. file
79*7f296bb3SBarry Smith2. environmental variable
80*7f296bb3SBarry Smith3. command line
81*7f296bb3SBarry Smith
82*7f296bb3SBarry SmithThus, the command line options supersede the environmental variable
83*7f296bb3SBarry Smithoptions, which in turn supersede the options file.
84*7f296bb3SBarry Smith
85*7f296bb3SBarry SmithThe file format for specifying options is
86*7f296bb3SBarry Smith
87*7f296bb3SBarry Smith```none
88*7f296bb3SBarry Smith-optionname possible_value
89*7f296bb3SBarry Smith-anotheroptionname possible_value
90*7f296bb3SBarry Smith...
91*7f296bb3SBarry Smith```
92*7f296bb3SBarry Smith
93*7f296bb3SBarry SmithAll of the option names must begin with a dash (-) and have no
94*7f296bb3SBarry Smithintervening spaces. Note that the option values cannot have intervening
95*7f296bb3SBarry Smithspaces either, and tab characters cannot be used between the option
96*7f296bb3SBarry Smithnames and values. For
97*7f296bb3SBarry Smithuniformity throughout PETSc, we employ the format
98*7f296bb3SBarry Smith`-[prefix_]package_option` (for instance, `-ksp_type`,
99*7f296bb3SBarry Smith`-mat_view ::info`, or `-mg_levels_ksp_type`).
100*7f296bb3SBarry Smith
101*7f296bb3SBarry SmithUsers can specify an alias for any option name (to avoid typing the
102*7f296bb3SBarry Smithsometimes lengthy default name) by adding an alias to the `.petscrc`
103*7f296bb3SBarry Smithfile in the format
104*7f296bb3SBarry Smith
105*7f296bb3SBarry Smith```none
106*7f296bb3SBarry Smithalias -newname -oldname
107*7f296bb3SBarry Smith```
108*7f296bb3SBarry Smith
109*7f296bb3SBarry SmithFor example,
110*7f296bb3SBarry Smith
111*7f296bb3SBarry Smith```none
112*7f296bb3SBarry Smithalias -kspt -ksp_type
113*7f296bb3SBarry Smithalias -sd -start_in_debugger
114*7f296bb3SBarry Smith```
115*7f296bb3SBarry Smith
116*7f296bb3SBarry SmithComments can be placed in the `.petscrc` file by using `#` in the
117*7f296bb3SBarry Smithfirst column of a line.
118*7f296bb3SBarry Smith
119*7f296bb3SBarry Smith### Options Prefixes
120*7f296bb3SBarry Smith
121*7f296bb3SBarry SmithOptions prefixes allow specific objects to be controlled from the
122*7f296bb3SBarry Smithoptions database. For instance, `PCMG` gives prefixes to its nested
123*7f296bb3SBarry Smith`KSP` objects; one may control the coarse grid solver by adding the
124*7f296bb3SBarry Smith`mg_coarse` prefix, for example `-mg_coarse_ksp_type preonly`. One
125*7f296bb3SBarry Smithmay also use `KSPSetOptionsPrefix()`,`DMSetOptionsPrefix()` ,
126*7f296bb3SBarry Smith`SNESSetOptionsPrefix()`, `TSSetOptionsPrefix()`, and similar
127*7f296bb3SBarry Smithfunctions to assign custom prefixes, useful for applications with
128*7f296bb3SBarry Smithmultiple or nested solvers.
129*7f296bb3SBarry Smith
130*7f296bb3SBarry Smith### Adding options from a file
131*7f296bb3SBarry Smith
132*7f296bb3SBarry SmithPETSc can load additional options from a file using `PetscOptionsInsertFile()`,
133*7f296bb3SBarry Smithwhich can also be used from the command line, e.g. `-options_file my_options.opts`.
134*7f296bb3SBarry Smith
135*7f296bb3SBarry SmithOne can also use YAML files with `PetscOptionsInsertFileYAML()`.
136*7f296bb3SBarry SmithFor example, the following file:
137*7f296bb3SBarry Smith
138*7f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-options.yaml
139*7f296bb3SBarry Smith:language: yaml
140*7f296bb3SBarry Smith```
141*7f296bb3SBarry Smith
142*7f296bb3SBarry Smithcorresponds to the following PETSc options:
143*7f296bb3SBarry Smith
144*7f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/output/ex47_3_options.out
145*7f296bb3SBarry Smith:end-before: '#End'
146*7f296bb3SBarry Smith:language: none
147*7f296bb3SBarry Smith:start-after: '#'
148*7f296bb3SBarry Smith```
149*7f296bb3SBarry Smith
150*7f296bb3SBarry SmithWith `-options_file`, PETSc will parse the file as YAML if it ends in a standard
151*7f296bb3SBarry SmithYAML or JSON [^json] extension or if one uses a `:yaml` postfix,
152*7f296bb3SBarry Smithe.g. `-options_file my_options.yaml` or `-options_file my_options.txt:yaml`
153*7f296bb3SBarry Smith
154*7f296bb3SBarry SmithPETSc will also check the first line of the options file itself and
155*7f296bb3SBarry Smithparse the file as YAML if it matches certain criteria, for example.
156*7f296bb3SBarry Smith
157*7f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-yaml_tag
158*7f296bb3SBarry Smith:language: yaml
159*7f296bb3SBarry Smith```
160*7f296bb3SBarry Smith
161*7f296bb3SBarry Smithand
162*7f296bb3SBarry Smith
163*7f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-yaml_doc
164*7f296bb3SBarry Smith:language: yaml
165*7f296bb3SBarry Smith```
166*7f296bb3SBarry Smith
167*7f296bb3SBarry Smithboth correspond to options
168*7f296bb3SBarry Smith
169*7f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/output/ex47_2_auto.out
170*7f296bb3SBarry Smith:end-before: '#End'
171*7f296bb3SBarry Smith:language: none
172*7f296bb3SBarry Smith:start-after: '#'
173*7f296bb3SBarry Smith```
174*7f296bb3SBarry Smith
175*7f296bb3SBarry Smith### User-Defined PetscOptions
176*7f296bb3SBarry Smith
177*7f296bb3SBarry SmithAny subroutine in a PETSc program can add entries to the database with
178*7f296bb3SBarry Smiththe command
179*7f296bb3SBarry Smith
180*7f296bb3SBarry Smith```
181*7f296bb3SBarry SmithPetscOptionsSetValue(PetscOptions options, char *name, char *value);
182*7f296bb3SBarry Smith```
183*7f296bb3SBarry Smith
184*7f296bb3SBarry Smiththough this is rarely done. To locate options in the database, one
185*7f296bb3SBarry Smithshould use the commands
186*7f296bb3SBarry Smith
187*7f296bb3SBarry Smith```
188*7f296bb3SBarry SmithPetscOptionsHasName(PetscOptions options, char *pre, char *name, PetscBool *flg);
189*7f296bb3SBarry SmithPetscOptionsGetInt(PetscOptions options, char *pre, char *name, PetscInt *value, PetscBool *flg);
190*7f296bb3SBarry SmithPetscOptionsGetReal(PetscOptions options, char *pre, char *name, PetscReal *value, PetscBool *flg);
191*7f296bb3SBarry SmithPetscOptionsGetString(PetscOptions options, char *pre, char *name, char *value, size_t maxlen, PetscBool  *flg);
192*7f296bb3SBarry SmithPetscOptionsGetStringArray(PetscOptions options, char *pre, char *name, char **values, PetscInt *nmax, PetscBool *flg);
193*7f296bb3SBarry SmithPetscOptionsGetIntArray(PetscOptions options, char *pre, char *name, PetscInt *value, PetscInt *nmax, PetscBool *flg);
194*7f296bb3SBarry SmithPetscOptionsGetRealArray(PetscOptions options, char *pre, char *name, PetscReal *value, PetscInt *nmax, PetscBool *flg);
195*7f296bb3SBarry Smith```
196*7f296bb3SBarry Smith
197*7f296bb3SBarry SmithAll of these routines set `flg=PETSC_TRUE` if the corresponding option
198*7f296bb3SBarry Smithwas found, `flg=PETSC_FALSE` if it was not found. The optional
199*7f296bb3SBarry Smithargument `pre` indicates that the true name of the option is the given
200*7f296bb3SBarry Smithname (with the dash “-” removed) prepended by the prefix `pre`.
201*7f296bb3SBarry SmithUsually `pre` should be set to `NULL` (or `PETSC_NULL_CHARACTER`
202*7f296bb3SBarry Smithfor Fortran); its purpose is to allow someone to rename all the options
203*7f296bb3SBarry Smithin a package without knowing the names of the individual options. For
204*7f296bb3SBarry Smithexample, when using block Jacobi preconditioning, the `KSP` and `PC`
205*7f296bb3SBarry Smithmethods used on the individual blocks can be controlled via the options
206*7f296bb3SBarry Smith`-sub_ksp_type` and `-sub_pc_type`.
207*7f296bb3SBarry Smith
208*7f296bb3SBarry Smith### Keeping Track of Options
209*7f296bb3SBarry Smith
210*7f296bb3SBarry SmithOne useful means of keeping track of user-specified runtime options is
211*7f296bb3SBarry Smithuse of `-options_view`, which prints to `stdout` during
212*7f296bb3SBarry Smith`PetscFinalize()` a table of all runtime options that the user has
213*7f296bb3SBarry Smithspecified. A related option is `-options_left`, which prints the
214*7f296bb3SBarry Smithoptions table and indicates any options that have *not* been requested
215*7f296bb3SBarry Smithupon a call to `PetscFinalize()`. This feature is useful to check
216*7f296bb3SBarry Smithwhether an option has been activated for a particular PETSc object (such
217*7f296bb3SBarry Smithas a solver or matrix format), or whether an option name may have been
218*7f296bb3SBarry Smithaccidentally misspelled.
219*7f296bb3SBarry Smith
220*7f296bb3SBarry SmithThe option `-options_monitor` `<viewer>` turns on the default monitoring of options.
221*7f296bb3SBarry Smith`PetscOptionsMonitorSet()` can be used to provide custom monitors.
222*7f296bb3SBarry SmithThe option `-options_monitor_cancel` prevents any monitoring by monitors set with `PetscOptionsMonitorSet()` (but not that set with `-options_monitor`).
223*7f296bb3SBarry Smith
224*7f296bb3SBarry Smith(sec_viewers)=
225*7f296bb3SBarry Smith
226*7f296bb3SBarry Smith## Viewers: Looking at PETSc Objects
227*7f296bb3SBarry Smith
228*7f296bb3SBarry SmithPETSc employs a consistent scheme for examining, printing, and saving
229*7f296bb3SBarry Smithobjects through commands of the form
230*7f296bb3SBarry Smith
231*7f296bb3SBarry Smith```
232*7f296bb3SBarry SmithXXXView(XXX obj, PetscViewer viewer);
233*7f296bb3SBarry Smith```
234*7f296bb3SBarry Smith
235*7f296bb3SBarry SmithHere `obj` is a PETSc object of type `XXX`, where `XXX` is
236*7f296bb3SBarry Smith`Mat`, `Vec`, `SNES`, etc. There are several predefined viewers.
237*7f296bb3SBarry Smith
238*7f296bb3SBarry Smith- Passing in a zero (`0`) for the viewer causes the object to be
239*7f296bb3SBarry Smith  printed to the screen; this is useful when viewing an object in a
240*7f296bb3SBarry Smith  debugger but should be avoided in source code.
241*7f296bb3SBarry Smith- `PETSC_VIEWER_STDOUT_SELF` and `PETSC_VIEWER_STDOUT_WORLD` causes
242*7f296bb3SBarry Smith  the object to be printed to the screen.
243*7f296bb3SBarry Smith- `PETSC_VIEWER_DRAW_SELF` `PETSC_VIEWER_DRAW_WORLD` causes the
244*7f296bb3SBarry Smith  object to be drawn in a default X window.
245*7f296bb3SBarry Smith- Passing in a viewer obtained by `PetscViewerDrawOpen()` causes the
246*7f296bb3SBarry Smith  object to be displayed graphically. See
247*7f296bb3SBarry Smith  {any}`sec_graphics` for more on PETSc’s graphics support.
248*7f296bb3SBarry Smith- To save an object to a file in ASCII format, the user creates the
249*7f296bb3SBarry Smith  viewer object with the command
250*7f296bb3SBarry Smith  `PetscViewerASCIIOpen(MPI_Comm comm, char* file, PetscViewer *viewer)`.
251*7f296bb3SBarry Smith  This object is analogous to `PETSC_VIEWER_STDOUT_SELF` (for a
252*7f296bb3SBarry Smith  communicator of `MPI_COMM_SELF`) and `PETSC_VIEWER_STDOUT_WORLD`
253*7f296bb3SBarry Smith  (for a parallel communicator).
254*7f296bb3SBarry Smith- To save an object to a file in binary format, the user creates the
255*7f296bb3SBarry Smith  viewer object with the command
256*7f296bb3SBarry Smith  `PetscViewerBinaryOpen(MPI_Comm comm, char* file, PetscViewerBinaryType type, PetscViewer *viewer)`.
257*7f296bb3SBarry Smith  Details of binary I/O are discussed below.
258*7f296bb3SBarry Smith- Vector and matrix objects can be passed to a running MATLAB process
259*7f296bb3SBarry Smith  with a viewer created by
260*7f296bb3SBarry Smith  `PetscViewerSocketOpen(MPI_Comm comm, char *machine, int port, PetscViewer *viewer)`.
261*7f296bb3SBarry Smith  See {any}`sec_matlabsocket`.
262*7f296bb3SBarry Smith
263*7f296bb3SBarry SmithThe user can control the format of ASCII printed objects with viewers
264*7f296bb3SBarry Smithcreated by `PetscViewerASCIIOpen()` by calling
265*7f296bb3SBarry Smith
266*7f296bb3SBarry Smith```
267*7f296bb3SBarry SmithPetscViewerPushFormat(PetscViewer viewer, PetscViewerFormat format);
268*7f296bb3SBarry Smith```
269*7f296bb3SBarry Smith
270*7f296bb3SBarry SmithFormats include `PETSC_VIEWER_DEFAULT`, `PETSC_VIEWER_ASCII_MATLAB`,
271*7f296bb3SBarry Smithand `PETSC_VIEWER_ASCII_IMPL`. The implementation-specific format,
272*7f296bb3SBarry Smith`PETSC_VIEWER_ASCII_IMPL`, displays the object in the most natural way
273*7f296bb3SBarry Smithfor a particular implementation.
274*7f296bb3SBarry Smith
275*7f296bb3SBarry SmithThe routines
276*7f296bb3SBarry Smith
277*7f296bb3SBarry Smith```
278*7f296bb3SBarry SmithPetscViewerPushFormat(PetscViewer viewer, PetscViewerFormat format);
279*7f296bb3SBarry SmithPetscViewerPopFormat(PetscViewer viewer);
280*7f296bb3SBarry Smith```
281*7f296bb3SBarry Smith
282*7f296bb3SBarry Smithallow one to temporarily change the format of a viewer.
283*7f296bb3SBarry Smith
284*7f296bb3SBarry SmithAs discussed above, one can output PETSc objects in binary format by
285*7f296bb3SBarry Smithfirst opening a binary viewer with `PetscViewerBinaryOpen()` and then
286*7f296bb3SBarry Smithusing `MatView()`, `VecView()`, etc. The corresponding routines for
287*7f296bb3SBarry Smithinput of a binary object have the form `XXXLoad()`. In particular,
288*7f296bb3SBarry Smithmatrix and vector binary input is handled by the following routines:
289*7f296bb3SBarry Smith
290*7f296bb3SBarry Smith```
291*7f296bb3SBarry SmithMatLoad(Mat newmat, PetscViewer viewer);
292*7f296bb3SBarry SmithVecLoad(Vec newvec, PetscViewer viewer);
293*7f296bb3SBarry Smith```
294*7f296bb3SBarry Smith
295*7f296bb3SBarry SmithThese routines generate parallel matrices and vectors if the viewer’s
296*7f296bb3SBarry Smithcommunicator has more than one process. The particular matrix and vector
297*7f296bb3SBarry Smithformats are determined from the options database; see the manual pages
298*7f296bb3SBarry Smithfor details.
299*7f296bb3SBarry Smith
300*7f296bb3SBarry SmithOne can provide additional information about matrix data for matrices
301*7f296bb3SBarry Smithstored on disk by providing an optional file `matrixfilename.info`,
302*7f296bb3SBarry Smithwhere `matrixfilename` is the name of the file containing the matrix.
303*7f296bb3SBarry SmithThe format of the optional file is the same as the `.petscrc` file and
304*7f296bb3SBarry Smithcan (currently) contain the following:
305*7f296bb3SBarry Smith
306*7f296bb3SBarry Smith```none
307*7f296bb3SBarry Smith-matload_block_size <bs>
308*7f296bb3SBarry Smith```
309*7f296bb3SBarry Smith
310*7f296bb3SBarry SmithThe block size indicates the size of blocks to use if the matrix is read
311*7f296bb3SBarry Smithinto a block oriented data structure (for example, `MATMPIBAIJ`). The
312*7f296bb3SBarry Smithdiagonal information `s1,s2,s3,...` indicates which (block) diagonals
313*7f296bb3SBarry Smithin the matrix have nonzero values. The info file is automatically created
314*7f296bb3SBarry Smithwhen `VecView()` or `MatView()` is used with a binary viewer; hence if you
315*7f296bb3SBarry Smithsave a matrix with a given block size with `MatView()`, then a `MatLoad()`
316*7f296bb3SBarry Smithon that file will automatically use the saved block size.
317*7f296bb3SBarry Smith
318*7f296bb3SBarry Smith(sec_viewfromoptions)=
319*7f296bb3SBarry Smith
320*7f296bb3SBarry Smith### Viewing From Options
321*7f296bb3SBarry Smith
322*7f296bb3SBarry SmithCommand-line options provide a particularly convenient way to view PETSc
323*7f296bb3SBarry Smithobjects. All options of the form `-xxx_view` accept
324*7f296bb3SBarry Smithcolon(`:`)-separated compound arguments which specify a viewer type,
325*7f296bb3SBarry Smithformat, and/or destination (e.g. file name or socket) if appropriate.
326*7f296bb3SBarry SmithFor example, to quickly export a binary file containing a matrix, one
327*7f296bb3SBarry Smithmay use `-mat_view binary:matrix.out`, or to output to a
328*7f296bb3SBarry SmithMATLAB-compatible ASCII file, one may use
329*7f296bb3SBarry Smith`-mat_view ascii:matrix.m:ascii_matlab`. See the
330*7f296bb3SBarry Smith`PetscOptionsCreateViewer()` man page for full details, as well as the
331*7f296bb3SBarry Smith`XXXViewFromOptions()` man pages (for instance,
332*7f296bb3SBarry Smith`PetscDrawSetFromOptions()`) for many other convenient command-line
333*7f296bb3SBarry Smithoptions.
334*7f296bb3SBarry Smith
335*7f296bb3SBarry Smith### Using Viewers to Check Load Imbalance
336*7f296bb3SBarry Smith
337*7f296bb3SBarry SmithThe `PetscViewer` format `PETSC_VIEWER_LOAD_BALANCE` will cause certain
338*7f296bb3SBarry Smithobjects to display simple measures of their imbalance. For example
339*7f296bb3SBarry Smith
340*7f296bb3SBarry Smith```none
341*7f296bb3SBarry Smith-n 4 ./ex32 -ksp_view_mat ::load_balance
342*7f296bb3SBarry Smith```
343*7f296bb3SBarry Smith
344*7f296bb3SBarry Smithwill display
345*7f296bb3SBarry Smith
346*7f296bb3SBarry Smith```none
347*7f296bb3SBarry SmithNonzeros: Min 162  avg 168  max 174
348*7f296bb3SBarry Smith```
349*7f296bb3SBarry Smith
350*7f296bb3SBarry Smithindicating that one process has 162 nonzero entries in the matrix, the
351*7f296bb3SBarry Smithaverage number of nonzeros per process is 168 and the maximum number of
352*7f296bb3SBarry Smithnonzeros is 174. Similar for vectors one can see the load balancing
353*7f296bb3SBarry Smithwith, for example,
354*7f296bb3SBarry Smith
355*7f296bb3SBarry Smith```none
356*7f296bb3SBarry Smith-n 4 ./ex32 -ksp_view_rhs ::load_balance
357*7f296bb3SBarry Smith```
358*7f296bb3SBarry Smith
359*7f296bb3SBarry SmithThe measurements of load balancing can also be done within the program
360*7f296bb3SBarry Smithwith calls to the appropriate object viewer with the viewer format
361*7f296bb3SBarry Smith`PETSC_VIEWER_LOAD_BALANCE`.
362*7f296bb3SBarry Smith
363*7f296bb3SBarry Smith(sec_saws)=
364*7f296bb3SBarry Smith
365*7f296bb3SBarry Smith## Using SAWs with PETSc
366*7f296bb3SBarry Smith
367*7f296bb3SBarry SmithThe Scientific Application Web server, SAWs [^saws], allows one to monitor
368*7f296bb3SBarry Smithrunning PETSc applications from a browser. To use SAWs you must `configure` PETSc with
369*7f296bb3SBarry Smiththe option `--download-saws`. Options to use SAWs include
370*7f296bb3SBarry Smith
371*7f296bb3SBarry Smith- `-saws_options` - allows setting values in the PETSc options
372*7f296bb3SBarry Smith  database via the browser (works only on one process).
373*7f296bb3SBarry Smith- `-stack_view saws` - allows monitoring the current stack frame that
374*7f296bb3SBarry Smith  PETSc is in; refresh to see the new location.
375*7f296bb3SBarry Smith- `-snes_monitor_saws, -ksp_monitor_saws` - monitor the solvers’
376*7f296bb3SBarry Smith  iterations from the web browser.
377*7f296bb3SBarry Smith
378*7f296bb3SBarry SmithFor each of these you need to point your browser to
379*7f296bb3SBarry Smith`http://hostname:8080`, for example `http://localhost:8080`. Options
380*7f296bb3SBarry Smiththat control behavior of SAWs include
381*7f296bb3SBarry Smith
382*7f296bb3SBarry Smith- `-saws_log filename` - log all SAWs actions in a file.
383*7f296bb3SBarry Smith- `-saws_https certfile` - use HTTPS instead of HTTP with a
384*7f296bb3SBarry Smith  certificate.
385*7f296bb3SBarry Smith- `-saws_port_auto_select` - have SAWs pick a port number instead of
386*7f296bb3SBarry Smith  using 8080.
387*7f296bb3SBarry Smith- `-saws_port port` - use `port` instead of 8080.
388*7f296bb3SBarry Smith- `-saws_root rootdirectory` - local directory to which the SAWs
389*7f296bb3SBarry Smith  browser will have read access.
390*7f296bb3SBarry Smith- `-saws_local` - use the local file system to obtain the SAWS
391*7f296bb3SBarry Smith  javascript files (they much be in `rootdirectory/js`).
392*7f296bb3SBarry Smith
393*7f296bb3SBarry SmithAlso see the manual pages for `PetscSAWsBlock()`,
394*7f296bb3SBarry Smith`PetscObjectSAWsTakeAccess()`, `PetscObjectSAWsGrantAccess()`,
395*7f296bb3SBarry Smith`PetscObjectSAWsSetBlock()`, `PetscStackSAWsGrantAccess()`
396*7f296bb3SBarry Smith`PetscStackSAWsTakeAccess()`, `KSPMonitorSAWs()`, and
397*7f296bb3SBarry Smith`SNESMonitorSAWs()`.
398*7f296bb3SBarry Smith
399*7f296bb3SBarry Smith(sec_debugging)=
400*7f296bb3SBarry Smith
401*7f296bb3SBarry Smith## Debugging
402*7f296bb3SBarry Smith
403*7f296bb3SBarry SmithPETSc programs may be debugged using one of the two options below.
404*7f296bb3SBarry Smith
405*7f296bb3SBarry Smith- `-start_in_debugger` `[noxterm,dbx,xxgdb,xdb,xldb,lldb]`
406*7f296bb3SBarry Smith  `[-display name]` - start all processes in debugger
407*7f296bb3SBarry Smith- `-on_error_attach_debugger` `[noxterm,dbx,xxgdb,xdb,xldb,lldb]`
408*7f296bb3SBarry Smith  `[-display name]` - start debugger only on encountering an error
409*7f296bb3SBarry Smith
410*7f296bb3SBarry SmithNote that, in general, debugging MPI programs cannot be done in the
411*7f296bb3SBarry Smithusual manner of starting the programming in the debugger (because then
412*7f296bb3SBarry Smithit cannot set up the MPI communication and remote processes).
413*7f296bb3SBarry Smith
414*7f296bb3SBarry SmithBy default on Linux systems the GNU debugger `gdb` is used, on macOS systems `lldb` is used
415*7f296bb3SBarry Smith
416*7f296bb3SBarry SmithBy default, the debugger will be started in a new
417*7f296bb3SBarry Smithxterm (Apple Terminal on macOS), to enable running separate debuggers on each process, unless the
418*7f296bb3SBarry Smithoption `noxterm` is used. In order to handle the MPI startup phase,
419*7f296bb3SBarry Smiththe debugger command `cont` should be used to continue execution of
420*7f296bb3SBarry Smiththe program within the debugger. Rerunning the program through the
421*7f296bb3SBarry Smithdebugger requires terminating the first job and restarting the
422*7f296bb3SBarry Smithprocessor(s); the usual `run` option in the debugger will not
423*7f296bb3SBarry Smithcorrectly handle the MPI startup and should not be used. Not all
424*7f296bb3SBarry Smithdebuggers work on all machines, the user may have to experiment to find
425*7f296bb3SBarry Smithone that works correctly.
426*7f296bb3SBarry Smith
427*7f296bb3SBarry SmithYou can select a subset of the processes to be debugged (the rest just
428*7f296bb3SBarry Smithrun without the debugger) with the option
429*7f296bb3SBarry Smith
430*7f296bb3SBarry Smith```none
431*7f296bb3SBarry Smith-debugger_ranks rank1,rank2,...
432*7f296bb3SBarry Smith```
433*7f296bb3SBarry Smith
434*7f296bb3SBarry Smithwhere you simply list the ranks you want the debugger to run with.
435*7f296bb3SBarry Smith
436*7f296bb3SBarry Smith(sec_errors)=
437*7f296bb3SBarry Smith
438*7f296bb3SBarry Smith## Error Handling
439*7f296bb3SBarry Smith
440*7f296bb3SBarry SmithErrors are handled through the routine `PetscError()`. This routine
441*7f296bb3SBarry Smithchecks a stack of error handlers and calls the one on the top. If the
442*7f296bb3SBarry Smithstack is empty, it selects `PetscTraceBackErrorHandler()`, which tries
443*7f296bb3SBarry Smithto print a traceback. A new error handler can be put on the stack with
444*7f296bb3SBarry Smith
445*7f296bb3SBarry Smith```
446*7f296bb3SBarry SmithPetscPushErrorHandler(PetscErrorCode (*HandlerFunction)(int line, char *dir, char *file, char *message, int number, void*), void *HandlerContext)
447*7f296bb3SBarry Smith```
448*7f296bb3SBarry Smith
449*7f296bb3SBarry SmithThe arguments to `HandlerFunction()` are the line number where the
450*7f296bb3SBarry Smitherror occurred, the file in which the error was detected, the
451*7f296bb3SBarry Smithcorresponding directory, the error message, the error integer, and the
452*7f296bb3SBarry Smith`HandlerContext.` The routine
453*7f296bb3SBarry Smith
454*7f296bb3SBarry Smith```
455*7f296bb3SBarry SmithPetscPopErrorHandler()
456*7f296bb3SBarry Smith```
457*7f296bb3SBarry Smith
458*7f296bb3SBarry Smithremoves the last error handler and discards it.
459*7f296bb3SBarry Smith
460*7f296bb3SBarry SmithPETSc provides two additional error handlers besides
461*7f296bb3SBarry Smith`PetscTraceBackErrorHandler()`:
462*7f296bb3SBarry Smith
463*7f296bb3SBarry Smith```
464*7f296bb3SBarry SmithPetscAbortErrorHandler()
465*7f296bb3SBarry SmithPetscAttachDebuggerErrorHandler()
466*7f296bb3SBarry Smith```
467*7f296bb3SBarry Smith
468*7f296bb3SBarry SmithThe function `PetscAbortErrorHandler()` calls abort on encountering an
469*7f296bb3SBarry Smitherror, while `PetscAttachDebuggerErrorHandler()` attaches a debugger to the
470*7f296bb3SBarry Smithrunning process if an error is detected. At runtime, these error
471*7f296bb3SBarry Smithhandlers can be set with the options `-on_error_abort` or
472*7f296bb3SBarry Smith`-on_error_attach_debugger` `[noxterm, dbx, xxgdb, xldb]`
473*7f296bb3SBarry Smith`[-display DISPLAY]`.
474*7f296bb3SBarry Smith
475*7f296bb3SBarry SmithAll PETSc calls can be traced (useful for determining where a program is
476*7f296bb3SBarry Smithhanging without running in the debugger) with the option
477*7f296bb3SBarry Smith
478*7f296bb3SBarry Smith```none
479*7f296bb3SBarry Smith-log_trace [filename]
480*7f296bb3SBarry Smith```
481*7f296bb3SBarry Smith
482*7f296bb3SBarry Smithwhere `filename` is optional. By default the traces are printed to the
483*7f296bb3SBarry Smithscreen. This can also be set with the command
484*7f296bb3SBarry Smith`PetscLogTraceBegin(FILE*)`.
485*7f296bb3SBarry Smith
486*7f296bb3SBarry SmithIt is also possible to trap signals by using the command
487*7f296bb3SBarry Smith
488*7f296bb3SBarry Smith```
489*7f296bb3SBarry SmithPetscPushSignalHandler(PetscErrorCode (*Handler)(int, void *), void *ctx);
490*7f296bb3SBarry Smith```
491*7f296bb3SBarry Smith
492*7f296bb3SBarry SmithThe default handler `PetscSignalHandlerDefault()` calls
493*7f296bb3SBarry Smith`PetscError()` and then terminates. In general, a signal in PETSc
494*7f296bb3SBarry Smithindicates a catastrophic failure. Any error handler that the user
495*7f296bb3SBarry Smithprovides should try to clean up only before exiting. By default all
496*7f296bb3SBarry SmithPETSc programs turn on the default PETSc signal handler in `PetscInitialize()`,
497*7f296bb3SBarry Smiththis can be prevented with the option `-no_signal_handler` that can be provided on the command line,
498*7f296bb3SBarry Smithin the ~./petscrc file, or with the call
499*7f296bb3SBarry Smith
500*7f296bb3SBarry Smith```
501*7f296bb3SBarry SmithPetscCall(PetscOptionsSetValue(NULL, "-no_signal_handler", "true"));
502*7f296bb3SBarry Smith```
503*7f296bb3SBarry Smith
504*7f296bb3SBarry SmithOnce the first PETSc signal handler has been pushed it is impossible to go back to
505*7f296bb3SBarry Smithto a signal handler that was set directly by the user with the UNIX signal handler API or by
506*7f296bb3SBarry Smiththe loader.
507*7f296bb3SBarry Smith
508*7f296bb3SBarry SmithSome Fortran compilers/loaders cause, by default, a traceback of the Fortran call stack when a
509*7f296bb3SBarry Smithsegmentation violation occurs to be printed. This is handled by them setting a special signal handler
510*7f296bb3SBarry Smithwhen the program is started up. This feature is useful for debugging without needing to start up a debugger.
511*7f296bb3SBarry SmithIf `PetscPushSignalHandler()` has been called this traceback will not occur, hence if the Fortran traceback
512*7f296bb3SBarry Smithis desired one should put
513*7f296bb3SBarry Smith
514*7f296bb3SBarry Smith```
515*7f296bb3SBarry SmithPetscCallA(PetscOptionsSetValue(PETSC_NULL_OPTIONS,"-no_signal_handler","true",ierr))
516*7f296bb3SBarry Smith```
517*7f296bb3SBarry Smith
518*7f296bb3SBarry Smith**before** the call to `PetscInitialize()`. This prevents PETSc from defaulting to using a signal handler.
519*7f296bb3SBarry Smith
520*7f296bb3SBarry SmithThere is a separate signal handler for floating-point exceptions. The
521*7f296bb3SBarry Smithoption `-fp_trap` turns on the floating-point trap at runtime, and the
522*7f296bb3SBarry Smithroutine
523*7f296bb3SBarry Smith
524*7f296bb3SBarry Smith```
525*7f296bb3SBarry SmithPetscFPTrapPush(PetscFPTrap flag);
526*7f296bb3SBarry Smith```
527*7f296bb3SBarry Smith
528*7f296bb3SBarry Smithcan be used within a program. A `flag` of `PETSC_FP_TRAP_ON` indicates that
529*7f296bb3SBarry Smithfloating-point exceptions should be trapped, while a value of
530*7f296bb3SBarry Smith`PETSC_FP_TRAP_OFF` (the default) indicates that they should be
531*7f296bb3SBarry Smithignored.
532*7f296bb3SBarry Smith
533*7f296bb3SBarry Smith```
534*7f296bb3SBarry SmithPetscFPTrapPop(void);
535*7f296bb3SBarry Smith```
536*7f296bb3SBarry Smith
537*7f296bb3SBarry Smithshould be used to revert to the previous handling of floating point exceptions before the call to `PetscFPTrapPush()`.
538*7f296bb3SBarry Smith
539*7f296bb3SBarry SmithA small set of macros is used to make the error handling lightweight.
540*7f296bb3SBarry SmithThese macros are used throughout the PETSc libraries and can be employed
541*7f296bb3SBarry Smithby the application programmer as well. When an error is first detected,
542*7f296bb3SBarry Smithone should set it by calling
543*7f296bb3SBarry Smith
544*7f296bb3SBarry Smith```
545*7f296bb3SBarry SmithSETERRQ(MPI_Comm comm, PetscErrorCode flag, char *message);
546*7f296bb3SBarry Smith```
547*7f296bb3SBarry Smith
548*7f296bb3SBarry SmithThe user should check the return codes for all PETSc routines (and
549*7f296bb3SBarry Smithpossibly user-defined routines as well) with
550*7f296bb3SBarry Smith
551*7f296bb3SBarry Smith```
552*7f296bb3SBarry SmithPetscCall(PetscRoutine(...));
553*7f296bb3SBarry Smith```
554*7f296bb3SBarry Smith
555*7f296bb3SBarry SmithLikewise, all memory allocations should be checked with
556*7f296bb3SBarry Smith
557*7f296bb3SBarry Smith```
558*7f296bb3SBarry SmithPetscCall(PetscMalloc1(n, &ptr));
559*7f296bb3SBarry Smith```
560*7f296bb3SBarry Smith
561*7f296bb3SBarry SmithIf this procedure is followed throughout all of the user’s libraries and
562*7f296bb3SBarry Smithcodes, any error will by default generate a clean traceback of the
563*7f296bb3SBarry Smithlocation of the error.
564*7f296bb3SBarry Smith
565*7f296bb3SBarry SmithNote that the macro `PETSC_FUNCTION_NAME` is used to keep track of
566*7f296bb3SBarry Smithroutine names during error tracebacks. Users need not worry about this
567*7f296bb3SBarry Smithmacro in their application codes; however, users can take advantage of
568*7f296bb3SBarry Smiththis feature if desired by setting this macro before each user-defined
569*7f296bb3SBarry Smithroutine that may call `SETERRQ()`, `PetscCall()`. A simple example of
570*7f296bb3SBarry Smithusage is given below.
571*7f296bb3SBarry Smith
572*7f296bb3SBarry Smith```
573*7f296bb3SBarry SmithPetscErrorCode MyRoutine1()
574*7f296bb3SBarry Smith{
575*7f296bb3SBarry Smith    /* Declarations Here */
576*7f296bb3SBarry Smith    PetscFunctionBeginUser;
577*7f296bb3SBarry Smith    /* code here */
578*7f296bb3SBarry Smith    PetscFunctionReturn(PETSC_SUCCESS);
579*7f296bb3SBarry Smith}
580*7f296bb3SBarry Smith```
581*7f296bb3SBarry Smith
582*7f296bb3SBarry Smith(sec_complex)=
583*7f296bb3SBarry Smith
584*7f296bb3SBarry Smith## Numbers
585*7f296bb3SBarry Smith
586*7f296bb3SBarry SmithPETSc supports the use of complex numbers in application programs
587*7f296bb3SBarry Smithwritten in C, C++, and Fortran. To do so, we employ either the C99
588*7f296bb3SBarry Smith`complex` type or the C++ versions of the PETSc libraries in which the
589*7f296bb3SBarry Smithbasic “scalar” datatype, given in PETSc codes by `PetscScalar`, is
590*7f296bb3SBarry Smithdefined as `complex` (or `complex<double>` for machines using
591*7f296bb3SBarry Smithtemplated complex class libraries). To work with complex numbers, the
592*7f296bb3SBarry Smithuser should run `configure` with the additional option
593*7f296bb3SBarry Smith`--with-scalar-type=complex`. The
594*7f296bb3SBarry Smith{doc}`installation instructions </install/index>`
595*7f296bb3SBarry Smithprovide detailed instructions for installing PETSc. You can use
596*7f296bb3SBarry Smith`--with-clanguage=c` (the default) to use the C99 complex numbers or
597*7f296bb3SBarry Smith`--with-clanguage=c++` to use the C++ complex type [^cxx-note].
598*7f296bb3SBarry Smith
599*7f296bb3SBarry SmithRecall that each configuration of the PETSc libraries is stored in a different
600*7f296bb3SBarry Smithdirectory, given by `$PETSC_DIR/$PETSC_ARCH`
601*7f296bb3SBarry Smithaccording to the architecture. Thus, the libraries for complex numbers
602*7f296bb3SBarry Smithare maintained separately from those for real numbers. When using any of
603*7f296bb3SBarry Smiththe complex numbers versions of PETSc, *all* vector and matrix elements
604*7f296bb3SBarry Smithare treated as complex, even if their imaginary components are zero. Of
605*7f296bb3SBarry Smithcourse, one can elect to use only the real parts of the complex numbers
606*7f296bb3SBarry Smithwhen using the complex versions of the PETSc libraries; however, when
607*7f296bb3SBarry Smithworking *only* with real numbers in a code, one should use a version of
608*7f296bb3SBarry SmithPETSc for real numbers for best efficiency.
609*7f296bb3SBarry Smith
610*7f296bb3SBarry SmithThe program
611*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11.c.html">KSP Tutorial ex11</a>
612*7f296bb3SBarry Smithsolves a linear system with a complex coefficient matrix. Its Fortran
613*7f296bb3SBarry Smithcounterpart is
614*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11f.F90.html">KSP Tutorial ex11f</a>.
615*7f296bb3SBarry Smith
616*7f296bb3SBarry Smith## Parallel Communication
617*7f296bb3SBarry Smith
618*7f296bb3SBarry SmithWhen used in a message-passing environment, all communication within
619*7f296bb3SBarry SmithPETSc is done through MPI, the message-passing interface standard
620*7f296bb3SBarry Smith{cite}`mpi-final`. Any file that includes `petscsys.h` (or
621*7f296bb3SBarry Smithany other PETSc include file) can freely use any MPI routine.
622*7f296bb3SBarry Smith
623*7f296bb3SBarry Smith(sec_graphics)=
624*7f296bb3SBarry Smith
625*7f296bb3SBarry Smith## Graphics
626*7f296bb3SBarry Smith
627*7f296bb3SBarry SmithThe PETSc graphics library is not intended to compete with high-quality
628*7f296bb3SBarry Smithgraphics packages. Instead, it is intended to be easy to use
629*7f296bb3SBarry Smithinteractively with PETSc programs. We urge users to generate their
630*7f296bb3SBarry Smithpublication-quality graphics using a professional graphics package. If a
631*7f296bb3SBarry Smithuser wants to hook certain packages into PETSc, he or she should send a
632*7f296bb3SBarry Smithmessage to
633*7f296bb3SBarry Smith[petsc-maint@mcs.anl.gov](mailto:petsc-maint@mcs.anl.gov); we
634*7f296bb3SBarry Smithwill see whether it is reasonable to try to provide direct interfaces.
635*7f296bb3SBarry Smith
636*7f296bb3SBarry Smith### Windows as PetscViewers
637*7f296bb3SBarry Smith
638*7f296bb3SBarry SmithFor drawing predefined PETSc objects such as matrices and vectors, one
639*7f296bb3SBarry Smithmay first create a viewer using the command
640*7f296bb3SBarry Smith
641*7f296bb3SBarry Smith```
642*7f296bb3SBarry SmithPetscViewerDrawOpen(MPI_Comm comm, char *display, char *title, int x, int y, int w, int h, PetscViewer *viewer);
643*7f296bb3SBarry Smith```
644*7f296bb3SBarry Smith
645*7f296bb3SBarry SmithThis viewer may be passed to any of the `XXXView()` routines.
646*7f296bb3SBarry SmithAlternately, one may use command-line options to quickly specify viewer
647*7f296bb3SBarry Smithformats, including `PetscDraw`-based ones; see
648*7f296bb3SBarry Smith{any}`sec_viewfromoptions`.
649*7f296bb3SBarry Smith
650*7f296bb3SBarry SmithTo draw directly into the viewer, one must obtain the `PetscDraw`
651*7f296bb3SBarry Smithobject with the command
652*7f296bb3SBarry Smith
653*7f296bb3SBarry Smith```
654*7f296bb3SBarry SmithPetscViewerDrawGetDraw(PetscViewer viewer, PetscDraw *draw);
655*7f296bb3SBarry Smith```
656*7f296bb3SBarry Smith
657*7f296bb3SBarry SmithThen one can call any of the `PetscDrawXXX()` commands on the `draw`
658*7f296bb3SBarry Smithobject. If one obtains the `draw` object in this manner, one does not
659*7f296bb3SBarry Smithcall the `PetscDrawOpenX()` command discussed below.
660*7f296bb3SBarry Smith
661*7f296bb3SBarry SmithPredefined viewers, `PETSC_VIEWER_DRAW_WORLD` and
662*7f296bb3SBarry Smith`PETSC_VIEWER_DRAW_SELF`, may be used at any time. Their initial use
663*7f296bb3SBarry Smithwill cause the appropriate window to be created.
664*7f296bb3SBarry Smith
665*7f296bb3SBarry SmithImplementations using OpenGL, TikZ, and other formats may be selected
666*7f296bb3SBarry Smithwith `PetscDrawSetType()`. PETSc can also produce movies; see
667*7f296bb3SBarry Smith`PetscDrawSetSaveMovie()`, and note that command-line options can also
668*7f296bb3SBarry Smithbe convenient; see the `PetscDrawSetFromOptions()` man page.
669*7f296bb3SBarry Smith
670*7f296bb3SBarry SmithBy default, PETSc drawing tools employ a private colormap, which
671*7f296bb3SBarry Smithremedies the problem of poor color choices for contour plots due to an
672*7f296bb3SBarry Smithexternal program’s mangling of the colormap. Unfortunately, this may
673*7f296bb3SBarry Smithcause flashing of colors as the mouse is moved between the PETSc windows
674*7f296bb3SBarry Smithand other windows. Alternatively, a shared colormap can be used via the
675*7f296bb3SBarry Smithoption `-draw_x_shared_colormap`.
676*7f296bb3SBarry Smith
677*7f296bb3SBarry Smith### Simple PetscDrawing
678*7f296bb3SBarry Smith
679*7f296bb3SBarry SmithWith the default format, one can open a window that is not associated
680*7f296bb3SBarry Smithwith a viewer directly under the X11 Window System with the
681*7f296bb3SBarry Smithcommand
682*7f296bb3SBarry Smith
683*7f296bb3SBarry Smith```
684*7f296bb3SBarry SmithPetscDrawCreate(MPI_Comm comm, char *display, char *title, int x, int y, int w, int h, PetscDraw *win);
685*7f296bb3SBarry SmithPetscDrawSetFromOptions(win);
686*7f296bb3SBarry Smith```
687*7f296bb3SBarry Smith
688*7f296bb3SBarry SmithAll drawing routines are performed relative to the window’s coordinate
689*7f296bb3SBarry Smithsystem and viewport. By default, the drawing coordinates are from
690*7f296bb3SBarry Smith`(0,0)` to `(1,1)`, where `(0,0)` indicates the lower left corner
691*7f296bb3SBarry Smithof the window. The application program can change the window coordinates
692*7f296bb3SBarry Smithwith the command
693*7f296bb3SBarry Smith
694*7f296bb3SBarry Smith```
695*7f296bb3SBarry SmithPetscDrawSetCoordinates(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr);
696*7f296bb3SBarry Smith```
697*7f296bb3SBarry Smith
698*7f296bb3SBarry SmithBy default, graphics will be drawn in the entire window. To restrict the
699*7f296bb3SBarry Smithdrawing to a portion of the window, one may use the command
700*7f296bb3SBarry Smith
701*7f296bb3SBarry Smith```
702*7f296bb3SBarry SmithPetscDrawSetViewPort(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr);
703*7f296bb3SBarry Smith```
704*7f296bb3SBarry Smith
705*7f296bb3SBarry SmithThese arguments, which indicate the fraction of the window in which the
706*7f296bb3SBarry Smithdrawing should be done, must satisfy
707*7f296bb3SBarry Smith$0 \leq {\tt xl} \leq {\tt xr} \leq 1$ and
708*7f296bb3SBarry Smith$0 \leq {\tt yl} \leq {\tt yr} \leq 1.$
709*7f296bb3SBarry Smith
710*7f296bb3SBarry SmithTo draw a line, one uses the command
711*7f296bb3SBarry Smith
712*7f296bb3SBarry Smith```
713*7f296bb3SBarry SmithPetscDrawLine(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl);
714*7f296bb3SBarry Smith```
715*7f296bb3SBarry Smith
716*7f296bb3SBarry SmithThe argument `cl` indicates the color (which is an integer between 0
717*7f296bb3SBarry Smithand 255) of the line. A list of predefined colors may be found in
718*7f296bb3SBarry Smith`include/petscdraw.h` and includes `PETSC_DRAW_BLACK`,
719*7f296bb3SBarry Smith`PETSC_DRAW_RED`, `PETSC_DRAW_BLUE` etc.
720*7f296bb3SBarry Smith
721*7f296bb3SBarry SmithTo ensure that all graphics actually have been displayed, one should use
722*7f296bb3SBarry Smiththe command
723*7f296bb3SBarry Smith
724*7f296bb3SBarry Smith```
725*7f296bb3SBarry SmithPetscDrawFlush(PetscDraw win);
726*7f296bb3SBarry Smith```
727*7f296bb3SBarry Smith
728*7f296bb3SBarry SmithWhen displaying by using double buffering, which is set with the command
729*7f296bb3SBarry Smith
730*7f296bb3SBarry Smith```
731*7f296bb3SBarry SmithPetscDrawSetDoubleBuffer(PetscDraw win);
732*7f296bb3SBarry Smith```
733*7f296bb3SBarry Smith
734*7f296bb3SBarry Smith*all* processes must call
735*7f296bb3SBarry Smith
736*7f296bb3SBarry Smith```
737*7f296bb3SBarry SmithPetscDrawFlush(PetscDraw win);
738*7f296bb3SBarry Smith```
739*7f296bb3SBarry Smith
740*7f296bb3SBarry Smithin order to swap the buffers. From the options database one may use
741*7f296bb3SBarry Smith`-draw_pause` `n`, which causes the PETSc application to pause `n`
742*7f296bb3SBarry Smithseconds at each `PetscDrawPause()`. A time of `-1` indicates that
743*7f296bb3SBarry Smiththe application should pause until receiving mouse input from the user.
744*7f296bb3SBarry Smith
745*7f296bb3SBarry SmithText can be drawn with commands
746*7f296bb3SBarry Smith
747*7f296bb3SBarry Smith```
748*7f296bb3SBarry SmithPetscDrawString(PetscDraw win, PetscReal x, PetscReal y, int color, char *text);
749*7f296bb3SBarry SmithPetscDrawStringVertical(PetscDraw win, PetscReal x, PetscReal y, int color, const char *text);
750*7f296bb3SBarry SmithPetscDrawStringCentered(PetscDraw win, PetscReal x, PetscReal y, int color, const char *text);
751*7f296bb3SBarry SmithPetscDrawStringBoxed(PetscDraw draw, PetscReal sxl, PetscReal syl, int sc, int bc, const char text[], PetscReal *w, PetscReal *h);
752*7f296bb3SBarry Smith```
753*7f296bb3SBarry Smith
754*7f296bb3SBarry SmithThe user can set the text font size or determine it with the commands
755*7f296bb3SBarry Smith
756*7f296bb3SBarry Smith```
757*7f296bb3SBarry SmithPetscDrawStringSetSize(PetscDraw win, PetscReal width, PetscReal height);
758*7f296bb3SBarry SmithPetscDrawStringGetSize(PetscDraw win, PetscReal *width, PetscReal *height);
759*7f296bb3SBarry Smith```
760*7f296bb3SBarry Smith
761*7f296bb3SBarry Smith### Line Graphs
762*7f296bb3SBarry Smith
763*7f296bb3SBarry SmithPETSc includes a set of routines for manipulating simple two-dimensional
764*7f296bb3SBarry Smithgraphs. These routines, which begin with `PetscDrawAxisDraw()`, are
765*7f296bb3SBarry Smithusually not used directly by the application programmer. Instead, the
766*7f296bb3SBarry Smithprogrammer employs the line graph routines to draw simple line graphs.
767*7f296bb3SBarry SmithAs shown in the {ref}`listing below <listing_draw_test_ex3>`, line
768*7f296bb3SBarry Smithgraphs are created with the command
769*7f296bb3SBarry Smith
770*7f296bb3SBarry Smith```
771*7f296bb3SBarry SmithPetscDrawLGCreate(PetscDraw win, PetscInt ncurves, PetscDrawLG *ctx);
772*7f296bb3SBarry Smith```
773*7f296bb3SBarry Smith
774*7f296bb3SBarry SmithThe argument `ncurves` indicates how many curves are to be drawn.
775*7f296bb3SBarry SmithPoints can be added to each of the curves with the command
776*7f296bb3SBarry Smith
777*7f296bb3SBarry Smith```
778*7f296bb3SBarry SmithPetscDrawLGAddPoint(PetscDrawLG ctx, PetscReal *x, PetscReal *y);
779*7f296bb3SBarry Smith```
780*7f296bb3SBarry Smith
781*7f296bb3SBarry SmithThe arguments `x` and `y` are arrays containing the next point value
782*7f296bb3SBarry Smithfor each curve. Several points for each curve may be added with
783*7f296bb3SBarry Smith
784*7f296bb3SBarry Smith```
785*7f296bb3SBarry SmithPetscDrawLGAddPoints(PetscDrawLG ctx, PetscInt n, PetscReal **x, PetscReal **y);
786*7f296bb3SBarry Smith```
787*7f296bb3SBarry Smith
788*7f296bb3SBarry SmithThe line graph is drawn (or redrawn) with the command
789*7f296bb3SBarry Smith
790*7f296bb3SBarry Smith```
791*7f296bb3SBarry SmithPetscDrawLGDraw(PetscDrawLG ctx);
792*7f296bb3SBarry Smith```
793*7f296bb3SBarry Smith
794*7f296bb3SBarry SmithA line graph that is no longer needed can be destroyed with the command
795*7f296bb3SBarry Smith
796*7f296bb3SBarry Smith```
797*7f296bb3SBarry SmithPetscDrawLGDestroy(PetscDrawLG *ctx);
798*7f296bb3SBarry Smith```
799*7f296bb3SBarry Smith
800*7f296bb3SBarry SmithTo plot new curves, one can reset a linegraph with the command
801*7f296bb3SBarry Smith
802*7f296bb3SBarry Smith```
803*7f296bb3SBarry SmithPetscDrawLGReset(PetscDrawLG ctx);
804*7f296bb3SBarry Smith```
805*7f296bb3SBarry Smith
806*7f296bb3SBarry SmithThe line graph automatically determines the range of values to display
807*7f296bb3SBarry Smithon the two axes. The user can change these defaults with the command
808*7f296bb3SBarry Smith
809*7f296bb3SBarry Smith```
810*7f296bb3SBarry SmithPetscDrawLGSetLimits(PetscDrawLG ctx, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax);
811*7f296bb3SBarry Smith```
812*7f296bb3SBarry Smith
813*7f296bb3SBarry SmithIt is also possible to change the display of the axes and to label them.
814*7f296bb3SBarry SmithThis procedure is done by first obtaining the axes context with the
815*7f296bb3SBarry Smithcommand
816*7f296bb3SBarry Smith
817*7f296bb3SBarry Smith```
818*7f296bb3SBarry SmithPetscDrawLGGetAxis(PetscDrawLG ctx, PetscDrawAxis *axis);
819*7f296bb3SBarry Smith```
820*7f296bb3SBarry Smith
821*7f296bb3SBarry SmithOne can set the axes’ colors and labels, respectively, by using the
822*7f296bb3SBarry Smithcommands
823*7f296bb3SBarry Smith
824*7f296bb3SBarry Smith```
825*7f296bb3SBarry SmithPetscDrawAxisSetColors(PetscDrawAxis axis, int axis_lines, int ticks, int text);
826*7f296bb3SBarry SmithPetscDrawAxisSetLabels(PetscDrawAxis axis, char *top, char *x, char *y);
827*7f296bb3SBarry Smith```
828*7f296bb3SBarry Smith
829*7f296bb3SBarry SmithIt is possible to turn off all graphics with the option `-nox`. This
830*7f296bb3SBarry Smithwill prevent any windows from being opened or any drawing actions to be
831*7f296bb3SBarry Smithdone. This is useful for running large jobs when the graphics overhead
832*7f296bb3SBarry Smithis too large, or for timing.
833*7f296bb3SBarry Smith
834*7f296bb3SBarry SmithThe full example, <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/draw/tests/ex3.c.html">Draw Test ex3</a>,
835*7f296bb3SBarry Smithfollows.
836*7f296bb3SBarry Smith
837*7f296bb3SBarry Smith(listing_draw_test_ex3)=
838*7f296bb3SBarry Smith
839*7f296bb3SBarry Smith:::{admonition} Listing: `src/classes/draw/tests/ex3.c`
840*7f296bb3SBarry Smith:name: snes-ex1
841*7f296bb3SBarry Smith
842*7f296bb3SBarry Smith```{literalinclude} /../src/sys/classes/draw/tests/ex3.c
843*7f296bb3SBarry Smith:end-before: /*TEST
844*7f296bb3SBarry Smith```
845*7f296bb3SBarry Smith:::
846*7f296bb3SBarry Smith
847*7f296bb3SBarry Smith### Graphical Convergence Monitor
848*7f296bb3SBarry Smith
849*7f296bb3SBarry SmithFor both the linear and nonlinear solvers default routines allow one to
850*7f296bb3SBarry Smithgraphically monitor convergence of the iterative method. These are
851*7f296bb3SBarry Smithaccessed via the command line with `-ksp_monitor draw::draw_lg` and
852*7f296bb3SBarry Smith`-snes_monitor draw::draw_lg`. See also
853*7f296bb3SBarry Smith{any}`sec_kspmonitor` and {any}`sec_snesmonitor`.
854*7f296bb3SBarry Smith
855*7f296bb3SBarry Smith### Disabling Graphics at Compile Time
856*7f296bb3SBarry Smith
857*7f296bb3SBarry SmithTo disable all X-window-based graphics, run `configure` with the
858*7f296bb3SBarry Smithadditional option `--with-x=0`
859*7f296bb3SBarry Smith
860*7f296bb3SBarry Smith(sec_developer_environments)=
861*7f296bb3SBarry Smith
862*7f296bb3SBarry Smith# Developer Environments
863*7f296bb3SBarry Smith
864*7f296bb3SBarry Smith## Emacs Users
865*7f296bb3SBarry Smith
866*7f296bb3SBarry SmithMany PETSc developers use Emacs, which can be used as a "simple" text editor or a comprehensive development environment.
867*7f296bb3SBarry SmithFor a more integrated development environment, we recommend using [lsp-mode](https://emacs-lsp.github.io/lsp-mode/) (or [eglot](https://github.com/joaotavora/eglot)) with [clangd](https://clangd.llvm.org/).
868*7f296bb3SBarry SmithThe most convenient way to teach clangd what compilation flags to use is to install [Bear](https://github.com/rizsotto/Bear) ("build ear") and run:
869*7f296bb3SBarry Smith
870*7f296bb3SBarry Smith```
871*7f296bb3SBarry Smithbear make -B
872*7f296bb3SBarry Smith```
873*7f296bb3SBarry Smith
874*7f296bb3SBarry Smithwhich will do a complete rebuild (`-B`) of PETSc and capture the compilation commands in a file named `compile_commands.json`, which will be automatically picked up by clangd.
875*7f296bb3SBarry SmithYou can use the same procedure when building examples or your own project.
876*7f296bb3SBarry SmithIt can also be used with any other editor that supports clangd, including VS Code and Vim.
877*7f296bb3SBarry SmithWhen lsp-mode is accompanied by [flycheck](https://www.flycheck.org/en/latest/), Emacs will provide real-time feedback and syntax checking, along with refactoring tools provided by clangd.
878*7f296bb3SBarry Smith
879*7f296bb3SBarry SmithThe easiest way to install packages in recent Emacs is to use the "Options" menu to select "Manage Emacs Packages".
880*7f296bb3SBarry Smith
881*7f296bb3SBarry Smith### Tags
882*7f296bb3SBarry Smith
883*7f296bb3SBarry SmithIt is sometimes useful to cross-reference tags across projects.
884*7f296bb3SBarry SmithRegardless of whether you use lsp-mode, it can be useful to use [GNU Global](https://www.gnu.org/software/global/) (install `gtags`) to provide reverse lookups (e.g. find all call sites
885*7f296bb3SBarry Smithfor a given function) across all projects you might work on/browse.
886*7f296bb3SBarry SmithTags for PETSc can be generated by running `make allgtags` from `$PETSC_DIR`, or one can generate tags for all projects by running a command such as
887*7f296bb3SBarry Smith
888*7f296bb3SBarry Smith```none
889*7f296bb3SBarry Smithfind $PETSC_DIR/{include,src,tutorials,$PETSC_ARCH/include} any/other/paths \
890*7f296bb3SBarry Smith   -regex '.*\.\(cc\|hh\|cpp\|cxx\|C\|hpp\|c\|h\|cu\)$' \
891*7f296bb3SBarry Smith   | grep -v ftn-auto | gtags -f -
892*7f296bb3SBarry Smith```
893*7f296bb3SBarry Smith
894*7f296bb3SBarry Smithfrom your home directory or wherever you keep source code. If you are
895*7f296bb3SBarry Smithmaking large changes, it is useful to either set this up to run as a
896*7f296bb3SBarry Smithcron job or to make a convenient alias so that refreshing is easy. Then
897*7f296bb3SBarry Smithadd the following to `~/.emacs` to enable gtags and specify key bindings.
898*7f296bb3SBarry Smith
899*7f296bb3SBarry Smith```emacs
900*7f296bb3SBarry Smith(when (require 'gtags)
901*7f296bb3SBarry Smith  (global-set-key (kbd "C-c f") 'gtags-find-file)
902*7f296bb3SBarry Smith  (global-set-key (kbd "C-c .") 'gtags-find-tag)
903*7f296bb3SBarry Smith  (global-set-key (kbd "C-c r") 'gtags-find-rtag)
904*7f296bb3SBarry Smith  (global-set-key (kbd "C-c ,") 'gtags-pop-stack))
905*7f296bb3SBarry Smith(add-hook 'c-mode-common-hook
906*7f296bb3SBarry Smith          '(lambda () (gtags-mode t))) ; Or add to existing hook
907*7f296bb3SBarry Smith```
908*7f296bb3SBarry Smith
909*7f296bb3SBarry SmithA more basic alternative to the GNU Global (`gtags`) approach that does not require adding packages is to use
910*7f296bb3SBarry Smiththe builtin `etags` feature. First, run `make alletags` from the
911*7f296bb3SBarry SmithPETSc home directory to generate the file `$PETSC_DIR/TAGS`, and
912*7f296bb3SBarry Smiththen from within Emacs, run
913*7f296bb3SBarry Smith
914*7f296bb3SBarry Smith```none
915*7f296bb3SBarry SmithM-x visit-tags-table
916*7f296bb3SBarry Smith```
917*7f296bb3SBarry Smith
918*7f296bb3SBarry Smithwhere `M` denotes the Emacs Meta key, and enter the name of the
919*7f296bb3SBarry Smith`TAGS` file. Then the command `M-.` will cause Emacs to find the
920*7f296bb3SBarry Smithfile and line number where a desired PETSc function is defined. Any
921*7f296bb3SBarry Smithstring in any of the PETSc files can be found with the command `M-x tags-search`.
922*7f296bb3SBarry SmithTo find repeated occurrences, one can simply use `M-,` to find the next occurrence.
923*7f296bb3SBarry Smith
924*7f296bb3SBarry Smith## VS Code Users
925*7f296bb3SBarry Smith
926*7f296bb3SBarry Smith[VS Code](https://code.visualstudio.com/) (unlike {ref}`sec_visual_studio`, described below) is an open-source editor with a rich extension ecosystem.
927*7f296bb3SBarry SmithIt has [excellent integration](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd) with clangd and will automatically pick up `compile_commands.json`
928*7f296bb3SBarry Smithas produced by a command such as `bear make -B` (see {ref}`sec_developer_environments`).
929*7f296bb3SBarry SmithIf you have no prior attachment to a specific code editor, we recommend trying VS Code.
930*7f296bb3SBarry Smith
931*7f296bb3SBarry Smith## Vi and Vim Users
932*7f296bb3SBarry Smith
933*7f296bb3SBarry SmithThis section lists helpful Vim commands for PETSc. Ones that configure Vim can be placed
934*7f296bb3SBarry Smithin a `.vimrc` file in the top of the PETSc directory and will be loaded automatically.
935*7f296bb3SBarry Smith
936*7f296bb3SBarry SmithVim has configurable keymaps: all of the "command mode" commands given that start with
937*7f296bb3SBarry Smitha colon (such as `:help`) can be assigned to short sequences in "normal mode," which
938*7f296bb3SBarry Smithis how most Vim users use their most frequently used commands.
939*7f296bb3SBarry Smith
940*7f296bb3SBarry SmithSee the {ref}`sec_developer_environments` discussion above for configuration of clangd, which
941*7f296bb3SBarry Smithprovides integrated development environment.
942*7f296bb3SBarry Smith
943*7f296bb3SBarry Smith### Tags
944*7f296bb3SBarry Smith
945*7f296bb3SBarry SmithThe `tags` feature can be used to search PETSc files quickly and efficiently.
946*7f296bb3SBarry SmithTo use this feature, one should first check if the file, `$PETSC_DIR/CTAGS`
947*7f296bb3SBarry Smithexists. If this file is not present, it should be generated by running `make
948*7f296bb3SBarry Smithalletags` from the PETSc home directory. Once the file exists, from Vi/Vim the
949*7f296bb3SBarry Smithuser should issue the command
950*7f296bb3SBarry Smith
951*7f296bb3SBarry Smith```none
952*7f296bb3SBarry Smith:set tags=CTAGS
953*7f296bb3SBarry Smith```
954*7f296bb3SBarry Smith
955*7f296bb3SBarry Smithfrom the `$PETSC_DIR` directory and enter the name of the `CTAGS` file. The
956*7f296bb3SBarry Smithcommand `:tag functionname` will cause Vi/Vim to open the file and line
957*7f296bb3SBarry Smithnumber where a desired PETSc function is defined in the current window.
958*7f296bb3SBarry Smith`<Ctrl-o>` will return the screen to your previous location.
959*7f296bb3SBarry Smith
960*7f296bb3SBarry SmithThe command `:stag functionname` will split the current window and then open
961*7f296bb3SBarry Smiththe file and line number for that function in one half. Some prefer this because
962*7f296bb3SBarry Smithit is easier to compare the file you are editing to the function definition this way.
963*7f296bb3SBarry Smith
964*7f296bb3SBarry Smith### Cscope and gtags
965*7f296bb3SBarry Smith
966*7f296bb3SBarry SmithVim can also use the `cscope` utility to navigate source code. One useful thing
967*7f296bb3SBarry Smithit can do that the basic `tags` feature can't is search for references to a symbol,
968*7f296bb3SBarry Smithrather than its definition, which can be useful for refactoring. The command
969*7f296bb3SBarry Smith
970*7f296bb3SBarry Smith```none
971*7f296bb3SBarry Smith:cs find s functionname
972*7f296bb3SBarry Smith```
973*7f296bb3SBarry Smith
974*7f296bb3SBarry Smithopens a list of all of the places the function is called in PETSc, and opens
975*7f296bb3SBarry Smiththe file and line that you choose. The variant `:scs find s functionname`
976*7f296bb3SBarry Smithdoes the same but splits the window like `stag`.
977*7f296bb3SBarry Smith
978*7f296bb3SBarry SmithThe PETSc makefile does not have a command for building a cscope database, but
979*7f296bb3SBarry SmithGNU Global is cross-compatible with cscope: call `make allgtags` to make the
980*7f296bb3SBarry Smithgtags database, and run the commands
981*7f296bb3SBarry Smith
982*7f296bb3SBarry Smith```none
983*7f296bb3SBarry Smith:set csprg=gtags-cscope
984*7f296bb3SBarry Smith:cs add GTAGS
985*7f296bb3SBarry Smith```
986*7f296bb3SBarry Smith
987*7f296bb3SBarry Smith### Quickfix
988*7f296bb3SBarry Smith
989*7f296bb3SBarry SmithRather than exiting editing a file to build the library and check for errors or
990*7f296bb3SBarry Smithwarnings, calling `:make` runs the make command without leaving Vim and
991*7f296bb3SBarry Smithcollects the errors and warnings in a "quickfix" window. Move the cursor to
992*7f296bb3SBarry Smithone of the errors or warnings in the quickfix window and press `<Enter>` and
993*7f296bb3SBarry Smiththe main window will jump to the file and line with the error. The following
994*7f296bb3SBarry Smithcommands filter lines of out PETSc's make output that can clutter the quickfix window:
995*7f296bb3SBarry Smith
996*7f296bb3SBarry Smith```none
997*7f296bb3SBarry Smith:set efm^=%-GStarting\ make\ run\ on\ %.%#
998*7f296bb3SBarry Smith:set efm^=%-GMachine\ characteristics:\ %.%#
999*7f296bb3SBarry Smith:set efm^=%-G#define\ PETSC%.%#
1000*7f296bb3SBarry Smith```
1001*7f296bb3SBarry Smith
1002*7f296bb3SBarry Smith### Autocompletion and snippets
1003*7f296bb3SBarry Smith
1004*7f296bb3SBarry SmithAutocompletion of long function names can be helpful when working with PETSc.
1005*7f296bb3SBarry SmithIf you have a tags file, you can press `<Ctrl-N>` when you have partially
1006*7f296bb3SBarry Smithtyped a word to bring up a list of potential completions that you can choose
1007*7f296bb3SBarry Smithfrom with `<Tab>`.
1008*7f296bb3SBarry Smith
1009*7f296bb3SBarry SmithMore powerful autocompletion, such as completing the fieldname of a struct, is
1010*7f296bb3SBarry Smithavailable from external plugins that can be added to Vim, such as [SuperTab](https://github.com/ervandew/supertab), [VimCompletesMe](https://github.com/ackyshake/VimCompletesMe), or [YouCompleteMe](https://github.com/ycm-core/YouCompleteMe).
1011*7f296bb3SBarry Smith
1012*7f296bb3SBarry SmithAlong the same lines, plugins can be added that fill in the boilerplate
1013*7f296bb3SBarry Smithassociated with PETSc programming with code snippets. One such tool is
1014*7f296bb3SBarry Smith[UltiSnips](https://github.com/sirver/UltiSnips).
1015*7f296bb3SBarry Smith
1016*7f296bb3SBarry Smith### LSP for Vim
1017*7f296bb3SBarry Smith
1018*7f296bb3SBarry SmithSeveral plugins provide the equivalent of emacs' lsp-mode: YouCompleteMe,
1019*7f296bb3SBarry Smithmentioned above, is one; another popular one is [ale](https://github.com/dense-analysis/ale). These can check for syntax errors,
1020*7f296bb3SBarry Smithcheck for compilation errors in the background, and provide sophisticated tools
1021*7f296bb3SBarry Smithfor refactoring. Like lsp-mode, they also rely on a compilation database, so
1022*7f296bb3SBarry Smith`bear -- make -B` should be used as well to generate the file
1023*7f296bb3SBarry Smith`compile_commands.json`.
1024*7f296bb3SBarry Smith
1025*7f296bb3SBarry SmithSee [online tutorials](http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html)
1026*7f296bb3SBarry Smithfor additional Vi/Vim options.
1027*7f296bb3SBarry Smith
1028*7f296bb3SBarry Smith## Eclipse Users
1029*7f296bb3SBarry Smith
1030*7f296bb3SBarry SmithIf you are interested in developing code that uses PETSc from Eclipse or
1031*7f296bb3SBarry Smithdeveloping PETSc in Eclipse and have knowledge of how to do indexing and
1032*7f296bb3SBarry Smithbuild libraries in Eclipse, please contact us at
1033*7f296bb3SBarry Smith[petsc-dev@mcs.anl.gov](mailto:petsc-dev@mcs.anl.gov).
1034*7f296bb3SBarry Smith
1035*7f296bb3SBarry SmithOne way to index and build PETSc in Eclipse is as follows.
1036*7f296bb3SBarry Smith
1037*7f296bb3SBarry Smith1. Open
1038*7f296bb3SBarry Smith   “File$\rightarrow$Import$\rightarrow$Git$\rightarrow$Projects
1039*7f296bb3SBarry Smith   from Git”. In the next two panels, you can either add your existing
1040*7f296bb3SBarry Smith   local repository or download PETSc from Bitbucket by providing the
1041*7f296bb3SBarry Smith   URL. Most Eclipse distributions come with Git support. If not,
1042*7f296bb3SBarry Smith   install the EGit plugin. When importing the project, select the
1043*7f296bb3SBarry Smith   wizard “Import as general project”.
1044*7f296bb3SBarry Smith2. Right-click on the project (or the “File” menu on top) and select
1045*7f296bb3SBarry Smith   “New $\rightarrow$ Convert to a C/C++ Project (Adds C/C++
1046*7f296bb3SBarry Smith   Nature)”. In the setting window, choose “C Project” and specify the
1047*7f296bb3SBarry Smith   project type as “Shared Library”.
1048*7f296bb3SBarry Smith3. Right-click on the C project and open the “Properties” panel. Under
1049*7f296bb3SBarry Smith   “C/C++ Build $\rightarrow$ Builder Settings”, set the Build
1050*7f296bb3SBarry Smith   directory to `$PETSC_DIR` and make sure “Generate Makefiles
1051*7f296bb3SBarry Smith   automatically” is unselected. Under the section “C/C++
1052*7f296bb3SBarry Smith   General$\rightarrow$Paths and Symbols”, add the PETSc paths
1053*7f296bb3SBarry Smith   to “Includes”.
1054*7f296bb3SBarry Smith
1055*7f296bb3SBarry Smith> ```none
1056*7f296bb3SBarry Smith>      $PETSC_DIR/include
1057*7f296bb3SBarry Smith>      $PETSC_DIR/$PETSC_ARCH/include
1058*7f296bb3SBarry Smith>
1059*7f296bb3SBarry Smith> Under the section “C/C++ General\ :math:`\rightarrow`\ index”, choose
1060*7f296bb3SBarry Smith> “Use active build configuration”.
1061*7f296bb3SBarry Smith> ```
1062*7f296bb3SBarry Smith
1063*7f296bb3SBarry Smith1. Configure PETSc normally outside Eclipse to generate a makefile and
1064*7f296bb3SBarry Smith   then build the project in Eclipse. The source code will be parsed by
1065*7f296bb3SBarry Smith   Eclipse.
1066*7f296bb3SBarry Smith
1067*7f296bb3SBarry SmithIf you launch Eclipse from the Dock on Mac OS X, `.bashrc` will not be
1068*7f296bb3SBarry Smithloaded (a known OS X behavior, for security reasons). This will be a
1069*7f296bb3SBarry Smithproblem if you set the environment variables `$PETSC_DIR` and
1070*7f296bb3SBarry Smith`$PETSC_ARCH` in `.bashrc`. A solution which involves replacing the
1071*7f296bb3SBarry Smithexecutable can be found at
1072*7f296bb3SBarry Smith`` `/questions/829749/launch-mac-eclipse-with-environment-variables-set `` \</questions/829749/launch-mac-eclipse-with-environment-variables-set>\`\_\_.
1073*7f296bb3SBarry SmithAlternatively, you can add `$PETSC_DIR` and `$PETSC_ARCH` manually
1074*7f296bb3SBarry Smithunder “Properties $\rightarrow$ C/C++ Build $\rightarrow$
1075*7f296bb3SBarry SmithEnvironment”.
1076*7f296bb3SBarry Smith
1077*7f296bb3SBarry SmithTo allow an Eclipse code to compile with the PETSc include files and
1078*7f296bb3SBarry Smithlink with the PETSc libraries, a PETSc user has suggested the following.
1079*7f296bb3SBarry Smith
1080*7f296bb3SBarry Smith1. Right-click on your C project and select “Properties
1081*7f296bb3SBarry Smith   $\rightarrow$ C/C++ Build $\rightarrow$ Settings”
1082*7f296bb3SBarry Smith2. A new window on the righthand side appears with various settings
1083*7f296bb3SBarry Smith   options. Select “Includes” and add the required PETSc paths,
1084*7f296bb3SBarry Smith
1085*7f296bb3SBarry Smith```none
1086*7f296bb3SBarry Smith$PETSC_DIR/include
1087*7f296bb3SBarry Smith$PETSC_DIR/$PETSC_ARCH/include
1088*7f296bb3SBarry Smith```
1089*7f296bb3SBarry Smith
1090*7f296bb3SBarry Smith1. Select “Libraries” under the header Linker and set the library search
1091*7f296bb3SBarry Smith   path:
1092*7f296bb3SBarry Smith
1093*7f296bb3SBarry Smith```none
1094*7f296bb3SBarry Smith   $PETSC_DIR/$PETSC_ARCH/lib
1095*7f296bb3SBarry Smith
1096*7f296bb3SBarry Smithand the libraries, for example
1097*7f296bb3SBarry Smith```
1098*7f296bb3SBarry Smith
1099*7f296bb3SBarry Smith```none
1100*7f296bb3SBarry Smithm, petsc, stdc++, mpichxx, mpich, lapack, blas, gfortran, dl, rt,gcc_s, pthread, X11
1101*7f296bb3SBarry Smith```
1102*7f296bb3SBarry Smith
1103*7f296bb3SBarry SmithAnother PETSc user has provided the following steps to build an Eclipse
1104*7f296bb3SBarry Smithindex for PETSc that can be used with their own code, without compiling
1105*7f296bb3SBarry SmithPETSc source into their project.
1106*7f296bb3SBarry Smith
1107*7f296bb3SBarry Smith1. In the user project source directory, create a symlink to the PETSC
1108*7f296bb3SBarry Smith   `src/` directory.
1109*7f296bb3SBarry Smith2. Refresh the project explorer in Eclipse, so the new symlink is
1110*7f296bb3SBarry Smith   followed.
1111*7f296bb3SBarry Smith3. Right-click on the project in the project explorer, and choose “Index
1112*7f296bb3SBarry Smith   $\rightarrow$ Rebuild”. The index should now be built.
1113*7f296bb3SBarry Smith4. Right-click on the PETSc symlink in the project explorer, and choose
1114*7f296bb3SBarry Smith   “Exclude from build...” to make sure Eclipse does not try to compile
1115*7f296bb3SBarry Smith   PETSc with the project.
1116*7f296bb3SBarry Smith
1117*7f296bb3SBarry SmithFor further examples of using Eclipse with a PETSc-based application,
1118*7f296bb3SBarry Smithsee the documentation for LaMEM [^lamem].
1119*7f296bb3SBarry Smith
1120*7f296bb3SBarry Smith## Qt Creator Users
1121*7f296bb3SBarry Smith
1122*7f296bb3SBarry SmithThis information was provided by Mohammad Mirzadeh. The Qt Creator IDE
1123*7f296bb3SBarry Smithis part of the Qt SDK, developed for cross-platform GUI programming
1124*7f296bb3SBarry Smithusing C++. It is available under GPL v3, LGPL v2 and a commercial
1125*7f296bb3SBarry Smithlicense and may be obtained, either as part of the Qt SDK or as
1126*7f296bb3SBarry Smithstand-alone software. It supports
1127*7f296bb3SBarry Smithautomatic makefile generation using cross-platform `qmake` and
1128*7f296bb3SBarry SmithCMake build systems as well as allowing one to import projects based
1129*7f296bb3SBarry Smithon existing, possibly hand-written, makefiles. Qt Creator has a visual
1130*7f296bb3SBarry Smithdebugger using GDB and LLDB (on Linux and OS X) or Microsoft’s CDB (on
1131*7f296bb3SBarry SmithMicrosoft Windows) as backends. It also has an interface to Valgrind’s “memcheck”
1132*7f296bb3SBarry Smithand “callgrind” tools to detect memory leaks and profile code. It has
1133*7f296bb3SBarry Smithbuilt-in support for a variety of version control systems including git,
1134*7f296bb3SBarry Smithmercurial, and subversion. Finally, Qt Creator comes fully equipped with
1135*7f296bb3SBarry Smithauto-completion, function look-up, and code refactoring tools. This
1136*7f296bb3SBarry Smithenables one to easily browse source files, find relevant functions, and
1137*7f296bb3SBarry Smithrefactor them across an entire project.
1138*7f296bb3SBarry Smith
1139*7f296bb3SBarry Smith### Creating a Project
1140*7f296bb3SBarry Smith
1141*7f296bb3SBarry SmithWhen using Qt Creator with `qmake`, one needs a `.pro` file. This
1142*7f296bb3SBarry Smithconfiguration file tells Qt Creator about all build/compile options and
1143*7f296bb3SBarry Smithlocations of source files. One may start with a blank `.pro` file and
1144*7f296bb3SBarry Smithfill in configuration options as needed. For example:
1145*7f296bb3SBarry Smith
1146*7f296bb3SBarry Smith```none
1147*7f296bb3SBarry Smith# The name of the application executable
1148*7f296bb3SBarry SmithTARGET = ex1
1149*7f296bb3SBarry Smith
1150*7f296bb3SBarry Smith# There are two ways to add PETSc functionality
1151*7f296bb3SBarry Smith# 1-Manual: Set all include path and libs required by PETSc
1152*7f296bb3SBarry SmithPETSC_INCLUDE = path/to/petsc_includes # e.g. obtained via running `make getincludedirs'
1153*7f296bb3SBarry SmithPETSC_LIBS = path/to/petsc_libs # e.g. obtained via running `make getlinklibs'
1154*7f296bb3SBarry Smith
1155*7f296bb3SBarry SmithINCLUDEPATH += $$PETSC_INCLUDES
1156*7f296bb3SBarry SmithLIBS += $$PETSC_LIBS
1157*7f296bb3SBarry Smith
1158*7f296bb3SBarry Smith# 2-Automatic: Use the PKGCONFIG functionality
1159*7f296bb3SBarry Smith# NOTE: petsc.pc must be in the pkgconfig path. You might need to adjust PKG_CONFIG_PATH
1160*7f296bb3SBarry SmithCONFIG += link_pkgconfig
1161*7f296bb3SBarry SmithPKGCONFIG += PETSc
1162*7f296bb3SBarry Smith
1163*7f296bb3SBarry Smith# Set appropriate compiler and its flags
1164*7f296bb3SBarry SmithQMAKE_CC = path/to/mpicc
1165*7f296bb3SBarry SmithQMAKE_CXX = path/to/mpicxx # if this is a cpp project
1166*7f296bb3SBarry SmithQMAKE_LINK = path/to/mpicxx # if this is a cpp project
1167*7f296bb3SBarry Smith
1168*7f296bb3SBarry SmithQMAKE_CFLAGS   += -O3 # add extra flags here
1169*7f296bb3SBarry SmithQMAKE_CXXFLAGS += -O3
1170*7f296bb3SBarry SmithQMAKE_LFLAGS   += -O3
1171*7f296bb3SBarry Smith
1172*7f296bb3SBarry Smith# Add all files that must be compiled
1173*7f296bb3SBarry SmithSOURCES += ex1.c source1.c source2.cpp
1174*7f296bb3SBarry Smith
1175*7f296bb3SBarry SmithHEADERS += source1.h source2.h
1176*7f296bb3SBarry Smith
1177*7f296bb3SBarry Smith# OTHER_FILES are ignored during compilation but will be shown in file panel in Qt Creator
1178*7f296bb3SBarry SmithOTHER_FILES += \
1179*7f296bb3SBarry Smith    path/to/resource_file \
1180*7f296bb3SBarry Smith    path/to/another_file
1181*7f296bb3SBarry Smith```
1182*7f296bb3SBarry Smith
1183*7f296bb3SBarry SmithIn this example, keywords include:
1184*7f296bb3SBarry Smith
1185*7f296bb3SBarry Smith- `TARGET`: The name of the application executable.
1186*7f296bb3SBarry Smith- `INCLUDEPATH`: Used at compile time to point to required include
1187*7f296bb3SBarry Smith  files. Essentially, it is used as an `-I \$\$INCLUDEPATH` flag for
1188*7f296bb3SBarry Smith  the compiler. This should include all application-specific header
1189*7f296bb3SBarry Smith  files and those related to PETSc (which may be found via
1190*7f296bb3SBarry Smith  `make getincludedirs`).
1191*7f296bb3SBarry Smith- `LIBS`: Defines all required external libraries to link with the
1192*7f296bb3SBarry Smith  application. To get PETSc’s linking libraries, use
1193*7f296bb3SBarry Smith  `make getlinklibs`.
1194*7f296bb3SBarry Smith- `CONFIG`: Configuration options to be used by `qmake`. Here, the
1195*7f296bb3SBarry Smith  option `link_pkgconfig` instructs `qmake` to internally use
1196*7f296bb3SBarry Smith  `pkgconfig` to resolve `INCLUDEPATH` and `LIBS` variables.
1197*7f296bb3SBarry Smith- `PKGCONFIG`: Name of the configuration file (the `.pc` file –
1198*7f296bb3SBarry Smith  here `petsc.pc`) to be passed to `pkgconfig`. Note that for this
1199*7f296bb3SBarry Smith  functionality to work, `petsc.pc` must be in path which might
1200*7f296bb3SBarry Smith  require adjusting the `PKG_CONFIG_PATH` environment variable. For
1201*7f296bb3SBarry Smith  more information see
1202*7f296bb3SBarry Smith  [the Qt Creator documentation](https://doc.qt.io/qtcreator/creator-build-settings.html).
1203*7f296bb3SBarry Smith- `QMAKE_CC` and `QMAKE_CXX`: Define which C/C++ compilers use.
1204*7f296bb3SBarry Smith- `QMAKE_LINK`: Defines the proper linker to be used. Relevant if
1205*7f296bb3SBarry Smith  compiling C++ projects.
1206*7f296bb3SBarry Smith- `QMAKE_CFLAGS`, `QMAKE_CXXFLAGS` and `QMAKE_LFLAGS`: Set the
1207*7f296bb3SBarry Smith  corresponding compile and linking flags.
1208*7f296bb3SBarry Smith- `SOURCES`: Source files to be compiled.
1209*7f296bb3SBarry Smith- `HEADERS`: Header files required by the application.
1210*7f296bb3SBarry Smith- `OTHER_FILES`: Other files to include (source, header, or any other
1211*7f296bb3SBarry Smith  extension). Note that none of the source files placed here are
1212*7f296bb3SBarry Smith  compiled.
1213*7f296bb3SBarry Smith
1214*7f296bb3SBarry SmithMore options can be included in a `.pro` file; see
1215*7f296bb3SBarry Smith<https://doc.qt.io/qt-5/qmake-project-files.html>. Once the `.pro` file
1216*7f296bb3SBarry Smithis generated, the user can simply open it via Qt Creator. Upon opening,
1217*7f296bb3SBarry Smithone has the option to create two different build options, debug and
1218*7f296bb3SBarry Smithrelease, and switch between the two. For more information on using the
1219*7f296bb3SBarry SmithQt Creator interface and other more advanced aspects of the IDE, refer
1220*7f296bb3SBarry Smithto <https://www.qt.io/qt-features-libraries-apis-tools-and-ide/>
1221*7f296bb3SBarry Smith
1222*7f296bb3SBarry Smith(sec_visual_studio)=
1223*7f296bb3SBarry Smith
1224*7f296bb3SBarry Smith## Visual Studio Users
1225*7f296bb3SBarry Smith
1226*7f296bb3SBarry SmithTo use PETSc from Microsoft Visual Studio, one would have to compile a PETSc
1227*7f296bb3SBarry Smithexample with its corresponding makefile and then transcribe all compiler
1228*7f296bb3SBarry Smithand linker options used in this build into a Visual Studio project file,
1229*7f296bb3SBarry Smithin the appropriate format in Visual Studio project settings.
1230*7f296bb3SBarry Smith
1231*7f296bb3SBarry Smith## Xcode IDE Users
1232*7f296bb3SBarry Smith
1233*7f296bb3SBarry SmithSee {any}`doc_macos_install` for the standard Unix command line tools approach to development on macOS. The information
1234*7f296bb3SBarry Smithbelow is only if you plan to write code within the Xcode IDE.
1235*7f296bb3SBarry Smith
1236*7f296bb3SBarry Smith### Apple Xcode IDE for macOS Applications
1237*7f296bb3SBarry Smith
1238*7f296bb3SBarry SmithFollow the instructions in `$PETSC_DIR/systems/Apple/OSX/bin/makeall`
1239*7f296bb3SBarry Smithto build the PETSc framework and documentation suitable for use in
1240*7f296bb3SBarry SmithXcode.
1241*7f296bb3SBarry Smith
1242*7f296bb3SBarry SmithYou can then use the PETSc framework in
1243*7f296bb3SBarry Smith`$PETSC_DIR/arch-osx/PETSc.framework` in the usual manner for Apple
1244*7f296bb3SBarry Smithframeworks. See the examples in
1245*7f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/OSX/examples`. When working in Xcode, things
1246*7f296bb3SBarry Smithlike function name completion should work for all PETSc functions as
1247*7f296bb3SBarry Smithwell as MPI functions. You must also link against the Apple
1248*7f296bb3SBarry Smith`Accelerate.framework`.
1249*7f296bb3SBarry Smith
1250*7f296bb3SBarry Smith### Apple Xcode IDE for iPhone/iPad iOS Applications
1251*7f296bb3SBarry Smith
1252*7f296bb3SBarry SmithFollow the instructions in
1253*7f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/iOS/bin/iosbuilder.py` to build the PETSc
1254*7f296bb3SBarry Smithlibrary for use on the iPhone/iPad.
1255*7f296bb3SBarry Smith
1256*7f296bb3SBarry SmithYou can then use the PETSc static library in
1257*7f296bb3SBarry Smith`$PETSC_DIR/arch-osx/libPETSc.a` in the usual manner for Apple
1258*7f296bb3SBarry Smithlibraries inside your iOS XCode projects; see the examples in
1259*7f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/iOS/examples`. You must also link against
1260*7f296bb3SBarry Smiththe Apple `Accelerate.framework`.
1261*7f296bb3SBarry Smith
1262*7f296bb3SBarry SmithA thorough discussion of the
1263*7f296bb3SBarry Smithprocedure is given in [Comparison of Migration Techniques for High-Performance Code to Android and iOS](https://www.researchgate.net/publication/308973080_Comparison_of_Migration_Techniques_for_High-Performance_Code_to_Android_and_iOS).
1264*7f296bb3SBarry Smith
1265*7f296bb3SBarry SmithFor Android, you must have your standalone bin folder in the path, so that the compilers
1266*7f296bb3SBarry Smithare visible.
1267*7f296bb3SBarry Smith
1268*7f296bb3SBarry SmithThe installation process has not been tested for iOS or Android since 2017.
1269*7f296bb3SBarry Smith
1270*7f296bb3SBarry Smith```{rubric} Footnotes
1271*7f296bb3SBarry Smith```
1272*7f296bb3SBarry Smith
1273*7f296bb3SBarry Smith[^saws]: [Saws wiki on Bitbucket](https://bitbucket.org/saws/saws/wiki/Home)
1274*7f296bb3SBarry Smith
1275*7f296bb3SBarry Smith[^cxx-note]: Note that this option is not required to use PETSc with C++
1276*7f296bb3SBarry Smith
1277*7f296bb3SBarry Smith[^lamem]: See the `doc/` directory at <https://bitbucket.org/bkaus/lamem>
1278*7f296bb3SBarry Smith
1279*7f296bb3SBarry Smith[^json]: JSON is a subset of YAML
1280