xref: /petsc/doc/manual/getting_started.md (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
1(sec_getting_started)=
2
3# Getting Started
4
5PETSc consists of a collection of classes,
6which are discussed in detail in later parts of the manual ({doc}`programming` and {doc}`additional`).
7The important PETSc classes include
8
9- index sets (`IS`), for indexing into
10  vectors, renumbering, permuting, etc;
11- {any}`ch_vectors` (`Vec`);
12- (generally sparse) {any}`ch_matrices` (`Mat`)
13- {any}`ch_ksp` (`KSP`);
14- preconditioners, including multigrid, block solvers, patch solvers, and
15  sparse direct solvers (`PC`);
16- {any}`ch_snes` (`SNES`);
17- {any}`ch_ts` for solving time-dependent (nonlinear) PDEs, including
18  support for differential-algebraic-equations, and the computation of
19  adjoints (sensitivities/gradients of the solutions) (`TS`);
20- scalable {any}`ch_tao` including a rich set of gradient-based optimizers,
21  Newton-based optimizers and optimization with constraints (`Tao`).
22- {any}ch_regressor (`PetscRegressor)`
23- {any}`ch_dmbase` code for managing interactions between mesh data structures and vectors,
24  matrices, and solvers (`DM`);
25
26Each class consists of an abstract interface (simply a set of calling
27sequences corresponding to an abstract base class in C++) and an implementation for each algorithm and data structure.
28This design enables easy comparison and use of different
29algorithms (for example, experimenting with different Krylov subspace
30methods, preconditioners, or truncated Newton methods). Hence, PETSc
31provides a rich environment for modeling scientific applications as well
32as for rapid algorithm design and prototyping.
33
34The classes enable easy customization and extension of both algorithms
35and implementations. This approach promotes code reuse and flexibility.
36The PETSc infrastructure creates a foundation for building large-scale
37applications.
38
39It is useful to consider the interrelationships among different pieces
40of PETSc. {any}`fig_library` is a diagram of some
41of these pieces. The figure illustrates the library’s hierarchical
42organization, enabling users to employ the most appropriate solvers for a particular problem.
43
44:::{figure} /images/manual/library_structure.svg
45:alt: PETSc numerical libraries
46:name: fig_library
47
48Numerical Libraries in PETSc
49:::
50
51## Suggested Reading
52
53The manual is divided into four parts:
54
55- {doc}`introduction`
56- {doc}`programming`
57- {doc}`dm`
58- {doc}`additional`
59
60{doc}`introduction` describes the basic procedure for using the PETSc library and
61presents simple examples of solving linear systems with PETSc. This
62section conveys the typical style used throughout the library and
63enables the application programmer to begin using the software
64immediately.
65
66{doc}`programming` explains in detail the use of the various PETSc algebraic objects, such
67as vectors, matrices, index sets, and PETSc solvers, including linear and nonlinear solvers, time integrators,
68and optimization support.
69
70{doc}`dm` details how a user's models and discretizations can easily be interfaced with the
71solvers by using the `DM` construct.
72
73{doc}`additional` describes a variety of useful information, including
74profiling, the options database, viewers, error handling, and some
75details of PETSc design.
76
77[Visual Studio Code](https://code.visualstudio.com/), Eclipse, Emacs, and Vim users may find their development environment's options for
78searching in the source code are
79useful for exploring the PETSc source code. Details of this feature are provided in {any}`sec_developer_environments`.
80
81**Note to Fortran Programmers**: In most of the manual, the examples and calling sequences are given
82for the C/C++ family of programming languages. However, Fortran
83programmers can use all of the functionality of PETSc from Fortran,
84with only minor differences in the user interface.
85{any}`ch_fortran` provides a discussion of the differences between
86using PETSc from Fortran and C, as well as several complete Fortran
87examples.
88
89**Note to Python Programmers**: To program with PETSc in Python, you need to enable Python bindings
90(i.e. petsc4py) with the configure option `--with-petsc4py=1`. See the
91{doc}`PETSc installation guide </install/index>`
92for more details.
93
94(sec_running)=
95
96## Running PETSc Programs
97
98Before using PETSc, the user must first set the environmental variable
99`PETSC_DIR` to indicate the full path of the PETSc home directory. For
100example, under the Unix bash shell, a command of the form
101
102```console
103$ export PETSC_DIR=$HOME/petsc
104```
105
106can be placed in the user’s `.bashrc` or other startup file. In
107addition, the user may need to set the environment variable
108`$PETSC_ARCH` to specify a particular configuration of the PETSc
109libraries. Note that `$PETSC_ARCH` is just a name selected by the
110installer to refer to the libraries compiled for a particular set of
111compiler options and machine type. Using different values of
112`$PETSC_ARCH` allows one to switch between several different sets (say
113debug and optimized versions) of libraries easily. To determine if you need to
114set `$PETSC_ARCH`, look in the directory indicated by `$PETSC_DIR`, if
115there are subdirectories beginning with `arch` then those
116subdirectories give the possible values for `$PETSC_ARCH`.
117
118See {any}`handson` to immediately jump in and run PETSc code.
119
120All PETSc programs use the MPI (Message Passing Interface) standard for
121message-passing communication {cite}`mpi-final`. Thus, to
122execute PETSc programs, users must know the procedure for beginning MPI
123jobs on their selected computer system(s). For instance, when using the
124[MPICH](https://www.mpich.org/) implementation of MPI and many
125others, the following command initiates a program that uses eight
126processors:
127
128```console
129$ mpiexec -n 8 ./petsc_program_name petsc_options
130```
131
132PETSc also provides a script that automatically uses the correct
133`mpiexec` for your configuration.
134
135```console
136$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 8 ./petsc_program_name petsc_options
137```
138
139Certain options are supported by all PETSc programs. We list a few
140particularly useful ones below; a complete list can be obtained by
141running any PETSc program with the option `-help`.
142
143- `-log_view` - summarize the program’s performance (see {any}`ch_profiling`)
144- `-fp_trap` - stop on floating-point exceptions; for example divide
145  by zero
146- `-malloc_dump` - enable memory tracing; dump list of unfreed memory
147  at conclusion of the run, see
148  {any}`detecting_memory_problems`,
149- `-malloc_debug` - enable memory debugging (by default, this is
150  activated for the debugging version of PETSc), see
151  {any}`detecting_memory_problems`,
152- `-start_in_debugger` `[noxterm,gdb,lldb]`
153  `[-display name]` - start all (or a subset of the) processes in a debugger. See
154  {any}`sec_debugging`, for more information on
155  debugging PETSc programs.
156- `-on_error_attach_debugger` `[noxterm,gdb,lldb]`
157  `[-display name]` - start debugger only on encountering an error
158- `-info` - print a great deal of information about what the program
159  is doing as it runs
160- `-version` - display the version of PETSc being used
161
162(sec_writing)=
163
164## Writing PETSc Programs
165
166Most PETSc programs begin with a call to
167
168```
169PetscInitialize(int *argc,char ***argv,char *file,char *help);
170```
171
172which initializes PETSc and MPI. The arguments `argc` and `argv` are
173the usual command line arguments in C and C++ programs. The
174argument `file` optionally indicates an alternative name for the PETSc
175options file, `.petscrc`, which resides by default in the user’s home
176directory. {any}`sec_options` provides details
177regarding this file and the PETSc options database, which can be used
178for runtime customization. The final argument, `help`, is an optional
179character string that will be printed if the program is run with the
180`-help` option. In Fortran, the initialization command has the form
181
182```fortran
183call PetscInitialize(character(*) file,integer ierr)
184```
185
186where the file argument is optional.
187
188`PetscInitialize()` automatically calls `MPI_Init()` if MPI has not
189been not previously initialized. In certain circumstances in which MPI
190needs to be initialized directly (or is initialized by some other
191library), the user can first call `MPI_Init()` (or have the other
192library do it), and then call `PetscInitialize()`. By default,
193`PetscInitialize()` sets the PETSc “world” communicator
194`PETSC_COMM_WORLD` to `MPI_COMM_WORLD`.
195
196For those unfamiliar with MPI, a *communicator* indicates
197a collection of processes that will be involved in a
198calculation or communication. Communicators have the variable type
199`MPI_Comm`. In most cases, users can employ the communicator
200`PETSC_COMM_WORLD` to indicate all processes in a given run and
201`PETSC_COMM_SELF` to indicate a single process.
202
203MPI provides routines for generating new communicators consisting of
204subsets of processors, though most users rarely need to use these. The
205book *Using MPI*, by Lusk, Gropp, and Skjellum
206{cite}`using-mpi` provides an excellent introduction to the
207concepts in MPI. See also the [MPI homepage](https://www.mcs.anl.gov/research/projects/mpi/).
208Note that PETSc users
209need not program much message passing directly with MPI, but they must
210be familiar with the basic concepts of message passing and distributed
211memory computing.
212
213All PETSc programs should call `PetscFinalize()` as their final (or
214nearly final) statement. This routine handles options to be called at the conclusion of the
215program and calls `MPI_Finalize()` if `PetscInitialize()` began
216MPI. If MPI was initiated externally from PETSc (by either the user or
217another software package), the user is responsible for calling
218`MPI_Finalize()`.
219
220### Error Checking
221
222Most PETSc functions return a `PetscErrorCode`, an integer
223indicating whether an error occurred during the call. The error code
224is set to be nonzero if an error has been detected; otherwise, it is
225zero. For the C/C++ interface, the error variable is the routine’s
226return value, while for the Fortran version, each PETSc routine has an integer error variable as
227its final argument.
228
229One should always check these routine values as given below in the C/C++
230formats, respectively:
231
232```c
233PetscCall(PetscFunction(Args));
234```
235
236or for Fortran
237
238```fortran
239! within the main program
240PetscCallA(PetscFunction(Args,ierr))
241```
242
243```fortran
244! within any subroutine
245PetscCall(PetscFunction(Args,ierr))
246```
247
248These macros check the returned error code, and if it is nonzero, they call the PETSc error
249handler and then return from the function with the error code. The macros above should be used on all PETSc calls to enable
250a complete error traceback. See {any}`sec_error2` for more details on PETSc error handling.
251
252(sec_simple)=
253
254## Simple PETSc Examples
255
256To help the user use PETSc immediately, we begin with a simple
257uniprocessor example that
258solves the one-dimensional Laplacian problem with finite differences.
259This sequential code illustrates the solution of
260a linear system with `KSP`, the interface to the preconditioners,
261Krylov subspace methods and direct linear solvers of PETSc. Following
262the code, we highlight a few of the most important parts of this example.
263
264:::{admonition} Listing: <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex1.c.html">KSP Tutorial src/ksp/ksp/tutorials/ex1.c</a>
265:name: ksp-ex1
266
267```{literalinclude} /../src/ksp/ksp/tutorials/ex1.c
268:end-before: /*TEST
269```
270:::
271
272### Include Files
273
274The C/C++ include files for PETSc should be used via statements such as
275
276```
277#include <petscksp.h>
278```
279
280where <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscksp.h.html">petscksp.h</a>
281is the include file for the linear solver library.
282Each PETSc program must specify an include file corresponding to the
283highest level PETSc objects needed within the program; all of the
284required lower level include files are automatically included within the
285higher level files. For example, <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscksp.h.html">petscksp.h</a> includes
286<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscmat.h.html">petscmat.h</a>
287(matrices),
288<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscvec.h.html">petscvec.h</a>
289(vectors), and
290<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscsys.h.html">petscsys.h</a>
291(base PETSc
292file). The PETSc include files are located in the directory
293<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/index.html">\$PETSC_DIR/include</a>.
294See {any}`sec_fortran_includes`
295for a discussion of PETSc include files in Fortran programs.
296
297(the_options_database)=
298
299### The Options Database
300
301As shown in {any}`sec_simple`, the user can
302input control data at run time using the options database. In this
303example the command `PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);`
304checks whether the user has provided a command line option to set the
305value of `n`, the problem dimension. If so, the variable `n` is set
306accordingly; otherwise, `n` remains unchanged. A complete description
307of the options database may be found in {any}`sec_options`.
308
309(sec_vecintro)=
310
311### Vectors
312
313One creates a new parallel or sequential vector, `x`, of global
314dimension `M` with the commands
315
316```
317VecCreate(MPI_Comm comm,Vec *x);
318VecSetSizes(Vec x, PetscInt m, PetscInt M);
319```
320
321where `comm` denotes the MPI communicator and `m` is the optional
322local size which may be `PETSC_DECIDE`. The type of storage for the
323vector may be set with either calls to `VecSetType()` or
324`VecSetFromOptions()`. Additional vectors of the same type can be
325formed with
326
327```
328VecDuplicate(Vec old,Vec *new);
329```
330
331The commands
332
333```
334VecSet(Vec x,PetscScalar value);
335VecSetValues(Vec x,PetscInt n,PetscInt *indices,PetscScalar *values,INSERT_VALUES);
336```
337
338respectively set all the components of a vector to a particular scalar
339value and assign a different value to each component. More detailed
340information about PETSc vectors, including their basic operations,
341scattering/gathering, index sets, and distributed arrays is available
342in Chapter {any}`ch_vectors`.
343
344Note the use of the PETSc variable type `PetscScalar` in this example.
345`PetscScalar` is defined to be `double` in C/C++ (or
346correspondingly `double precision` in Fortran) for versions of PETSc
347that have *not* been compiled for use with complex numbers. The
348`PetscScalar` data type enables identical code to be used when the
349PETSc libraries have been compiled for use with complex numbers.
350{any}`sec_complex` discusses the use of complex
351numbers in PETSc programs.
352
353(sec_matintro)=
354
355### Matrices
356
357The usage of PETSc matrices and vectors is similar. The user can create a
358new parallel or sequential matrix, `A`, which has `M` global rows
359and `N` global columns, with the routines
360
361```
362MatCreate(MPI_Comm comm,Mat *A);
363MatSetSizes(Mat A,PETSC_DECIDE,PETSC_DECIDE,PetscInt M,PetscInt N);
364```
365
366where the matrix format can be specified at runtime via the options
367database. The user could alternatively specify each processes’ number of
368local rows and columns using `m` and `n`.
369
370```
371MatSetSizes(Mat A,PetscInt m,PetscInt n,PETSC_DETERMINE,PETSC_DETERMINE);
372```
373
374Generally, one then sets the “type” of the matrix, with, for example,
375
376```
377MatSetType(A,MATAIJ);
378```
379
380This causes the matrix `A` to use the compressed sparse row storage
381format to store the matrix entries. See `MatType` for a list of all
382matrix types. Values can then be set with the command
383
384```
385MatSetValues(Mat A,PetscInt m,PetscInt *im,PetscInt n,PetscInt *in,PetscScalar *values,INSERT_VALUES);
386```
387
388After all elements have been inserted into the matrix, it must be
389processed with the pair of commands
390
391```
392MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
393MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
394```
395
396{any}`ch_matrices` discusses various matrix formats as
397well as the details of some basic matrix manipulation routines.
398
399### Linear Solvers
400
401After creating the matrix and vectors that define a linear system,
402`Ax` $=$ `b`, the user can then use `KSP` to solve the
403system with the following sequence of commands:
404
405```
406KSPCreate(MPI_Comm comm,KSP *ksp);
407KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat);
408KSPSetFromOptions(KSP ksp);
409KSPSolve(KSP ksp,Vec b,Vec x);
410KSPDestroy(KSP ksp);
411```
412
413The user first creates the `KSP` context and sets the operators
414associated with the system (matrix that defines the linear system,
415`Amat` and matrix from which the preconditioner is constructed,
416`Pmat` ). The user then sets various options for customized solutions,
417solves the linear system, and finally destroys the `KSP` context. The command `KSPSetFromOptions()` enables the user to
418customize the linear solution method at runtime using the options
419database, which is discussed in {any}`sec_options`. Through this database, the
420user not only can select an iterative method and preconditioner, but
421can also prescribe the convergence tolerance, set various monitoring
422routines, etc. (see, e.g., {any}`sec_profiling_programs`).
423
424{any}`ch_ksp` describes in detail the `KSP` package,
425including the `PC` and `KSP` packages for preconditioners and Krylov
426subspace methods.
427
428### Nonlinear Solvers
429
430PETSc provides
431an interface to tackle nonlinear problems called `SNES`.
432{any}`ch_snes` describes the nonlinear
433solvers in detail. We highly recommend most PETSc users work directly with
434`SNES`, rather than using PETSc for the linear problem and writing their own
435nonlinear solver. Similarly, users should use `TS` rather than rolling their own time integrators.
436
437(sec_error2)=
438
439### Error Checking
440
441As noted above, PETSc functions return a `PetscErrorCode`, which is an integer
442indicating whether an error has occurred during the call. Below, we indicate a traceback
443generated by error detection within a sample PETSc program. The error
444occurred on line 3618 of the file
445`$PETSC_DIR/src/mat/impls/aij/seq/aij.c` and was caused by trying to
446allocate too large an array in memory. The routine was called in the
447program `ex3.c` on line 66. See
448{any}`sec_fortran_errors` for details regarding error checking
449when using the PETSc Fortran interface.
450
451```none
452$ cd $PETSC_DIR/src/ksp/ksp/tutorials
453$ make ex3
454$ mpiexec -n 1 ./ex3 -m 100000
455[0]PETSC ERROR: --------------------- Error Message --------------------------------
456[0]PETSC ERROR: Out of memory. This could be due to allocating
457[0]PETSC ERROR: too large an object or bleeding by not properly
458[0]PETSC ERROR: destroying unneeded objects.
459[0]PETSC ERROR: Memory allocated 11282182704 Memory used by process 7075897344
460[0]PETSC ERROR: Try running with -malloc_dump or -malloc_view for info.
461[0]PETSC ERROR: Memory requested 18446744072169447424
462[0]PETSC ERROR: PETSc Development Git Revision: v3.7.1-224-g9c9a9c5 Git Date: 2016-05-18 22:43:00 -0500
463[0]PETSC ERROR: ./ex3 on a arch-darwin-double-debug named Patricks-MacBook-Pro-2.local by patrick Mon Jun 27 18:04:03 2016
464[0]PETSC ERROR: Configure options PETSC_DIR=/Users/patrick/petsc PETSC_ARCH=arch-darwin-double-debug --download-mpich --download-f2cblaslapack --with-cc=clang --with-cxx=clang++ --with-fc=gfortran --with-debugging=1 --with-precision=double --with-scalar-type=real --with-viennacl=0 --download-c2html -download-sowing
465[0]PETSC ERROR: #1 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
466[0]PETSC ERROR: #2 PetscTrMallocDefault() line 188 in /Users/patrick/petsc/src/sys/memory/mtr.c
467[0]PETSC ERROR: #3 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
468[0]PETSC ERROR: #4 MatSeqAIJSetPreallocation() line 3562 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c
469[0]PETSC ERROR: #5 main() line 66 in /Users/patrick/petsc/src/ksp/ksp/tutorials/ex3.c
470[0]PETSC ERROR: PETSc Option Table entries:
471[0]PETSC ERROR: -m 100000
472[0]PETSC ERROR: ----------------End of Error Message ------- send entire error message to petsc-maint@mcs.anl.gov----------
473```
474
475When running the debug version [^debug-footnote] of the PETSc libraries, it checks for memory corruption (writing outside of array bounds
476, etc.). The macro `CHKMEMQ` can be called anywhere in the code to check
477the current status of the memory for corruption. By putting several (or
478many) of these macros into your code, you can usually easily track down
479in what small segment of your code the corruption has occurred. One can
480also use Valgrind to track down memory errors; see the [FAQ](https://petsc.org/release/faq/).
481
482For complete error handling, calls to MPI functions should be made with `PetscCallMPI(MPI_Function(Args))`.
483In Fortran subroutines use `PetscCallMPI(MPI_Function(Args, ierr))` and in Fortran main use
484`PetscCallMPIA(MPI_Function(Args, ierr))`.
485
486PETSc has a small number of C/C++-only macros that do not explicitly return error codes. These are used in the style
487
488```c
489XXXBegin(Args);
490other code
491XXXEnd();
492```
493
494and include `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscObjectOptionsBegin()`,
495`PetscOptionsHeadBegin()`, `PetscOptionsHeadEnd()`, `PetscDrawCollectiveBegin()`, `PetscDrawCollectiveEnd()`,
496`MatPreallocateEnd()`, and `MatPreallocateBegin()`. These should not be checked for error codes.
497Another class of functions with the `Begin()` and `End()` paradigm
498including `MatAssemblyBegin()`, and `MatAssemblyEnd()` do return error codes that should be checked.
499
500PETSc also has a set of C/C++-only macros that return an object, or `NULL` if an error has been detected. These include
501`PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_STDOUT_(MPI_Comm)`, and `PETSC_VIEWER_DRAW_(MPI_Comm)`.
502
503Finally `PetscObjectComm((PetscObject)x)` returns the communicator associated with the object `x` or `MPI_COMM_NULL` if an
504error was detected.
505
506(sec_parallel)=
507
508# Parallel and GPU Programming
509
510Numerical computing today has multiple levels of parallelism (concurrency).
511
512- Low-level, single instruction multiple data (SIMD) parallelism or, somewhat similar, on-GPU parallelism,
513- medium-level, multiple instruction multiple data shared memory parallelism (thread parallelism), and
514- high-level, distributed memory parallelism.
515
516Traditional CPUs support the lower two levels via, for example, Intel AVX-like instructions ({any}`sec_cpu_simd`) and Unix threads, often managed by using OpenMP pragmas ({any}`sec_cpu_openmp`),
517(or multiple processes). GPUs also support the lower two levels via kernel functions ({any}`sec_gpu_kernels`) and streams ({any}`sec_gpu_streams`).
518Distributed memory parallelism is created by combining multiple
519CPUs and/or GPUs and using MPI for communication ({any}`sec_mpi`).
520
521In addition, there is also concurrency between computations (floating point operations) and data movement (from memory to caches and registers
522and via MPI between distinct memory nodes).
523
524PETSc supports all these parallelism levels, but its strongest support is for MPI-based distributed memory parallelism.
525
526(sec_mpi)=
527
528## MPI Parallelism
529
530Since PETSc uses the message-passing model for parallel programming and
531employs MPI for all interprocessor communication, the user can
532employ MPI routines as needed throughout an application code. However,
533by default, the user is shielded from many of the details of message
534passing within PETSc since these are hidden within parallel objects,
535such as vectors, matrices, and solvers. In addition, PETSc provides
536tools such as vector scatter and gather to assist in the
537management of parallel data.
538
539Recall that the user must specify a communicator upon creation of any
540PETSc object (such as a vector, matrix, or solver) to indicate the
541processors over which the object is to be distributed. For example, as
542mentioned above, some commands for matrix, vector, and linear solver
543creation are:
544
545```
546MatCreate(MPI_Comm comm,Mat *A);
547VecCreate(MPI_Comm comm,Vec *x);
548KSPCreate(MPI_Comm comm,KSP *ksp);
549```
550
551The creation routines are collective on all processes in the
552communicator; thus, all processors in the communicator *must* call the
553creation routine. In addition, if a sequence of collective routines is
554being used, they *must* be called in the same order on each process.
555
556The next example, given below,
557illustrates the solution of a linear system in parallel. This code,
558corresponding to
559<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex2.c.html">KSP Tutorial ex2</a>,
560handles the two-dimensional Laplacian discretized with finite
561differences, where the linear system is again solved with KSP. The code
562performs the same tasks as the sequential version within
563{any}`sec_simple`. Note that the user interface
564for initiating the program, creating vectors and matrices, and solving
565the linear system is *exactly* the same for the uniprocessor and
566multiprocessor examples. The primary difference between the examples in
567{any}`sec_simple` and
568here is each processor forms only its
569local part of the matrix and vectors in the parallel case.
570
571:::{admonition} Listing: <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex2.c.html">KSP Tutorial src/ksp/ksp/tutorials/ex2.c</a>
572:name: ksp-ex2
573
574```{literalinclude} /../src/ksp/ksp/tutorials/ex2.c
575:end-before: /*TEST
576```
577:::
578
579(sec_cpu_simd)=
580
581## CPU SIMD parallelism
582
583SIMD parallelism occurs most commonly in the Intel advanced vector extensions (AVX) families of instructions (see [Wikipedia](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)).
584It may be automatically used by the optimizing compiler or in low-level libraries that PETSc uses, such as BLAS
585(see [BLIS](https://github.com/flame/blis)), or rarely,
586directly in PETSc C/C++ code, as in [MatMult_SeqSELL](https://petsc.org/main/src/mat/impls/sell/seq/sell.c.html#MatMult_SeqSELL).
587
588(sec_cpu_openmp)=
589
590## CPU OpenMP parallelism
591
592OpenMP parallelism is thread parallelism. Multiple threads (independent streams of instructions) process data and perform computations on different
593parts of memory that is
594shared (accessible) to all of the threads. The OpenMP model is based on inserting pragmas into code, indicating that a series of instructions
595(often within a loop) can be run in parallel. This is also called a fork-join model of parallelism since much of the code remains sequential and only the
596computationally expensive parts in the 'parallel region' are parallel. Thus, OpenMP makes it relatively easy to add some
597parallelism to a conventional sequential code in a shared memory environment.
598
599POSIX threads (pthreads) is a library that may be called from C/C++. The library contains routines to create, join, and remove threads, plus manage communications and
600synchronizations between threads. Pthreads is rarely used directly in numerical libraries and applications. Sometimes OpenMP is implemented on top of pthreads.
601
602If one adds
603OpenMP parallelism to an MPI code, one must not over-subscribe the hardware resources. For example, if MPI already has one MPI process (rank)
604per hardware core, then
605using four OpenMP threads per MPI process will slow the code down since now one core must switch back and forth between four OpenMP threads.
606
607For application codes that use certain external packages, including BLAS/LAPACK, SuperLU_DIST, MUMPS, MKL, and SuiteSparse, one can build PETSc and these
608packages to take advantage of OpenMP by using the configure option `--with-openmp`. The number of OpenMP threads used in the application can be controlled with
609the PETSc command line option `-omp_num_threads <num>` or the environmental variable `OMP_NUM_THREADS`. Running a PETSc program with `-omp_view` will display the
610number of threads used. The default number is often absurdly high for the given hardware, so we recommend always setting it appropriately.
611
612Users can also put OpenMP pragmas into their own code. However, since standard PETSc is not thread-safe, they should not, in general,
613call PETSc routines from inside the parallel regions.
614
615There is an OpenMP thread-safe subset of PETSc that may be configured for using `--with-threadsafety` (often used along with `--with-openmp` or
616`--download-concurrencykit`). <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex61f.F90.html">KSP Tutorial ex61f</a> demonstrates
617how this may be used with OpenMP. In this mode, one may have individual OpenMP threads that each manage their own
618(sequential) PETSc objects (each thread can interact only with its own objects). This
619is useful when one has many small systems (or sets of ODEs) that must be integrated in an
620"embarrassingly parallel" fashion on multicore systems.
621
622The ./configure option `--with-openmp-kernels` causes some PETSc numerical kernels to be compiled using OpenMP pragmas to take advantage of multiple cores.
623One must be careful to ensure the number of threads used by each MPI process **times** the number of MPI processes is less than the number of
624cores on the system; otherwise the code will slow down dramatically.
625
626PETSc's MPI-based linear solvers may be accessed from a sequential or non-MPI OpenMP program, see {any}`sec_pcmpi`.
627
628:::{seealso}
629Edward A. Lee, [The Problem with Threads](https://digitalassets.lib.berkeley.edu/techreports/ucb/text/EECS-2006-1.pdf), Technical Report No. UCB/EECS-2006-1 January [[DOI]](https://doi.org/10.1109/MC.2006.180)
63010, 2006
631:::
632
633(sec_gpu_kernels)=
634
635## GPU kernel parallelism
636
637GPUs offer at least two levels of clearly defined parallelism. Kernel-level parallelism is much like SIMD parallelism applied to loops;
638many "iterations" of the loop index run on different hardware in "lock-step".
639PETSc utilizes this parallelism with three similar but slightly different models:
640
641- CUDA, which is provided by NVIDIA and runs on NVIDIA GPUs
642- HIP, provided by AMD, which can, in theory, run on both AMD and NVIDIA GPUs
643- and Kokkos, an open-source package that provides a slightly higher-level programming model to utilize GPU kernels.
644
645To utilize this one configures PETSc with either `--with-cuda` or `--with-hip` and, if they plan to use Kokkos, also `--download-kokkos --download-kokkos-kernels`.
646
647In the GPU programming model that PETSc uses, the GPU memory is distinct from the CPU memory. This means that data that resides on the CPU
648memory must be copied to the GPU (often, this copy is done automatically by the libraries, and the user does not need to manage it)
649if one wishes to use the GPU computational power on it. This memory copy is slow compared to the GPU speed; hence, it is crucial to minimize these copies. This often
650translates to trying to do almost all the computation on the GPU and not constantly switching between computations on the CPU and the GPU on the same data.
651
652PETSc utilizes GPUs by providing vector and matrix classes (Vec and Mat) specifically written to run on the GPU. However, since it is difficult to
653write an entire PETSc code that runs only on the GPU, one can also access and work with (for example, put entries into) the vectors and matrices
654on the CPU. The vector classes
655are `VECCUDA`, `MATAIJCUSPARSE`, `VECKOKKOS`, `MATAIJKOKKOS`, and `VECHIP` (matrices are not yet supported by PETSc with HIP).
656
657More details on using GPUs from PETSc will follow in this document.
658
659(sec_gpu_streams)=
660
661## GPU stream parallelism
662
663Please contribute to this document.
664
665```{raw} latex
666\newpage
667```
668
669# Compiling and Running Programs
670
671The output below illustrates compiling and running a
672PETSc program using MPICH on a macOS laptop. Note that different
673machines will have compilation commands as determined by the
674configuration process. See {any}`sec_writing_application_codes` for
675a discussion about how to compile your PETSc programs. Users who are
676experiencing difficulties linking PETSc programs should refer to the [FAQ](https://petsc.org/release/faq/).
677
678```none
679$ cd $PETSC_DIR/src/ksp/ksp/tutorials
680$ make ex2
681/Users/patrick/petsc/arch-debug/bin/mpicc -o ex2.o -c -g3   -I/Users/patrick/petsc/include -I/Users/patrick/petsc/arch-debug/include `pwd`/ex2.c
682/Users/patrick/petsc/arch-debug/bin/mpicc -g3  -o ex2 ex2.o  -Wl,-rpath,/Users/patrick/petsc/arch-debug/lib -L/Users/patrick/petsc/arch-debug/lib  -lpetsc -lf2clapack -lf2cblas -lmpifort -lgfortran -lgcc_ext.10.5 -lquadmath -lm -lclang_rt.osx -lmpicxx -lc++ -ldl -lmpi -lpmpi -lSystem
683/bin/rm -f ex2.o
684$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex2
685Norm of error 0.000156044 iterations 6
686$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 2 ./ex2
687Norm of error 0.000411674 iterations 7
688```
689
690(sec_profiling_programs)=
691
692# Profiling Programs
693
694The option
695`-log_view` activates printing of a performance summary, including
696times, floating point operation (flop) rates, and message-passing
697activity. {any}`ch_profiling` provides details about
698profiling, including the interpretation of the output data below.
699This particular example involves
700the solution of a linear system on one processor using GMRES and ILU.
701The low floating point operation (flop) rates in this example are because the code solved a tiny system. We include this example
702merely to demonstrate the ease of extracting performance information.
703
704(listing_exprof)=
705
706```none
707$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex1 -n 1000 -pc_type ilu -ksp_type gmres -ksp_rtol 1.e-7 -log_view
708...
709------------------------------------------------------------------------------------------------------------------------
710Event                Count      Time (sec)     Flops                             --- Global ---  --- Stage ----  Total
711                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %T %F %M %L %R  %T %F %M %L %R Mflop/s
712------------------------------------------------------------------------------------------------------------------------
713
714VecMDot                1 1.0 3.2830e-06 1.0 2.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0  5  0  0  0   0  5  0  0  0   609
715VecNorm                3 1.0 4.4550e-06 1.0 6.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 14  0  0  0   0 14  0  0  0  1346
716VecScale               2 1.0 4.0110e-06 1.0 2.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0  5  0  0  0   0  5  0  0  0   499
717VecCopy                1 1.0 3.2280e-06 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
718VecSet                11 1.0 2.5537e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
719VecAXPY                2 1.0 2.0930e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 10  0  0  0   0 10  0  0  0  1911
720VecMAXPY               2 1.0 1.1280e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  0 10  0  0  0   0 10  0  0  0  3546
721VecNormalize           2 1.0 9.3970e-06 1.0 6.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 14  0  0  0   1 14  0  0  0   638
722MatMult                2 1.0 1.1177e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   894
723MatSolve               2 1.0 1.9933e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   501
724MatLUFactorNum         1 1.0 3.5081e-05 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  2 10  0  0  0   2 10  0  0  0   114
725MatILUFactorSym        1 1.0 4.4259e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  3  0  0  0  0   3  0  0  0  0     0
726MatAssemblyBegin       1 1.0 8.2015e-08 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
727MatAssemblyEnd         1 1.0 3.3536e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
728MatGetRowIJ            1 1.0 1.5960e-06 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  0  0  0  0  0   0  0  0  0  0     0
729MatGetOrdering         1 1.0 3.9791e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  3  0  0  0  0   3  0  0  0  0     0
730MatView                2 1.0 6.7909e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  5  0  0  0  0   5  0  0  0  0     0
731KSPGMRESOrthog         1 1.0 7.5970e-06 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 10  0  0  0   1 10  0  0  0   526
732KSPSetUp               1 1.0 3.4424e-05 1.0 0.00e+00 0.0 0.0e+00 0.0e+00 0.0e+00  2  0  0  0  0   2  0  0  0  0     0
733KSPSolve               1 1.0 2.7264e-04 1.0 3.30e+04 1.0 0.0e+00 0.0e+00 0.0e+00 19 79  0  0  0  19 79  0  0  0   121
734PCSetUp                1 1.0 1.5234e-04 1.0 4.00e+03 1.0 0.0e+00 0.0e+00 0.0e+00 11 10  0  0  0  11 10  0  0  0    26
735PCApply                2 1.0 2.1022e-05 1.0 9.99e+03 1.0 0.0e+00 0.0e+00 0.0e+00  1 24  0  0  0   1 24  0  0  0   475
736------------------------------------------------------------------------------------------------------------------------
737
738Memory usage is given in bytes:
739
740Object Type          Creations   Destructions     Memory  Descendants' Mem.
741Reports information only for process 0.
742
743--- Event Stage 0: Main Stage
744
745              Vector     8              8        76224     0.
746              Matrix     2              2       134212     0.
747       Krylov Solver     1              1        18400     0.
748      Preconditioner     1              1         1032     0.
749           Index Set     3              3        10328     0.
750              Viewer     1              0            0     0.
751========================================================================================================================
752...
753```
754
755(sec_writing_application_codes)=
756
757# Writing C/C++ or Fortran Applications
758
759The examples throughout the library demonstrate the software usage and
760can serve as templates for developing custom applications. We suggest
761that new PETSc users examine programs in the directories
762`$PETSC_DIR/src/<library>/tutorials` where `<library>` denotes any
763of the PETSc libraries (listed in the following section), such as
764`SNES` or `KSP`, `TS`, or `TAO`. The manual pages at
765<https://petsc.org/release/documentation/> provide links (organized by
766routine names and concepts) to the tutorial examples.
767
768To develop an application program that uses PETSc, we suggest the following:
769
770- {ref}`Download <doc_download>` and {ref}`install <doc_install>` PETSc.
771
772- For completely new applications
773
774  > 1. Make a directory for your source code: for example, `mkdir $HOME/application`
775  >
776  > 2. Change to that directory, for
777  >    example, `cd $HOME/application`
778  >
779  > 3. Copy an example in the directory that corresponds to the
780  >    problems of interest into your directory, for
781  >    example, `cp $PETSC_DIR/src/snes/tutorials/ex19.c app.c`
782  >
783  > 4. Select an application build process. The `PETSC_DIR` (and `PETSC_ARCH` if the `--prefix=directoryname`
784  >    option was not used when configuring PETSc) environmental variable(s) must be
785  >    set for any of these approaches.
786  >
787  >    - make (recommended). It uses the [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) tool
788  >      and is the recommended approach. Copy \$PETSC_DIR/share/petsc/Makefile.user or \$PETSC_DIR/share/petsc/Makefile.basic.user
789  >      to your directory, for example, `cp $PETSC_DIR/share/petsc/Makefile.user makefile`
790  >
791  >      Examine the comments in this makefile.
792  >
793  >      Makefile.user uses the [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) tool and is the recommended approach.
794  >
795  >      Use `make app` to compile your program.
796  >
797  >    - CMake. Copy \$PETSC_DIR/share/petsc/CMakeLists.txt to your directory, for example, `cp $PETSC_DIR/share/petsc/CMakeLists.txt CMakeLists.txt`
798  >
799  >      Edit CMakeLists.txt, read the comments on usage, and change the name of the application from ex1 to your application executable name.
800  >
801  > 5. Run the program, for example,
802  >    `./app`
803  >
804  > 6. Start to modify the program to develop your application.
805
806- For adding PETSc to an existing application
807
808  > 1. Start with a working version of your code that you build and run to confirm that it works.
809  >
810  > 2. Upgrade your build process. The `PETSC_DIR` (and `PETSC_ARCH` if the `--prefix=directoryname`
811  >    option was not used when configuring PETSc) environmental variable(s) must be
812  >    set for any of these approaches.
813  >
814  >    - Using make. Update the application makefile to add the appropriate PETSc include
815  >      directories and libraries.
816  >
817  >      - Recommended approach. Examine the comments in \$PETSC_DIR/share/petsc/Makefile.user and transfer selected portions of
818  >        that file to your makefile.
819  >
820  >      - Minimalist. Add the line
821  >
822  >        ```console
823  >        include ${PETSC_DIR}/lib/petsc/conf/variables
824  >        ```
825  >
826  >        to the bottom of your makefile. This will provide a set of PETSc-specific make variables you may use in your makefile. See
827  >        the comments in the file \$PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage.
828  >
829  >      - Simple, but hands the build process over to PETSc's control. Add the lines
830  >
831  >        ```console
832  >        include ${PETSC_DIR}/lib/petsc/conf/variables
833  >        include ${PETSC_DIR}/lib/petsc/conf/rules
834  >        ```
835  >
836  >        to the bottom of your makefile. See the comments in the file \$PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage.
837  >        Since PETSc's rules now control the build process, you will likely need to simplify and remove much of the material that is in
838  >        your makefile.
839  >
840  >      - Not recommended since you must change your makefile for each new configuration/computing system. This approach does not require
841  >        the environmental variable `PETSC_DIR` to be set when building your application since the information will be hardwired in your
842  >        makefile. Run the following command in the PETSc root directory to get the information needed by your makefile:
843  >
844  >        ```console
845  >        $ make getlinklibs getincludedirs getcflags getcxxflags getfortranflags getccompiler getfortrancompiler getcxxcompiler
846  >        ```
847  >
848  >        All the libraries listed need to be linked into your executable, and the
849  >        include directories and flags need to be passed to the compiler(s). Usually,
850  >        this is done by setting `LDFLAGS=<list of library flags and libraries>` and
851  >        `CFLAGS=<list of -I and other flags>` and `FFLAGS=<list of -I and other flags>` etc in your makefile.
852  >
853  >    - Using CMake. Update the application CMakeLists.txt by examining the code and comments in
854  >      \$PETSC_DIR/share/petsc/CMakeLists.txt
855  >
856  > 3. Rebuild your application and ensure it still runs correctly.
857  >
858  > 4. Add a `PetscInitialize()` near the beginning of your code and `PetscFinalize()` near the end with appropriate include commands
859  >    (and use statements in Fortran).
860  >
861  > 5. Rebuild your application and ensure it still runs correctly.
862  >
863  > 6. Slowly start utilizing PETSc functionality in your code, and ensure that your code continues to build and run correctly.
864
865(sec_oo)=
866
867# PETSc's Object-Oriented Design
868
869Though PETSc has a large API, conceptually, it's rather simple.
870There are three abstract basic data objects (classes): index sets, `IS`, vectors, `Vec`, and matrices, `Mat`.
871Plus, a larger number of abstract algorithm objects (classes) starting with: preconditioners, `PC`, Krylov solvers, `KSP`, and so forth.
872
873Let `Object`
874represent any of these objects. Objects are created with
875
876```
877Object obj;
878ObjectCreate(MPI_Comm, &obj);
879```
880
881The object is initially empty, and little can be done with it. A particular implementation of the class is associated with the object by setting the object's "type", where type
882is merely a string name of an implementation class using
883
884```
885ObjectSetType(obj,"ImplementationName");
886```
887
888Some objects support subclasses, which are specializations of the type. These are set with
889
890```
891ObjectNameSetType(obj,"ImplementationSubName");
892```
893
894For example, within `TS` one may do
895
896```
897TS ts;
898TSCreate(PETSC_COMM_WORLD,&ts);
899TSSetType(ts,TSARKIMEX);
900TSARKIMEXSetType(ts,TSARKIMEX3);
901```
902
903The abstract class `TS` can embody any ODE/DAE integrator scheme.
904This example creates an additive Runge-Kutta ODE/DAE IMEX integrator, whose type name is `TSARKIMEX`, using a 3rd-order scheme with an L-stable implicit part,
905whose subtype name is `TSARKIMEX3`.
906
907To allow PETSc objects to be runtime configurable, PETSc objects provide a universal way of selecting types (classes) and subtypes at runtime from
908what is referred to as the PETSc "options database". The code above can be replaced with
909
910```
911TS obj;
912TSCreate(PETSC_COMM_WORLD,&obj);
913TSSetFromOptions(obj);
914```
915
916now, both the type and subtype can be conveniently set from the command line
917
918```console
919$ ./app -ts_type arkimex -ts_arkimex_type 3
920```
921
922The object's type (implementation class) or subclass can also be changed at any time simply by calling `TSSetType()` again (though to override command line options, the call to `TSSetType()` must be made \_after\_ `TSSetFromOptions()`). For example:
923
924```
925// (if set) command line options "override" TSSetType()
926TSSetType(ts, TSGLLE);
927TSSetFromOptions(ts);
928
929// TSSetType() overrides command line options
930TSSetFromOptions(ts);
931TSSetType(ts, TSGLLE);
932```
933
934Since the later call always overrides the earlier call, the second form shown is rarely -- if ever -- used, as it is less flexible than configuring command line settings.
935
936The standard methods on an object are of the general form.
937
938```
939ObjectSetXXX(obj,...);
940ObjectGetXXX(obj,...);
941ObjectYYY(obj,...);
942```
943
944For example
945
946```
947TSSetRHSFunction(obj,...)
948```
949
950Particular types and subtypes of objects may have their own methods, which are given in the form
951
952```
953ObjectNameSetXXX(obj,...);
954ObjectNameGetXXX(obj,...);
955ObjectNameYYY(obj,...);
956```
957
958and
959
960```
961ObjectNameSubNameSetXXX(obj,...);
962ObjectNameSubNameGetXXX(obj,...);
963ObjectNameSubNameYYY(obj,...);
964```
965
966where Name and SubName are the type and subtype names (for example, as above `TSARKIMEX` and `3`. Most "set" operations have options database versions with the same
967names in lower case, separated by underscores, and with the word "set" removed. For example,
968
969```
970KSPGMRESSetRestart(obj,30);
971```
972
973can be set at the command line with
974
975```console
976$ ./app -ksp_gmres_restart 30
977```
978
979A special subset of type-specific methods is ignored if the type does not match the function name. These are usually setter functions that control some aspect specific to the subtype.
980Note that we leveraged this functionality in the MPI example above ({any}`sec_mpi`) by calling `Mat*SetPreallocation()` for a number of different matrix types. As another example,
981
982```
983KSPGMRESSetRestart(obj,30);   // ignored if the type is not KSPGMRES
984```
985
986These allow cleaner application code since it does not have many if statements to avoid inactive methods. That is, one does not need to write code like
987
988```
989if (type == KSPGMRES) {     // unneeded clutter
990  KSPGMRESSetRestart(obj,30);
991}
992```
993
994Many "get" routines give one temporary access to an object's internal data. They are used in the style
995
996```
997XXX xxx;
998ObjectGetXXX(obj,&xxx);
999// use xxx
1000ObjectRestoreXXX(obj,&xxx);
1001```
1002
1003Objects obtained with a "get" routine should be returned with a "restore" routine, generally within the same function. Objects obtained with a "create" routine should be freed
1004with a "destroy" routine.
1005
1006There may be variants of the "get" routines that give more limited access to the obtained object. For example,
1007
1008```
1009const PetscScalar *x;
1010
1011// specialized variant of VecGetArray()
1012VecGetArrayRead(vec, &x);
1013// one can read but not write with x[]
1014PetscReal y = 2*x[0];
1015// don't forget to restore x after you are done with it
1016VecRestoreArrayRead(vec, &x);
1017```
1018
1019Objects can be displayed (in a large number of ways) with
1020
1021```
1022ObjectView(obj,PetscViewer viewer);
1023ObjectViewFromOptions(obj,...);
1024```
1025
1026Where `PetscViewer` is an abstract object that can represent standard output, an ASCII or binary file, a graphical window, etc. The second
1027variant allows the user to delay until runtime the decision of what viewer and format to use to view the object or if to view the object at all.
1028
1029Objects are destroyed with
1030
1031```
1032ObjectDestroy(&obj)
1033```
1034
1035:::{figure} /images/manual/objectlife.svg
1036:name: fig_objectlife
1037
1038Sample lifetime of a PETSc object
1039:::
1040
1041## User Callbacks
1042
1043The user may wish to override or provide custom functionality in many situations. This is handled via callbacks, which the library will call at the appropriate time. The most general way to apply a callback has this form:
1044
1045```
1046ObjectCallbackSetter(obj, callbackfunction(), PetscCtx ctx, contextdestroy(PetscCtx ctx));
1047```
1048
1049where `ObjectCallbackSetter()` is a callback setter such as `SNESSetFunction()`. `callbackfunction()` is what will be called
1050by the library, `ctx` is an optional data structure (array, struct, PETSc object) that is used by `callbackfunction()`
1051and `contextdestroy(PetscCtx ctx)` is an optional function that will be called when `obj` is destroyed to free
1052anything in `ctx`. The use of the `contextdestroy()` allows users to "set and forget"
1053data structures that will not be needed elsewhere but still need to be deleted when no longer needed. Here is an example of the use of a full-fledged callback
1054
1055```
1056TS              ts;
1057TSMonitorLGCtx *ctx;
1058
1059TSMonitorLGCtxCreate(..., &ctx)
1060TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy);
1061TSSolve(ts);
1062```
1063
1064Occasionally, routines to set callback functions take additional data objects that will be used by the object but are not context data for the function. For example,
1065
1066```
1067SNES obj;
1068Vec  r;
1069PetscCtx ctx;
1070
1071SNESSetFunction(snes, r, UserApplyFunction(SNES, Vec, Vec, PetscCtx ctx), ctx);
1072```
1073
1074The `r` vector is an optional argument provided by the user, which will be used as work-space by `SNES`. Note that this callback does not provide a way for the user
1075to have the `ctx` destroyed when the `SNES` object is destroyed; the users must ensure that they free it at an appropriate time. There is no logic to the various ways
1076PETSc accepts callback functions in different places in the code.
1077
1078See {any}`fig_taocallbacks` for a cartoon on callbacks in `Tao`.
1079
1080(sec_directory)=
1081
1082# Directory Structure
1083
1084We conclude this introduction with an overview of the organization of
1085the PETSc software. The root directory of PETSc contains the following
1086directories:
1087
1088- `doc` The source code and Python scripts for building the website and documentation
1089
1090- `lib/petsc/conf` - Base PETSc configuration files that define the standard
1091  make variables and rules used by PETSc
1092
1093- `include` - All include files for PETSc that are visible to the
1094  user.
1095
1096- `include/petsc/finclude` - PETSc Fortran include files.
1097
1098- `include/petsc/private` - Private PETSc include files that should
1099  *not* need to be used by application programmers.
1100
1101- `share` - Some small test matrices and other data files
1102
1103- `src` - The source code for all PETSc libraries, which currently
1104  includes
1105
1106  - `vec` - vectors,
1107
1108    - `is` - index sets,
1109
1110  - `mat` - matrices,
1111
1112  - `ksp` - complete linear equations solvers,
1113
1114    - `ksp` - Krylov subspace accelerators,
1115    - `pc` - preconditioners,
1116
1117  - `snes` - nonlinear solvers
1118
1119  - `ts` - ODE/DAE solvers and timestepping,
1120
1121  - `tao` - optimizers,
1122
1123  - `ml` - Machine Learning
1124
1125    - `regressor` - Regression solvers
1126
1127  - `dm` - data management between meshes and solvers, vectors, and
1128    matrices,
1129
1130  - `sys` - general system-related routines,
1131
1132    - `logging` - PETSc logging and profiling routines,
1133
1134    - `classes` - low-level classes
1135
1136      - `draw` - simple graphics,
1137      - `viewer` - a mechanism for printing and visualizing PETSc
1138        objects,
1139      - `bag` - mechanism for saving and loading from disk user
1140        data stored in C structs.
1141      - `random` - random number generators.
1142
1143Each PETSc source code library directory has the following subdirectories:
1144
1145- `tutorials` - Programs designed to teach users about PETSc.
1146  These codes can serve as templates for applications.
1147- `tests` - Programs designed for thorough testing of PETSc. As
1148  such, these codes are not intended for examination by users.
1149- `interface` - Provides the abstract base classes for the objects.
1150  The code here does not know about particular implementations and does not perform
1151  operations on the underlying numerical data.
1152- `impls` - Source code for one or more implementations of the class for particular
1153  data structures or algorithms.
1154- `utils` - Utility routines. The source here may know about the
1155  implementations, but ideally, will not know about implementations for
1156  other components.
1157
1158```{rubric} Footnotes
1159```
1160
1161[^debug-footnote]: Configure PETSc with `--with-debugging`.
1162
1163```{eval-rst}
1164.. bibliography:: /petsc.bib
1165   :filter: docname in docnames
1166```
1167