17f296bb3SBarry Smith(sec_getting_started)= 27f296bb3SBarry Smith 37f296bb3SBarry Smith# Getting Started 47f296bb3SBarry Smith 57f296bb3SBarry SmithPETSc consists of a collection of classes, 67f296bb3SBarry Smithwhich are discussed in detail in later parts of the manual ({doc}`programming` and {doc}`additional`). 77f296bb3SBarry SmithThe important PETSc classes include 87f296bb3SBarry Smith 97f296bb3SBarry Smith- index sets (`IS`), for indexing into 107f296bb3SBarry Smith vectors, renumbering, permuting, etc; 117f296bb3SBarry Smith- {any}`ch_vectors` (`Vec`); 127f296bb3SBarry Smith- (generally sparse) {any}`ch_matrices` (`Mat`) 137f296bb3SBarry Smith- {any}`ch_ksp` (`KSP`); 147f296bb3SBarry Smith- preconditioners, including multigrid, block solvers, patch solvers, and 157f296bb3SBarry Smith sparse direct solvers (`PC`); 167f296bb3SBarry Smith- {any}`ch_snes` (`SNES`); 177f296bb3SBarry Smith- {any}`ch_ts` for solving time-dependent (nonlinear) PDEs, including 187f296bb3SBarry Smith support for differential-algebraic-equations, and the computation of 197f296bb3SBarry Smith adjoints (sensitivities/gradients of the solutions) (`TS`); 207f296bb3SBarry Smith- scalable {any}`ch_tao` including a rich set of gradient-based optimizers, 217f296bb3SBarry Smith Newton-based optimizers and optimization with constraints (`Tao`). 2234b254c5SRichard Tran Mills- {any}ch_regressor (`PetscRegressor)` 237f296bb3SBarry Smith- {any}`ch_dmbase` code for managing interactions between mesh data structures and vectors, 247f296bb3SBarry Smith matrices, and solvers (`DM`); 257f296bb3SBarry Smith 267f296bb3SBarry SmithEach class consists of an abstract interface (simply a set of calling 277f296bb3SBarry Smithsequences corresponding to an abstract base class in C++) and an implementation for each algorithm and data structure. 287f296bb3SBarry SmithThis design enables easy comparison and use of different 297f296bb3SBarry Smithalgorithms (for example, experimenting with different Krylov subspace 307f296bb3SBarry Smithmethods, preconditioners, or truncated Newton methods). Hence, PETSc 317f296bb3SBarry Smithprovides a rich environment for modeling scientific applications as well 327f296bb3SBarry Smithas for rapid algorithm design and prototyping. 337f296bb3SBarry Smith 347f296bb3SBarry SmithThe classes enable easy customization and extension of both algorithms 357f296bb3SBarry Smithand implementations. This approach promotes code reuse and flexibility. 367f296bb3SBarry SmithThe PETSc infrastructure creates a foundation for building large-scale 377f296bb3SBarry Smithapplications. 387f296bb3SBarry Smith 397f296bb3SBarry SmithIt is useful to consider the interrelationships among different pieces 407f296bb3SBarry Smithof PETSc. {any}`fig_library` is a diagram of some 417f296bb3SBarry Smithof these pieces. The figure illustrates the library’s hierarchical 427f296bb3SBarry Smithorganization, enabling users to employ the most appropriate solvers for a particular problem. 437f296bb3SBarry Smith 447f296bb3SBarry Smith:::{figure} /images/manual/library_structure.svg 457f296bb3SBarry Smith:alt: PETSc numerical libraries 467f296bb3SBarry Smith:name: fig_library 477f296bb3SBarry Smith 487f296bb3SBarry SmithNumerical Libraries in PETSc 497f296bb3SBarry Smith::: 507f296bb3SBarry Smith 517f296bb3SBarry Smith## Suggested Reading 527f296bb3SBarry Smith 537f296bb3SBarry SmithThe manual is divided into four parts: 547f296bb3SBarry Smith 557f296bb3SBarry Smith- {doc}`introduction` 567f296bb3SBarry Smith- {doc}`programming` 577f296bb3SBarry Smith- {doc}`dm` 587f296bb3SBarry Smith- {doc}`additional` 597f296bb3SBarry Smith 607f296bb3SBarry Smith{doc}`introduction` describes the basic procedure for using the PETSc library and 617f296bb3SBarry Smithpresents simple examples of solving linear systems with PETSc. This 627f296bb3SBarry Smithsection conveys the typical style used throughout the library and 637f296bb3SBarry Smithenables the application programmer to begin using the software 647f296bb3SBarry Smithimmediately. 657f296bb3SBarry Smith 667f296bb3SBarry Smith{doc}`programming` explains in detail the use of the various PETSc algebraic objects, such 677f296bb3SBarry Smithas vectors, matrices, index sets, and PETSc solvers, including linear and nonlinear solvers, time integrators, 687f296bb3SBarry Smithand optimization support. 697f296bb3SBarry Smith 707f296bb3SBarry Smith{doc}`dm` details how a user's models and discretizations can easily be interfaced with the 717f296bb3SBarry Smithsolvers by using the `DM` construct. 727f296bb3SBarry Smith 737f296bb3SBarry Smith{doc}`additional` describes a variety of useful information, including 747f296bb3SBarry Smithprofiling, the options database, viewers, error handling, and some 757f296bb3SBarry Smithdetails of PETSc design. 767f296bb3SBarry Smith 777f296bb3SBarry Smith[Visual Studio Code](https://code.visualstudio.com/), Eclipse, Emacs, and Vim users may find their development environment's options for 787f296bb3SBarry Smithsearching in the source code are 797f296bb3SBarry Smithuseful for exploring the PETSc source code. Details of this feature are provided in {any}`sec_developer_environments`. 807f296bb3SBarry Smith 817f296bb3SBarry Smith**Note to Fortran Programmers**: In most of the manual, the examples and calling sequences are given 827f296bb3SBarry Smithfor the C/C++ family of programming languages. However, Fortran 837f296bb3SBarry Smithprogrammers can use all of the functionality of PETSc from Fortran, 847f296bb3SBarry Smithwith only minor differences in the user interface. 857f296bb3SBarry Smith{any}`ch_fortran` provides a discussion of the differences between 867f296bb3SBarry Smithusing PETSc from Fortran and C, as well as several complete Fortran 877f296bb3SBarry Smithexamples. 887f296bb3SBarry Smith 897f296bb3SBarry Smith**Note to Python Programmers**: To program with PETSc in Python, you need to enable Python bindings 907f296bb3SBarry Smith(i.e. petsc4py) with the configure option `--with-petsc4py=1`. See the 917f296bb3SBarry Smith{doc}`PETSc installation guide </install/index>` 927f296bb3SBarry Smithfor more details. 937f296bb3SBarry Smith 947f296bb3SBarry Smith(sec_running)= 957f296bb3SBarry Smith 967f296bb3SBarry Smith## Running PETSc Programs 977f296bb3SBarry Smith 987f296bb3SBarry SmithBefore using PETSc, the user must first set the environmental variable 997f296bb3SBarry Smith`PETSC_DIR` to indicate the full path of the PETSc home directory. For 1007f296bb3SBarry Smithexample, under the Unix bash shell, a command of the form 1017f296bb3SBarry Smith 1027f296bb3SBarry Smith```console 1037f296bb3SBarry Smith$ export PETSC_DIR=$HOME/petsc 1047f296bb3SBarry Smith``` 1057f296bb3SBarry Smith 1067f296bb3SBarry Smithcan be placed in the user’s `.bashrc` or other startup file. In 1077f296bb3SBarry Smithaddition, the user may need to set the environment variable 1087f296bb3SBarry Smith`$PETSC_ARCH` to specify a particular configuration of the PETSc 1097f296bb3SBarry Smithlibraries. Note that `$PETSC_ARCH` is just a name selected by the 1107f296bb3SBarry Smithinstaller to refer to the libraries compiled for a particular set of 1117f296bb3SBarry Smithcompiler options and machine type. Using different values of 1127f296bb3SBarry Smith`$PETSC_ARCH` allows one to switch between several different sets (say 1137f296bb3SBarry Smithdebug and optimized versions) of libraries easily. To determine if you need to 1147f296bb3SBarry Smithset `$PETSC_ARCH`, look in the directory indicated by `$PETSC_DIR`, if 1157f296bb3SBarry Smiththere are subdirectories beginning with `arch` then those 1167f296bb3SBarry Smithsubdirectories give the possible values for `$PETSC_ARCH`. 1177f296bb3SBarry Smith 1187f296bb3SBarry SmithSee {any}`handson` to immediately jump in and run PETSc code. 1197f296bb3SBarry Smith 1207f296bb3SBarry SmithAll PETSc programs use the MPI (Message Passing Interface) standard for 1217f296bb3SBarry Smithmessage-passing communication {cite}`mpi-final`. Thus, to 1227f296bb3SBarry Smithexecute PETSc programs, users must know the procedure for beginning MPI 1237f296bb3SBarry Smithjobs on their selected computer system(s). For instance, when using the 1247f296bb3SBarry Smith[MPICH](https://www.mpich.org/) implementation of MPI and many 1257f296bb3SBarry Smithothers, the following command initiates a program that uses eight 1267f296bb3SBarry Smithprocessors: 1277f296bb3SBarry Smith 1287f296bb3SBarry Smith```console 1297f296bb3SBarry Smith$ mpiexec -n 8 ./petsc_program_name petsc_options 1307f296bb3SBarry Smith``` 1317f296bb3SBarry Smith 1327f296bb3SBarry SmithPETSc also provides a script that automatically uses the correct 1337f296bb3SBarry Smith`mpiexec` for your configuration. 1347f296bb3SBarry Smith 1357f296bb3SBarry Smith```console 1367f296bb3SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 8 ./petsc_program_name petsc_options 1377f296bb3SBarry Smith``` 1387f296bb3SBarry Smith 1397f296bb3SBarry SmithCertain options are supported by all PETSc programs. We list a few 1407f296bb3SBarry Smithparticularly useful ones below; a complete list can be obtained by 1417f296bb3SBarry Smithrunning any PETSc program with the option `-help`. 1427f296bb3SBarry Smith 1437f296bb3SBarry Smith- `-log_view` - summarize the program’s performance (see {any}`ch_profiling`) 1447f296bb3SBarry Smith- `-fp_trap` - stop on floating-point exceptions; for example divide 1457f296bb3SBarry Smith by zero 1467f296bb3SBarry Smith- `-malloc_dump` - enable memory tracing; dump list of unfreed memory 1477f296bb3SBarry Smith at conclusion of the run, see 1487f296bb3SBarry Smith {any}`detecting_memory_problems`, 1497f296bb3SBarry Smith- `-malloc_debug` - enable memory debugging (by default, this is 1507f296bb3SBarry Smith activated for the debugging version of PETSc), see 1517f296bb3SBarry Smith {any}`detecting_memory_problems`, 1527f296bb3SBarry Smith- `-start_in_debugger` `[noxterm,gdb,lldb]` 1537f296bb3SBarry Smith `[-display name]` - start all (or a subset of the) processes in a debugger. See 1547f296bb3SBarry Smith {any}`sec_debugging`, for more information on 1557f296bb3SBarry Smith debugging PETSc programs. 1567f296bb3SBarry Smith- `-on_error_attach_debugger` `[noxterm,gdb,lldb]` 1577f296bb3SBarry Smith `[-display name]` - start debugger only on encountering an error 1587f296bb3SBarry Smith- `-info` - print a great deal of information about what the program 1597f296bb3SBarry Smith is doing as it runs 1607f296bb3SBarry Smith- `-version` - display the version of PETSc being used 1617f296bb3SBarry Smith 1627f296bb3SBarry Smith(sec_writing)= 1637f296bb3SBarry Smith 1647f296bb3SBarry Smith## Writing PETSc Programs 1657f296bb3SBarry Smith 1667f296bb3SBarry SmithMost PETSc programs begin with a call to 1677f296bb3SBarry Smith 1687f296bb3SBarry Smith``` 1697f296bb3SBarry SmithPetscInitialize(int *argc,char ***argv,char *file,char *help); 1707f296bb3SBarry Smith``` 1717f296bb3SBarry Smith 1727f296bb3SBarry Smithwhich initializes PETSc and MPI. The arguments `argc` and `argv` are 1737f296bb3SBarry Smiththe usual command line arguments in C and C++ programs. The 1747f296bb3SBarry Smithargument `file` optionally indicates an alternative name for the PETSc 1757f296bb3SBarry Smithoptions file, `.petscrc`, which resides by default in the user’s home 1767f296bb3SBarry Smithdirectory. {any}`sec_options` provides details 1777f296bb3SBarry Smithregarding this file and the PETSc options database, which can be used 1787f296bb3SBarry Smithfor runtime customization. The final argument, `help`, is an optional 1797f296bb3SBarry Smithcharacter string that will be printed if the program is run with the 1807f296bb3SBarry Smith`-help` option. In Fortran, the initialization command has the form 1817f296bb3SBarry Smith 1827f296bb3SBarry Smith```fortran 1837f296bb3SBarry Smithcall PetscInitialize(character(*) file,integer ierr) 1847f296bb3SBarry Smith``` 1857f296bb3SBarry Smith 1867f296bb3SBarry Smithwhere the file argument is optional. 1877f296bb3SBarry Smith 1887f296bb3SBarry Smith`PetscInitialize()` automatically calls `MPI_Init()` if MPI has not 1897f296bb3SBarry Smithbeen not previously initialized. In certain circumstances in which MPI 1907f296bb3SBarry Smithneeds to be initialized directly (or is initialized by some other 1917f296bb3SBarry Smithlibrary), the user can first call `MPI_Init()` (or have the other 1927f296bb3SBarry Smithlibrary do it), and then call `PetscInitialize()`. By default, 1937f296bb3SBarry Smith`PetscInitialize()` sets the PETSc “world” communicator 1947f296bb3SBarry Smith`PETSC_COMM_WORLD` to `MPI_COMM_WORLD`. 1957f296bb3SBarry Smith 1967f296bb3SBarry SmithFor those unfamiliar with MPI, a *communicator* indicates 1977f296bb3SBarry Smitha collection of processes that will be involved in a 1987f296bb3SBarry Smithcalculation or communication. Communicators have the variable type 1997f296bb3SBarry Smith`MPI_Comm`. In most cases, users can employ the communicator 2007f296bb3SBarry Smith`PETSC_COMM_WORLD` to indicate all processes in a given run and 2017f296bb3SBarry Smith`PETSC_COMM_SELF` to indicate a single process. 2027f296bb3SBarry Smith 2037f296bb3SBarry SmithMPI provides routines for generating new communicators consisting of 2047f296bb3SBarry Smithsubsets of processors, though most users rarely need to use these. The 2057f296bb3SBarry Smithbook *Using MPI*, by Lusk, Gropp, and Skjellum 2067f296bb3SBarry Smith{cite}`using-mpi` provides an excellent introduction to the 2077f296bb3SBarry Smithconcepts in MPI. See also the [MPI homepage](https://www.mcs.anl.gov/research/projects/mpi/). 2087f296bb3SBarry SmithNote that PETSc users 2097f296bb3SBarry Smithneed not program much message passing directly with MPI, but they must 2107f296bb3SBarry Smithbe familiar with the basic concepts of message passing and distributed 2117f296bb3SBarry Smithmemory computing. 2127f296bb3SBarry Smith 2137f296bb3SBarry SmithAll PETSc programs should call `PetscFinalize()` as their final (or 2147f296bb3SBarry Smithnearly final) statement. This routine handles options to be called at the conclusion of the 2157f296bb3SBarry Smithprogram and calls `MPI_Finalize()` if `PetscInitialize()` began 2167f296bb3SBarry SmithMPI. If MPI was initiated externally from PETSc (by either the user or 2177f296bb3SBarry Smithanother software package), the user is responsible for calling 2187f296bb3SBarry Smith`MPI_Finalize()`. 2197f296bb3SBarry Smith 2207f296bb3SBarry Smith### Error Checking 2217f296bb3SBarry Smith 2227f296bb3SBarry SmithMost PETSc functions return a `PetscErrorCode`, an integer 2237f296bb3SBarry Smithindicating whether an error occurred during the call. The error code 2247f296bb3SBarry Smithis set to be nonzero if an error has been detected; otherwise, it is 2257f296bb3SBarry Smithzero. For the C/C++ interface, the error variable is the routine’s 2267f296bb3SBarry Smithreturn value, while for the Fortran version, each PETSc routine has an integer error variable as 2277f296bb3SBarry Smithits final argument. 2287f296bb3SBarry Smith 2297f296bb3SBarry SmithOne should always check these routine values as given below in the C/C++ 2307f296bb3SBarry Smithformats, respectively: 2317f296bb3SBarry Smith 2327f296bb3SBarry Smith```c 2337f296bb3SBarry SmithPetscCall(PetscFunction(Args)); 2347f296bb3SBarry Smith``` 2357f296bb3SBarry Smith 2367f296bb3SBarry Smithor for Fortran 2377f296bb3SBarry Smith 2387f296bb3SBarry Smith```fortran 2397f296bb3SBarry Smith! within the main program 2407f296bb3SBarry SmithPetscCallA(PetscFunction(Args,ierr)) 2417f296bb3SBarry Smith``` 2427f296bb3SBarry Smith 2437f296bb3SBarry Smith```fortran 2447f296bb3SBarry Smith! within any subroutine 2457f296bb3SBarry SmithPetscCall(PetscFunction(Args,ierr)) 2467f296bb3SBarry Smith``` 2477f296bb3SBarry Smith 2487f296bb3SBarry SmithThese macros check the returned error code, and if it is nonzero, they call the PETSc error 2497f296bb3SBarry Smithhandler and then return from the function with the error code. The macros above should be used on all PETSc calls to enable 2507f296bb3SBarry Smitha complete error traceback. See {any}`sec_error2` for more details on PETSc error handling. 2517f296bb3SBarry Smith 2527f296bb3SBarry Smith(sec_simple)= 2537f296bb3SBarry Smith 2547f296bb3SBarry Smith## Simple PETSc Examples 2557f296bb3SBarry Smith 2567f296bb3SBarry SmithTo help the user use PETSc immediately, we begin with a simple 2577f296bb3SBarry Smithuniprocessor example that 2587f296bb3SBarry Smithsolves the one-dimensional Laplacian problem with finite differences. 2597f296bb3SBarry SmithThis sequential code illustrates the solution of 2607f296bb3SBarry Smitha linear system with `KSP`, the interface to the preconditioners, 2617f296bb3SBarry SmithKrylov subspace methods and direct linear solvers of PETSc. Following 2627f296bb3SBarry Smiththe code, we highlight a few of the most important parts of this example. 2637f296bb3SBarry Smith 2647f296bb3SBarry Smith:::{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> 2657f296bb3SBarry Smith:name: ksp-ex1 2667f296bb3SBarry Smith 2677f296bb3SBarry Smith```{literalinclude} /../src/ksp/ksp/tutorials/ex1.c 2687f296bb3SBarry Smith:end-before: /*TEST 2697f296bb3SBarry Smith``` 2707f296bb3SBarry Smith::: 2717f296bb3SBarry Smith 2727f296bb3SBarry Smith### Include Files 2737f296bb3SBarry Smith 2747f296bb3SBarry SmithThe C/C++ include files for PETSc should be used via statements such as 2757f296bb3SBarry Smith 2767f296bb3SBarry Smith``` 2777f296bb3SBarry Smith#include <petscksp.h> 2787f296bb3SBarry Smith``` 2797f296bb3SBarry Smith 2807f296bb3SBarry Smithwhere <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscksp.h.html">petscksp.h</a> 2817f296bb3SBarry Smithis the include file for the linear solver library. 2827f296bb3SBarry SmithEach PETSc program must specify an include file corresponding to the 2837f296bb3SBarry Smithhighest level PETSc objects needed within the program; all of the 2847f296bb3SBarry Smithrequired lower level include files are automatically included within the 2857f296bb3SBarry Smithhigher level files. For example, <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscksp.h.html">petscksp.h</a> includes 2867f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscmat.h.html">petscmat.h</a> 2877f296bb3SBarry Smith(matrices), 2887f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscvec.h.html">petscvec.h</a> 2897f296bb3SBarry Smith(vectors), and 2907f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscsys.h.html">petscsys.h</a> 2917f296bb3SBarry Smith(base PETSc 2927f296bb3SBarry Smithfile). The PETSc include files are located in the directory 2937f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/index.html">\$PETSC_DIR/include</a>. 2947f296bb3SBarry SmithSee {any}`sec_fortran_includes` 2957f296bb3SBarry Smithfor a discussion of PETSc include files in Fortran programs. 2967f296bb3SBarry Smith 2977f296bb3SBarry Smith(the_options_database)= 2987f296bb3SBarry Smith 2997f296bb3SBarry Smith### The Options Database 3007f296bb3SBarry Smith 3017f296bb3SBarry SmithAs shown in {any}`sec_simple`, the user can 3027f296bb3SBarry Smithinput control data at run time using the options database. In this 3037f296bb3SBarry Smithexample the command `PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);` 3047f296bb3SBarry Smithchecks whether the user has provided a command line option to set the 3057f296bb3SBarry Smithvalue of `n`, the problem dimension. If so, the variable `n` is set 3067f296bb3SBarry Smithaccordingly; otherwise, `n` remains unchanged. A complete description 3077f296bb3SBarry Smithof the options database may be found in {any}`sec_options`. 3087f296bb3SBarry Smith 3097f296bb3SBarry Smith(sec_vecintro)= 3107f296bb3SBarry Smith 3117f296bb3SBarry Smith### Vectors 3127f296bb3SBarry Smith 3137f296bb3SBarry SmithOne creates a new parallel or sequential vector, `x`, of global 3147f296bb3SBarry Smithdimension `M` with the commands 3157f296bb3SBarry Smith 3167f296bb3SBarry Smith``` 3177f296bb3SBarry SmithVecCreate(MPI_Comm comm,Vec *x); 3187f296bb3SBarry SmithVecSetSizes(Vec x, PetscInt m, PetscInt M); 3197f296bb3SBarry Smith``` 3207f296bb3SBarry Smith 3217f296bb3SBarry Smithwhere `comm` denotes the MPI communicator and `m` is the optional 3227f296bb3SBarry Smithlocal size which may be `PETSC_DECIDE`. The type of storage for the 3237f296bb3SBarry Smithvector may be set with either calls to `VecSetType()` or 3247f296bb3SBarry Smith`VecSetFromOptions()`. Additional vectors of the same type can be 3257f296bb3SBarry Smithformed with 3267f296bb3SBarry Smith 3277f296bb3SBarry Smith``` 3287f296bb3SBarry SmithVecDuplicate(Vec old,Vec *new); 3297f296bb3SBarry Smith``` 3307f296bb3SBarry Smith 3317f296bb3SBarry SmithThe commands 3327f296bb3SBarry Smith 3337f296bb3SBarry Smith``` 3347f296bb3SBarry SmithVecSet(Vec x,PetscScalar value); 3357f296bb3SBarry SmithVecSetValues(Vec x,PetscInt n,PetscInt *indices,PetscScalar *values,INSERT_VALUES); 3367f296bb3SBarry Smith``` 3377f296bb3SBarry Smith 3387f296bb3SBarry Smithrespectively set all the components of a vector to a particular scalar 3397f296bb3SBarry Smithvalue and assign a different value to each component. More detailed 3407f296bb3SBarry Smithinformation about PETSc vectors, including their basic operations, 3417f296bb3SBarry Smithscattering/gathering, index sets, and distributed arrays is available 3427f296bb3SBarry Smithin Chapter {any}`ch_vectors`. 3437f296bb3SBarry Smith 3447f296bb3SBarry SmithNote the use of the PETSc variable type `PetscScalar` in this example. 3457f296bb3SBarry Smith`PetscScalar` is defined to be `double` in C/C++ (or 3467f296bb3SBarry Smithcorrespondingly `double precision` in Fortran) for versions of PETSc 3477f296bb3SBarry Smiththat have *not* been compiled for use with complex numbers. The 3487f296bb3SBarry Smith`PetscScalar` data type enables identical code to be used when the 3497f296bb3SBarry SmithPETSc libraries have been compiled for use with complex numbers. 3507f296bb3SBarry Smith{any}`sec_complex` discusses the use of complex 3517f296bb3SBarry Smithnumbers in PETSc programs. 3527f296bb3SBarry Smith 3537f296bb3SBarry Smith(sec_matintro)= 3547f296bb3SBarry Smith 3557f296bb3SBarry Smith### Matrices 3567f296bb3SBarry Smith 3577f296bb3SBarry SmithThe usage of PETSc matrices and vectors is similar. The user can create a 3587f296bb3SBarry Smithnew parallel or sequential matrix, `A`, which has `M` global rows 3597f296bb3SBarry Smithand `N` global columns, with the routines 3607f296bb3SBarry Smith 3617f296bb3SBarry Smith``` 3627f296bb3SBarry SmithMatCreate(MPI_Comm comm,Mat *A); 3637f296bb3SBarry SmithMatSetSizes(Mat A,PETSC_DECIDE,PETSC_DECIDE,PetscInt M,PetscInt N); 3647f296bb3SBarry Smith``` 3657f296bb3SBarry Smith 3667f296bb3SBarry Smithwhere the matrix format can be specified at runtime via the options 3677f296bb3SBarry Smithdatabase. The user could alternatively specify each processes’ number of 3687f296bb3SBarry Smithlocal rows and columns using `m` and `n`. 3697f296bb3SBarry Smith 3707f296bb3SBarry Smith``` 3717f296bb3SBarry SmithMatSetSizes(Mat A,PetscInt m,PetscInt n,PETSC_DETERMINE,PETSC_DETERMINE); 3727f296bb3SBarry Smith``` 3737f296bb3SBarry Smith 3747f296bb3SBarry SmithGenerally, one then sets the “type” of the matrix, with, for example, 3757f296bb3SBarry Smith 3767f296bb3SBarry Smith``` 3777f296bb3SBarry SmithMatSetType(A,MATAIJ); 3787f296bb3SBarry Smith``` 3797f296bb3SBarry Smith 3807f296bb3SBarry SmithThis causes the matrix `A` to use the compressed sparse row storage 3817f296bb3SBarry Smithformat to store the matrix entries. See `MatType` for a list of all 3827f296bb3SBarry Smithmatrix types. Values can then be set with the command 3837f296bb3SBarry Smith 3847f296bb3SBarry Smith``` 3857f296bb3SBarry SmithMatSetValues(Mat A,PetscInt m,PetscInt *im,PetscInt n,PetscInt *in,PetscScalar *values,INSERT_VALUES); 3867f296bb3SBarry Smith``` 3877f296bb3SBarry Smith 3887f296bb3SBarry SmithAfter all elements have been inserted into the matrix, it must be 3897f296bb3SBarry Smithprocessed with the pair of commands 3907f296bb3SBarry Smith 3917f296bb3SBarry Smith``` 3927f296bb3SBarry SmithMatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); 3937f296bb3SBarry SmithMatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); 3947f296bb3SBarry Smith``` 3957f296bb3SBarry Smith 3967f296bb3SBarry Smith{any}`ch_matrices` discusses various matrix formats as 3977f296bb3SBarry Smithwell as the details of some basic matrix manipulation routines. 3987f296bb3SBarry Smith 3997f296bb3SBarry Smith### Linear Solvers 4007f296bb3SBarry Smith 4017f296bb3SBarry SmithAfter creating the matrix and vectors that define a linear system, 4027f296bb3SBarry Smith`Ax` $=$ `b`, the user can then use `KSP` to solve the 4037f296bb3SBarry Smithsystem with the following sequence of commands: 4047f296bb3SBarry Smith 4057f296bb3SBarry Smith``` 4067f296bb3SBarry SmithKSPCreate(MPI_Comm comm,KSP *ksp); 4077f296bb3SBarry SmithKSPSetOperators(KSP ksp,Mat Amat,Mat Pmat); 4087f296bb3SBarry SmithKSPSetFromOptions(KSP ksp); 4097f296bb3SBarry SmithKSPSolve(KSP ksp,Vec b,Vec x); 4107f296bb3SBarry SmithKSPDestroy(KSP ksp); 4117f296bb3SBarry Smith``` 4127f296bb3SBarry Smith 4137f296bb3SBarry SmithThe user first creates the `KSP` context and sets the operators 4147f296bb3SBarry Smithassociated with the system (matrix that defines the linear system, 4157f296bb3SBarry Smith`Amat` and matrix from which the preconditioner is constructed, 4167f296bb3SBarry Smith`Pmat` ). The user then sets various options for customized solutions, 4177f296bb3SBarry Smithsolves the linear system, and finally destroys the `KSP` context. The command `KSPSetFromOptions()` enables the user to 4187f296bb3SBarry Smithcustomize the linear solution method at runtime using the options 4197f296bb3SBarry Smithdatabase, which is discussed in {any}`sec_options`. Through this database, the 4207f296bb3SBarry Smithuser not only can select an iterative method and preconditioner, but 4217f296bb3SBarry Smithcan also prescribe the convergence tolerance, set various monitoring 4227f296bb3SBarry Smithroutines, etc. (see, e.g., {any}`sec_profiling_programs`). 4237f296bb3SBarry Smith 4247f296bb3SBarry Smith{any}`ch_ksp` describes in detail the `KSP` package, 4257f296bb3SBarry Smithincluding the `PC` and `KSP` packages for preconditioners and Krylov 4267f296bb3SBarry Smithsubspace methods. 4277f296bb3SBarry Smith 4287f296bb3SBarry Smith### Nonlinear Solvers 4297f296bb3SBarry Smith 4307f296bb3SBarry SmithPETSc provides 4317f296bb3SBarry Smithan interface to tackle nonlinear problems called `SNES`. 4327f296bb3SBarry Smith{any}`ch_snes` describes the nonlinear 4337f296bb3SBarry Smithsolvers in detail. We highly recommend most PETSc users work directly with 4347f296bb3SBarry Smith`SNES`, rather than using PETSc for the linear problem and writing their own 4357f296bb3SBarry Smithnonlinear solver. Similarly, users should use `TS` rather than rolling their own time integrators. 4367f296bb3SBarry Smith 4377f296bb3SBarry Smith(sec_error2)= 4387f296bb3SBarry Smith 4397f296bb3SBarry Smith### Error Checking 4407f296bb3SBarry Smith 4417f296bb3SBarry SmithAs noted above, PETSc functions return a `PetscErrorCode`, which is an integer 4427f296bb3SBarry Smithindicating whether an error has occurred during the call. Below, we indicate a traceback 4437f296bb3SBarry Smithgenerated by error detection within a sample PETSc program. The error 4447f296bb3SBarry Smithoccurred on line 3618 of the file 4457f296bb3SBarry Smith`$PETSC_DIR/src/mat/impls/aij/seq/aij.c` and was caused by trying to 4467f296bb3SBarry Smithallocate too large an array in memory. The routine was called in the 4477f296bb3SBarry Smithprogram `ex3.c` on line 66. See 4487f296bb3SBarry Smith{any}`sec_fortran_errors` for details regarding error checking 4497f296bb3SBarry Smithwhen using the PETSc Fortran interface. 4507f296bb3SBarry Smith 4517f296bb3SBarry Smith```none 4527f296bb3SBarry Smith$ cd $PETSC_DIR/src/ksp/ksp/tutorials 4537f296bb3SBarry Smith$ make ex3 4547f296bb3SBarry Smith$ mpiexec -n 1 ./ex3 -m 100000 4557f296bb3SBarry Smith[0]PETSC ERROR: --------------------- Error Message -------------------------------- 4567f296bb3SBarry Smith[0]PETSC ERROR: Out of memory. This could be due to allocating 4577f296bb3SBarry Smith[0]PETSC ERROR: too large an object or bleeding by not properly 4587f296bb3SBarry Smith[0]PETSC ERROR: destroying unneeded objects. 4597f296bb3SBarry Smith[0]PETSC ERROR: Memory allocated 11282182704 Memory used by process 7075897344 4607f296bb3SBarry Smith[0]PETSC ERROR: Try running with -malloc_dump or -malloc_view for info. 4617f296bb3SBarry Smith[0]PETSC ERROR: Memory requested 18446744072169447424 4627f296bb3SBarry Smith[0]PETSC ERROR: PETSc Development Git Revision: v3.7.1-224-g9c9a9c5 Git Date: 2016-05-18 22:43:00 -0500 4637f296bb3SBarry Smith[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 4647f296bb3SBarry Smith[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 4657f296bb3SBarry Smith[0]PETSC ERROR: #1 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c 4667f296bb3SBarry Smith[0]PETSC ERROR: #2 PetscTrMallocDefault() line 188 in /Users/patrick/petsc/src/sys/memory/mtr.c 4677f296bb3SBarry Smith[0]PETSC ERROR: #3 MatSeqAIJSetPreallocation_SeqAIJ() line 3618 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c 4687f296bb3SBarry Smith[0]PETSC ERROR: #4 MatSeqAIJSetPreallocation() line 3562 in /Users/patrick/petsc/src/mat/impls/aij/seq/aij.c 4697f296bb3SBarry Smith[0]PETSC ERROR: #5 main() line 66 in /Users/patrick/petsc/src/ksp/ksp/tutorials/ex3.c 4707f296bb3SBarry Smith[0]PETSC ERROR: PETSc Option Table entries: 4717f296bb3SBarry Smith[0]PETSC ERROR: -m 100000 4727f296bb3SBarry Smith[0]PETSC ERROR: ----------------End of Error Message ------- send entire error message to petsc-maint@mcs.anl.gov---------- 4737f296bb3SBarry Smith``` 4747f296bb3SBarry Smith 4757f296bb3SBarry SmithWhen running the debug version [^debug-footnote] of the PETSc libraries, it checks for memory corruption (writing outside of array bounds 4767f296bb3SBarry Smith, etc.). The macro `CHKMEMQ` can be called anywhere in the code to check 4777f296bb3SBarry Smiththe current status of the memory for corruption. By putting several (or 4787f296bb3SBarry Smithmany) of these macros into your code, you can usually easily track down 4797f296bb3SBarry Smithin what small segment of your code the corruption has occurred. One can 4807f296bb3SBarry Smithalso use Valgrind to track down memory errors; see the [FAQ](https://petsc.org/release/faq/). 4817f296bb3SBarry Smith 4827f296bb3SBarry SmithFor complete error handling, calls to MPI functions should be made with `PetscCallMPI(MPI_Function(Args))`. 4837f296bb3SBarry SmithIn Fortran subroutines use `PetscCallMPI(MPI_Function(Args, ierr))` and in Fortran main use 4847f296bb3SBarry Smith`PetscCallMPIA(MPI_Function(Args, ierr))`. 4857f296bb3SBarry Smith 4867f296bb3SBarry SmithPETSc has a small number of C/C++-only macros that do not explicitly return error codes. These are used in the style 4877f296bb3SBarry Smith 4887f296bb3SBarry Smith```c 4897f296bb3SBarry SmithXXXBegin(Args); 4907f296bb3SBarry Smithother code 4917f296bb3SBarry SmithXXXEnd(); 4927f296bb3SBarry Smith``` 4937f296bb3SBarry Smith 4947f296bb3SBarry Smithand include `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscObjectOptionsBegin()`, 4957f296bb3SBarry Smith`PetscOptionsHeadBegin()`, `PetscOptionsHeadEnd()`, `PetscDrawCollectiveBegin()`, `PetscDrawCollectiveEnd()`, 4967f296bb3SBarry Smith`MatPreallocateEnd()`, and `MatPreallocateBegin()`. These should not be checked for error codes. 4977f296bb3SBarry SmithAnother class of functions with the `Begin()` and `End()` paradigm 4987f296bb3SBarry Smithincluding `MatAssemblyBegin()`, and `MatAssemblyEnd()` do return error codes that should be checked. 4997f296bb3SBarry Smith 5007f296bb3SBarry SmithPETSc also has a set of C/C++-only macros that return an object, or `NULL` if an error has been detected. These include 5017f296bb3SBarry Smith`PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_DRAW_WORLD`, `PETSC_VIEWER_STDOUT_(MPI_Comm)`, and `PETSC_VIEWER_DRAW_(MPI_Comm)`. 5027f296bb3SBarry Smith 5037f296bb3SBarry SmithFinally `PetscObjectComm((PetscObject)x)` returns the communicator associated with the object `x` or `MPI_COMM_NULL` if an 5047f296bb3SBarry Smitherror was detected. 5057f296bb3SBarry Smith 5067f296bb3SBarry Smith(sec_parallel)= 5077f296bb3SBarry Smith 5087f296bb3SBarry Smith# Parallel and GPU Programming 5097f296bb3SBarry Smith 5107f296bb3SBarry SmithNumerical computing today has multiple levels of parallelism (concurrency). 5117f296bb3SBarry Smith 5127f296bb3SBarry Smith- Low-level, single instruction multiple data (SIMD) parallelism or, somewhat similar, on-GPU parallelism, 5137f296bb3SBarry Smith- medium-level, multiple instruction multiple data shared memory parallelism (thread parallelism), and 5147f296bb3SBarry Smith- high-level, distributed memory parallelism. 5157f296bb3SBarry Smith 5167f296bb3SBarry SmithTraditional 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`), 5177f296bb3SBarry Smith(or multiple processes). GPUs also support the lower two levels via kernel functions ({any}`sec_gpu_kernels`) and streams ({any}`sec_gpu_streams`). 5187f296bb3SBarry SmithDistributed memory parallelism is created by combining multiple 5197f296bb3SBarry SmithCPUs and/or GPUs and using MPI for communication ({any}`sec_mpi`). 5207f296bb3SBarry Smith 5217f296bb3SBarry SmithIn addition, there is also concurrency between computations (floating point operations) and data movement (from memory to caches and registers 5227f296bb3SBarry Smithand via MPI between distinct memory nodes). 5237f296bb3SBarry Smith 5247f296bb3SBarry SmithPETSc supports all these parallelism levels, but its strongest support is for MPI-based distributed memory parallelism. 5257f296bb3SBarry Smith 5267f296bb3SBarry Smith(sec_mpi)= 5277f296bb3SBarry Smith 5287f296bb3SBarry Smith## MPI Parallelism 5297f296bb3SBarry Smith 5307f296bb3SBarry SmithSince PETSc uses the message-passing model for parallel programming and 5317f296bb3SBarry Smithemploys MPI for all interprocessor communication, the user can 5327f296bb3SBarry Smithemploy MPI routines as needed throughout an application code. However, 5337f296bb3SBarry Smithby default, the user is shielded from many of the details of message 5347f296bb3SBarry Smithpassing within PETSc since these are hidden within parallel objects, 5357f296bb3SBarry Smithsuch as vectors, matrices, and solvers. In addition, PETSc provides 5367f296bb3SBarry Smithtools such as vector scatter and gather to assist in the 5377f296bb3SBarry Smithmanagement of parallel data. 5387f296bb3SBarry Smith 5397f296bb3SBarry SmithRecall that the user must specify a communicator upon creation of any 5407f296bb3SBarry SmithPETSc object (such as a vector, matrix, or solver) to indicate the 5417f296bb3SBarry Smithprocessors over which the object is to be distributed. For example, as 5427f296bb3SBarry Smithmentioned above, some commands for matrix, vector, and linear solver 5437f296bb3SBarry Smithcreation are: 5447f296bb3SBarry Smith 5457f296bb3SBarry Smith``` 5467f296bb3SBarry SmithMatCreate(MPI_Comm comm,Mat *A); 5477f296bb3SBarry SmithVecCreate(MPI_Comm comm,Vec *x); 5487f296bb3SBarry SmithKSPCreate(MPI_Comm comm,KSP *ksp); 5497f296bb3SBarry Smith``` 5507f296bb3SBarry Smith 5517f296bb3SBarry SmithThe creation routines are collective on all processes in the 5527f296bb3SBarry Smithcommunicator; thus, all processors in the communicator *must* call the 5537f296bb3SBarry Smithcreation routine. In addition, if a sequence of collective routines is 5547f296bb3SBarry Smithbeing used, they *must* be called in the same order on each process. 5557f296bb3SBarry Smith 5567f296bb3SBarry SmithThe next example, given below, 5577f296bb3SBarry Smithillustrates the solution of a linear system in parallel. This code, 5587f296bb3SBarry Smithcorresponding to 5597f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex2.c.html">KSP Tutorial ex2</a>, 5607f296bb3SBarry Smithhandles the two-dimensional Laplacian discretized with finite 5617f296bb3SBarry Smithdifferences, where the linear system is again solved with KSP. The code 5627f296bb3SBarry Smithperforms the same tasks as the sequential version within 5637f296bb3SBarry Smith{any}`sec_simple`. Note that the user interface 5647f296bb3SBarry Smithfor initiating the program, creating vectors and matrices, and solving 5657f296bb3SBarry Smiththe linear system is *exactly* the same for the uniprocessor and 5667f296bb3SBarry Smithmultiprocessor examples. The primary difference between the examples in 5677f296bb3SBarry Smith{any}`sec_simple` and 5687f296bb3SBarry Smithhere is each processor forms only its 5697f296bb3SBarry Smithlocal part of the matrix and vectors in the parallel case. 5707f296bb3SBarry Smith 5717f296bb3SBarry Smith:::{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> 5727f296bb3SBarry Smith:name: ksp-ex2 5737f296bb3SBarry Smith 5747f296bb3SBarry Smith```{literalinclude} /../src/ksp/ksp/tutorials/ex2.c 5757f296bb3SBarry Smith:end-before: /*TEST 5767f296bb3SBarry Smith``` 5777f296bb3SBarry Smith::: 5787f296bb3SBarry Smith 5797f296bb3SBarry Smith(sec_cpu_simd)= 5807f296bb3SBarry Smith 5817f296bb3SBarry Smith## CPU SIMD parallelism 5827f296bb3SBarry Smith 5837f296bb3SBarry SmithSIMD parallelism occurs most commonly in the Intel advanced vector extensions (AVX) families of instructions (see [Wikipedia](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)). 5847f296bb3SBarry SmithIt may be automatically used by the optimizing compiler or in low-level libraries that PETSc uses, such as BLAS 5857f296bb3SBarry Smith(see [BLIS](https://github.com/flame/blis)), or rarely, 5867f296bb3SBarry Smithdirectly in PETSc C/C++ code, as in [MatMult_SeqSELL](https://petsc.org/main/src/mat/impls/sell/seq/sell.c.html#MatMult_SeqSELL). 5877f296bb3SBarry Smith 5887f296bb3SBarry Smith(sec_cpu_openmp)= 5897f296bb3SBarry Smith 5907f296bb3SBarry Smith## CPU OpenMP parallelism 5917f296bb3SBarry Smith 5927f296bb3SBarry SmithOpenMP parallelism is thread parallelism. Multiple threads (independent streams of instructions) process data and perform computations on different 5937f296bb3SBarry Smithparts of memory that is 5947f296bb3SBarry Smithshared (accessible) to all of the threads. The OpenMP model is based on inserting pragmas into code, indicating that a series of instructions 5957f296bb3SBarry Smith(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 5967f296bb3SBarry Smithcomputationally expensive parts in the 'parallel region' are parallel. Thus, OpenMP makes it relatively easy to add some 5977f296bb3SBarry Smithparallelism to a conventional sequential code in a shared memory environment. 5987f296bb3SBarry Smith 5997f296bb3SBarry SmithPOSIX 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 6007f296bb3SBarry Smithsynchronizations between threads. Pthreads is rarely used directly in numerical libraries and applications. Sometimes OpenMP is implemented on top of pthreads. 6017f296bb3SBarry Smith 6027f296bb3SBarry SmithIf one adds 6037f296bb3SBarry SmithOpenMP parallelism to an MPI code, one must not over-subscribe the hardware resources. For example, if MPI already has one MPI process (rank) 6047f296bb3SBarry Smithper hardware core, then 6057f296bb3SBarry Smithusing four OpenMP threads per MPI process will slow the code down since now one core must switch back and forth between four OpenMP threads. 6067f296bb3SBarry Smith 6077f296bb3SBarry SmithFor application codes that use certain external packages, including BLAS/LAPACK, SuperLU_DIST, MUMPS, MKL, and SuiteSparse, one can build PETSc and these 6087f296bb3SBarry Smithpackages 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 6097f296bb3SBarry Smiththe 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 6107f296bb3SBarry Smithnumber of threads used. The default number is often absurdly high for the given hardware, so we recommend always setting it appropriately. 6117f296bb3SBarry Smith 6127f296bb3SBarry SmithUsers can also put OpenMP pragmas into their own code. However, since standard PETSc is not thread-safe, they should not, in general, 6137f296bb3SBarry Smithcall PETSc routines from inside the parallel regions. 6147f296bb3SBarry Smith 6157f296bb3SBarry SmithThere is an OpenMP thread-safe subset of PETSc that may be configured for using `--with-threadsafety` (often used along with `--with-openmp` or 6167f296bb3SBarry Smith`--download-concurrencykit`). <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex61f.F90.html">KSP Tutorial ex61f</a> demonstrates 6177f296bb3SBarry Smithhow this may be used with OpenMP. In this mode, one may have individual OpenMP threads that each manage their own 6187f296bb3SBarry Smith(sequential) PETSc objects (each thread can interact only with its own objects). This 6197f296bb3SBarry Smithis useful when one has many small systems (or sets of ODEs) that must be integrated in an 6207f296bb3SBarry Smith"embarrassingly parallel" fashion on multicore systems. 6217f296bb3SBarry Smith 6227f296bb3SBarry SmithThe ./configure option `--with-openmp-kernels` causes some PETSc numerical kernels to be compiled using OpenMP pragmas to take advantage of multiple cores. 6237f296bb3SBarry SmithOne 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 6247f296bb3SBarry Smithcores on the system; otherwise the code will slow down dramatically. 6257f296bb3SBarry Smith 6267f296bb3SBarry SmithPETSc's MPI-based linear solvers may be accessed from a sequential or non-MPI OpenMP program, see {any}`sec_pcmpi`. 6277f296bb3SBarry Smith 6287f296bb3SBarry Smith:::{seealso} 6297f296bb3SBarry SmithEdward 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) 6307f296bb3SBarry Smith10, 2006 6317f296bb3SBarry Smith::: 6327f296bb3SBarry Smith 6337f296bb3SBarry Smith(sec_gpu_kernels)= 6347f296bb3SBarry Smith 6357f296bb3SBarry Smith## GPU kernel parallelism 6367f296bb3SBarry Smith 6377f296bb3SBarry SmithGPUs offer at least two levels of clearly defined parallelism. Kernel-level parallelism is much like SIMD parallelism applied to loops; 6387f296bb3SBarry Smithmany "iterations" of the loop index run on different hardware in "lock-step". 6397f296bb3SBarry SmithPETSc utilizes this parallelism with three similar but slightly different models: 6407f296bb3SBarry Smith 6417f296bb3SBarry Smith- CUDA, which is provided by NVIDIA and runs on NVIDIA GPUs 6427f296bb3SBarry Smith- HIP, provided by AMD, which can, in theory, run on both AMD and NVIDIA GPUs 6437f296bb3SBarry Smith- and Kokkos, an open-source package that provides a slightly higher-level programming model to utilize GPU kernels. 6447f296bb3SBarry Smith 6457f296bb3SBarry SmithTo 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`. 6467f296bb3SBarry Smith 6477f296bb3SBarry SmithIn 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 6487f296bb3SBarry Smithmemory must be copied to the GPU (often, this copy is done automatically by the libraries, and the user does not need to manage it) 6497f296bb3SBarry Smithif 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 6507f296bb3SBarry Smithtranslates 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. 6517f296bb3SBarry Smith 6527f296bb3SBarry SmithPETSc utilizes GPUs by providing vector and matrix classes (Vec and Mat) specifically written to run on the GPU. However, since it is difficult to 6537f296bb3SBarry Smithwrite 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 6547f296bb3SBarry Smithon the CPU. The vector classes 6557f296bb3SBarry Smithare `VECCUDA`, `MATAIJCUSPARSE`, `VECKOKKOS`, `MATAIJKOKKOS`, and `VECHIP` (matrices are not yet supported by PETSc with HIP). 6567f296bb3SBarry Smith 6577f296bb3SBarry SmithMore details on using GPUs from PETSc will follow in this document. 6587f296bb3SBarry Smith 6597f296bb3SBarry Smith(sec_gpu_streams)= 6607f296bb3SBarry Smith 6617f296bb3SBarry Smith## GPU stream parallelism 6627f296bb3SBarry Smith 6637f296bb3SBarry SmithPlease contribute to this document. 6647f296bb3SBarry Smith 6657f296bb3SBarry Smith```{raw} latex 6667f296bb3SBarry Smith\newpage 6677f296bb3SBarry Smith``` 6687f296bb3SBarry Smith 6697f296bb3SBarry Smith# Compiling and Running Programs 6707f296bb3SBarry Smith 6717f296bb3SBarry SmithThe output below illustrates compiling and running a 6727f296bb3SBarry SmithPETSc program using MPICH on a macOS laptop. Note that different 6737f296bb3SBarry Smithmachines will have compilation commands as determined by the 6747f296bb3SBarry Smithconfiguration process. See {any}`sec_writing_application_codes` for 6757f296bb3SBarry Smitha discussion about how to compile your PETSc programs. Users who are 6767f296bb3SBarry Smithexperiencing difficulties linking PETSc programs should refer to the [FAQ](https://petsc.org/release/faq/). 6777f296bb3SBarry Smith 6787f296bb3SBarry Smith```none 6797f296bb3SBarry Smith$ cd $PETSC_DIR/src/ksp/ksp/tutorials 6807f296bb3SBarry Smith$ make ex2 6817f296bb3SBarry Smith/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 6827f296bb3SBarry Smith/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 6837f296bb3SBarry Smith/bin/rm -f ex2.o 6847f296bb3SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex2 6857f296bb3SBarry SmithNorm of error 0.000156044 iterations 6 6867f296bb3SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 2 ./ex2 6877f296bb3SBarry SmithNorm of error 0.000411674 iterations 7 6887f296bb3SBarry Smith``` 6897f296bb3SBarry Smith 6907f296bb3SBarry Smith(sec_profiling_programs)= 6917f296bb3SBarry Smith 6927f296bb3SBarry Smith# Profiling Programs 6937f296bb3SBarry Smith 6947f296bb3SBarry SmithThe option 6957f296bb3SBarry Smith`-log_view` activates printing of a performance summary, including 6967f296bb3SBarry Smithtimes, floating point operation (flop) rates, and message-passing 6977f296bb3SBarry Smithactivity. {any}`ch_profiling` provides details about 6987f296bb3SBarry Smithprofiling, including the interpretation of the output data below. 6997f296bb3SBarry SmithThis particular example involves 7007f296bb3SBarry Smiththe solution of a linear system on one processor using GMRES and ILU. 7017f296bb3SBarry SmithThe low floating point operation (flop) rates in this example are because the code solved a tiny system. We include this example 7027f296bb3SBarry Smithmerely to demonstrate the ease of extracting performance information. 7037f296bb3SBarry Smith 7047f296bb3SBarry Smith(listing_exprof)= 7057f296bb3SBarry Smith 7067f296bb3SBarry Smith```none 7077f296bb3SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petscmpiexec -n 1 ./ex1 -n 1000 -pc_type ilu -ksp_type gmres -ksp_rtol 1.e-7 -log_view 7087f296bb3SBarry Smith... 7097f296bb3SBarry Smith------------------------------------------------------------------------------------------------------------------------ 7107f296bb3SBarry SmithEvent Count Time (sec) Flops --- Global --- --- Stage ---- Total 7117f296bb3SBarry Smith Max Ratio Max Ratio Max Ratio Mess AvgLen Reduct %T %F %M %L %R %T %F %M %L %R Mflop/s 7127f296bb3SBarry Smith------------------------------------------------------------------------------------------------------------------------ 7137f296bb3SBarry Smith 7147f296bb3SBarry SmithVecMDot 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 7157f296bb3SBarry SmithVecNorm 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 7167f296bb3SBarry SmithVecScale 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 7177f296bb3SBarry SmithVecCopy 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 7187f296bb3SBarry SmithVecSet 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 7197f296bb3SBarry SmithVecAXPY 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 7207f296bb3SBarry SmithVecMAXPY 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 7217f296bb3SBarry SmithVecNormalize 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 7227f296bb3SBarry SmithMatMult 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 7237f296bb3SBarry SmithMatSolve 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 7247f296bb3SBarry SmithMatLUFactorNum 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 7257f296bb3SBarry SmithMatILUFactorSym 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 7267f296bb3SBarry SmithMatAssemblyBegin 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 7277f296bb3SBarry SmithMatAssemblyEnd 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 7287f296bb3SBarry SmithMatGetRowIJ 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 7297f296bb3SBarry SmithMatGetOrdering 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 7307f296bb3SBarry SmithMatView 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 7317f296bb3SBarry SmithKSPGMRESOrthog 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 7327f296bb3SBarry SmithKSPSetUp 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 7337f296bb3SBarry SmithKSPSolve 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 7347f296bb3SBarry SmithPCSetUp 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 7357f296bb3SBarry SmithPCApply 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 7367f296bb3SBarry Smith------------------------------------------------------------------------------------------------------------------------ 7377f296bb3SBarry Smith 7387f296bb3SBarry SmithMemory usage is given in bytes: 7397f296bb3SBarry Smith 7407f296bb3SBarry SmithObject Type Creations Destructions Memory Descendants' Mem. 7417f296bb3SBarry SmithReports information only for process 0. 7427f296bb3SBarry Smith 7437f296bb3SBarry Smith--- Event Stage 0: Main Stage 7447f296bb3SBarry Smith 7457f296bb3SBarry Smith Vector 8 8 76224 0. 7467f296bb3SBarry Smith Matrix 2 2 134212 0. 7477f296bb3SBarry Smith Krylov Solver 1 1 18400 0. 7487f296bb3SBarry Smith Preconditioner 1 1 1032 0. 7497f296bb3SBarry Smith Index Set 3 3 10328 0. 7507f296bb3SBarry Smith Viewer 1 0 0 0. 7517f296bb3SBarry Smith======================================================================================================================== 7527f296bb3SBarry Smith... 7537f296bb3SBarry Smith``` 7547f296bb3SBarry Smith 7557f296bb3SBarry Smith(sec_writing_application_codes)= 7567f296bb3SBarry Smith 7577f296bb3SBarry Smith# Writing C/C++ or Fortran Applications 7587f296bb3SBarry Smith 7597f296bb3SBarry SmithThe examples throughout the library demonstrate the software usage and 7607f296bb3SBarry Smithcan serve as templates for developing custom applications. We suggest 7617f296bb3SBarry Smiththat new PETSc users examine programs in the directories 7627f296bb3SBarry Smith`$PETSC_DIR/src/<library>/tutorials` where `<library>` denotes any 7637f296bb3SBarry Smithof the PETSc libraries (listed in the following section), such as 7647f296bb3SBarry Smith`SNES` or `KSP`, `TS`, or `TAO`. The manual pages at 7657f296bb3SBarry Smith<https://petsc.org/release/documentation/> provide links (organized by 7667f296bb3SBarry Smithroutine names and concepts) to the tutorial examples. 7677f296bb3SBarry Smith 7687f296bb3SBarry SmithTo develop an application program that uses PETSc, we suggest the following: 7697f296bb3SBarry Smith 7707f296bb3SBarry Smith- {ref}`Download <doc_download>` and {ref}`install <doc_install>` PETSc. 7717f296bb3SBarry Smith 7727f296bb3SBarry Smith- For completely new applications 7737f296bb3SBarry Smith 7747f296bb3SBarry Smith > 1. Make a directory for your source code: for example, `mkdir $HOME/application` 7757f296bb3SBarry Smith > 7767f296bb3SBarry Smith > 2. Change to that directory, for 7777f296bb3SBarry Smith > example, `cd $HOME/application` 7787f296bb3SBarry Smith > 7797f296bb3SBarry Smith > 3. Copy an example in the directory that corresponds to the 7807f296bb3SBarry Smith > problems of interest into your directory, for 7817f296bb3SBarry Smith > example, `cp $PETSC_DIR/src/snes/tutorials/ex19.c app.c` 7827f296bb3SBarry Smith > 7837f296bb3SBarry Smith > 4. Select an application build process. The `PETSC_DIR` (and `PETSC_ARCH` if the `--prefix=directoryname` 7847f296bb3SBarry Smith > option was not used when configuring PETSc) environmental variable(s) must be 7857f296bb3SBarry Smith > set for any of these approaches. 7867f296bb3SBarry Smith > 7877f296bb3SBarry Smith > - make (recommended). It uses the [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) tool 7887f296bb3SBarry Smith > and is the recommended approach. Copy \$PETSC_DIR/share/petsc/Makefile.user or \$PETSC_DIR/share/petsc/Makefile.basic.user 7897f296bb3SBarry Smith > to your directory, for example, `cp $PETSC_DIR/share/petsc/Makefile.user makefile` 7907f296bb3SBarry Smith > 7917f296bb3SBarry Smith > Examine the comments in this makefile. 7927f296bb3SBarry Smith > 7937f296bb3SBarry Smith > Makefile.user uses the [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) tool and is the recommended approach. 7947f296bb3SBarry Smith > 7957f296bb3SBarry Smith > Use `make app` to compile your program. 7967f296bb3SBarry Smith > 7977f296bb3SBarry Smith > - CMake. Copy \$PETSC_DIR/share/petsc/CMakeLists.txt to your directory, for example, `cp $PETSC_DIR/share/petsc/CMakeLists.txt CMakeLists.txt` 7987f296bb3SBarry Smith > 7997f296bb3SBarry Smith > Edit CMakeLists.txt, read the comments on usage, and change the name of the application from ex1 to your application executable name. 8007f296bb3SBarry Smith > 8017f296bb3SBarry Smith > 5. Run the program, for example, 8027f296bb3SBarry Smith > `./app` 8037f296bb3SBarry Smith > 8047f296bb3SBarry Smith > 6. Start to modify the program to develop your application. 8057f296bb3SBarry Smith 8067f296bb3SBarry Smith- For adding PETSc to an existing application 8077f296bb3SBarry Smith 8087f296bb3SBarry Smith > 1. Start with a working version of your code that you build and run to confirm that it works. 8097f296bb3SBarry Smith > 8107f296bb3SBarry Smith > 2. Upgrade your build process. The `PETSC_DIR` (and `PETSC_ARCH` if the `--prefix=directoryname` 8117f296bb3SBarry Smith > option was not used when configuring PETSc) environmental variable(s) must be 8127f296bb3SBarry Smith > set for any of these approaches. 8137f296bb3SBarry Smith > 8147f296bb3SBarry Smith > - Using make. Update the application makefile to add the appropriate PETSc include 8157f296bb3SBarry Smith > directories and libraries. 8167f296bb3SBarry Smith > 8177f296bb3SBarry Smith > - Recommended approach. Examine the comments in \$PETSC_DIR/share/petsc/Makefile.user and transfer selected portions of 8187f296bb3SBarry Smith > that file to your makefile. 8197f296bb3SBarry Smith > 8207f296bb3SBarry Smith > - Minimalist. Add the line 8217f296bb3SBarry Smith > 8227f296bb3SBarry Smith > ```console 8237f296bb3SBarry Smith > include ${PETSC_DIR}/lib/petsc/conf/variables 8247f296bb3SBarry Smith > ``` 8257f296bb3SBarry Smith > 8267f296bb3SBarry Smith > to the bottom of your makefile. This will provide a set of PETSc-specific make variables you may use in your makefile. See 8277f296bb3SBarry Smith > the comments in the file \$PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage. 8287f296bb3SBarry Smith > 8297f296bb3SBarry Smith > - Simple, but hands the build process over to PETSc's control. Add the lines 8307f296bb3SBarry Smith > 8317f296bb3SBarry Smith > ```console 8327f296bb3SBarry Smith > include ${PETSC_DIR}/lib/petsc/conf/variables 8337f296bb3SBarry Smith > include ${PETSC_DIR}/lib/petsc/conf/rules 8347f296bb3SBarry Smith > ``` 8357f296bb3SBarry Smith > 8367f296bb3SBarry Smith > to the bottom of your makefile. See the comments in the file \$PETSC_DIR/share/petsc/Makefile.basic.user for details on the usage. 8377f296bb3SBarry Smith > Since PETSc's rules now control the build process, you will likely need to simplify and remove much of the material that is in 8387f296bb3SBarry Smith > your makefile. 8397f296bb3SBarry Smith > 8407f296bb3SBarry Smith > - Not recommended since you must change your makefile for each new configuration/computing system. This approach does not require 8417f296bb3SBarry Smith > the environmental variable `PETSC_DIR` to be set when building your application since the information will be hardwired in your 8427f296bb3SBarry Smith > makefile. Run the following command in the PETSc root directory to get the information needed by your makefile: 8437f296bb3SBarry Smith > 8447f296bb3SBarry Smith > ```console 8457f296bb3SBarry Smith > $ make getlinklibs getincludedirs getcflags getcxxflags getfortranflags getccompiler getfortrancompiler getcxxcompiler 8467f296bb3SBarry Smith > ``` 8477f296bb3SBarry Smith > 8487f296bb3SBarry Smith > All the libraries listed need to be linked into your executable, and the 8497f296bb3SBarry Smith > include directories and flags need to be passed to the compiler(s). Usually, 8507f296bb3SBarry Smith > this is done by setting `LDFLAGS=<list of library flags and libraries>` and 8517f296bb3SBarry Smith > `CFLAGS=<list of -I and other flags>` and `FFLAGS=<list of -I and other flags>` etc in your makefile. 8527f296bb3SBarry Smith > 8537f296bb3SBarry Smith > - Using CMake. Update the application CMakeLists.txt by examining the code and comments in 8547f296bb3SBarry Smith > \$PETSC_DIR/share/petsc/CMakeLists.txt 8557f296bb3SBarry Smith > 8567f296bb3SBarry Smith > 3. Rebuild your application and ensure it still runs correctly. 8577f296bb3SBarry Smith > 8587f296bb3SBarry Smith > 4. Add a `PetscInitialize()` near the beginning of your code and `PetscFinalize()` near the end with appropriate include commands 8597f296bb3SBarry Smith > (and use statements in Fortran). 8607f296bb3SBarry Smith > 8617f296bb3SBarry Smith > 5. Rebuild your application and ensure it still runs correctly. 8627f296bb3SBarry Smith > 8637f296bb3SBarry Smith > 6. Slowly start utilizing PETSc functionality in your code, and ensure that your code continues to build and run correctly. 8647f296bb3SBarry Smith 8657f296bb3SBarry Smith(sec_oo)= 8667f296bb3SBarry Smith 8677f296bb3SBarry Smith# PETSc's Object-Oriented Design 8687f296bb3SBarry Smith 8697f296bb3SBarry SmithThough PETSc has a large API, conceptually, it's rather simple. 8707f296bb3SBarry SmithThere are three abstract basic data objects (classes): index sets, `IS`, vectors, `Vec`, and matrices, `Mat`. 8717f296bb3SBarry SmithPlus, a larger number of abstract algorithm objects (classes) starting with: preconditioners, `PC`, Krylov solvers, `KSP`, and so forth. 8727f296bb3SBarry Smith 8737f296bb3SBarry SmithLet `Object` 8747f296bb3SBarry Smithrepresent any of these objects. Objects are created with 8757f296bb3SBarry Smith 8767f296bb3SBarry Smith``` 8777f296bb3SBarry SmithObject obj; 8787f296bb3SBarry SmithObjectCreate(MPI_Comm, &obj); 8797f296bb3SBarry Smith``` 8807f296bb3SBarry Smith 8817f296bb3SBarry SmithThe 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 8827f296bb3SBarry Smithis merely a string name of an implementation class using 8837f296bb3SBarry Smith 8847f296bb3SBarry Smith``` 8857f296bb3SBarry SmithObjectSetType(obj,"ImplementationName"); 8867f296bb3SBarry Smith``` 8877f296bb3SBarry Smith 8887f296bb3SBarry SmithSome objects support subclasses, which are specializations of the type. These are set with 8897f296bb3SBarry Smith 8907f296bb3SBarry Smith``` 8917f296bb3SBarry SmithObjectNameSetType(obj,"ImplementationSubName"); 8927f296bb3SBarry Smith``` 8937f296bb3SBarry Smith 8947f296bb3SBarry SmithFor example, within `TS` one may do 8957f296bb3SBarry Smith 8967f296bb3SBarry Smith``` 8977f296bb3SBarry SmithTS ts; 8987f296bb3SBarry SmithTSCreate(PETSC_COMM_WORLD,&ts); 8997f296bb3SBarry SmithTSSetType(ts,TSARKIMEX); 9007f296bb3SBarry SmithTSARKIMEXSetType(ts,TSARKIMEX3); 9017f296bb3SBarry Smith``` 9027f296bb3SBarry Smith 9037f296bb3SBarry SmithThe abstract class `TS` can embody any ODE/DAE integrator scheme. 9047f296bb3SBarry SmithThis 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, 9057f296bb3SBarry Smithwhose subtype name is `TSARKIMEX3`. 9067f296bb3SBarry Smith 9077f296bb3SBarry SmithTo allow PETSc objects to be runtime configurable, PETSc objects provide a universal way of selecting types (classes) and subtypes at runtime from 9087f296bb3SBarry Smithwhat is referred to as the PETSc "options database". The code above can be replaced with 9097f296bb3SBarry Smith 9107f296bb3SBarry Smith``` 9117f296bb3SBarry SmithTS obj; 9127f296bb3SBarry SmithTSCreate(PETSC_COMM_WORLD,&obj); 9137f296bb3SBarry SmithTSSetFromOptions(obj); 9147f296bb3SBarry Smith``` 9157f296bb3SBarry Smith 9167f296bb3SBarry Smithnow, both the type and subtype can be conveniently set from the command line 9177f296bb3SBarry Smith 9187f296bb3SBarry Smith```console 9197f296bb3SBarry Smith$ ./app -ts_type arkimex -ts_arkimex_type 3 9207f296bb3SBarry Smith``` 9217f296bb3SBarry Smith 9227f296bb3SBarry SmithThe 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: 9237f296bb3SBarry Smith 9247f296bb3SBarry Smith``` 9257f296bb3SBarry Smith// (if set) command line options "override" TSSetType() 9267f296bb3SBarry SmithTSSetType(ts, TSGLLE); 9277f296bb3SBarry SmithTSSetFromOptions(ts); 9287f296bb3SBarry Smith 9297f296bb3SBarry Smith// TSSetType() overrides command line options 9307f296bb3SBarry SmithTSSetFromOptions(ts); 9317f296bb3SBarry SmithTSSetType(ts, TSGLLE); 9327f296bb3SBarry Smith``` 9337f296bb3SBarry Smith 9347f296bb3SBarry SmithSince 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. 9357f296bb3SBarry Smith 9367f296bb3SBarry SmithThe standard methods on an object are of the general form. 9377f296bb3SBarry Smith 9387f296bb3SBarry Smith``` 9397f296bb3SBarry SmithObjectSetXXX(obj,...); 9407f296bb3SBarry SmithObjectGetXXX(obj,...); 9417f296bb3SBarry SmithObjectYYY(obj,...); 9427f296bb3SBarry Smith``` 9437f296bb3SBarry Smith 9447f296bb3SBarry SmithFor example 9457f296bb3SBarry Smith 9467f296bb3SBarry Smith``` 9477f296bb3SBarry SmithTSSetRHSFunction(obj,...) 9487f296bb3SBarry Smith``` 9497f296bb3SBarry Smith 9507f296bb3SBarry SmithParticular types and subtypes of objects may have their own methods, which are given in the form 9517f296bb3SBarry Smith 9527f296bb3SBarry Smith``` 9537f296bb3SBarry SmithObjectNameSetXXX(obj,...); 9547f296bb3SBarry SmithObjectNameGetXXX(obj,...); 9557f296bb3SBarry SmithObjectNameYYY(obj,...); 9567f296bb3SBarry Smith``` 9577f296bb3SBarry Smith 9587f296bb3SBarry Smithand 9597f296bb3SBarry Smith 9607f296bb3SBarry Smith``` 9617f296bb3SBarry SmithObjectNameSubNameSetXXX(obj,...); 9627f296bb3SBarry SmithObjectNameSubNameGetXXX(obj,...); 9637f296bb3SBarry SmithObjectNameSubNameYYY(obj,...); 9647f296bb3SBarry Smith``` 9657f296bb3SBarry Smith 9667f296bb3SBarry Smithwhere 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 9677f296bb3SBarry Smithnames in lower case, separated by underscores, and with the word "set" removed. For example, 9687f296bb3SBarry Smith 9697f296bb3SBarry Smith``` 9707f296bb3SBarry SmithKSPGMRESSetRestart(obj,30); 9717f296bb3SBarry Smith``` 9727f296bb3SBarry Smith 9737f296bb3SBarry Smithcan be set at the command line with 9747f296bb3SBarry Smith 9757f296bb3SBarry Smith```console 9767f296bb3SBarry Smith$ ./app -ksp_gmres_restart 30 9777f296bb3SBarry Smith``` 9787f296bb3SBarry Smith 9797f296bb3SBarry SmithA 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. 9807f296bb3SBarry SmithNote 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, 9817f296bb3SBarry Smith 9827f296bb3SBarry Smith``` 9837f296bb3SBarry SmithKSPGMRESSetRestart(obj,30); // ignored if the type is not KSPGMRES 9847f296bb3SBarry Smith``` 9857f296bb3SBarry Smith 9867f296bb3SBarry SmithThese 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 9877f296bb3SBarry Smith 9887f296bb3SBarry Smith``` 9897f296bb3SBarry Smithif (type == KSPGMRES) { // unneeded clutter 9907f296bb3SBarry Smith KSPGMRESSetRestart(obj,30); 9917f296bb3SBarry Smith} 9927f296bb3SBarry Smith``` 9937f296bb3SBarry Smith 9947f296bb3SBarry SmithMany "get" routines give one temporary access to an object's internal data. They are used in the style 9957f296bb3SBarry Smith 9967f296bb3SBarry Smith``` 9977f296bb3SBarry SmithXXX xxx; 9987f296bb3SBarry SmithObjectGetXXX(obj,&xxx); 9997f296bb3SBarry Smith// use xxx 10007f296bb3SBarry SmithObjectRestoreXXX(obj,&xxx); 10017f296bb3SBarry Smith``` 10027f296bb3SBarry Smith 10037f296bb3SBarry SmithObjects 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 10047f296bb3SBarry Smithwith a "destroy" routine. 10057f296bb3SBarry Smith 10067f296bb3SBarry SmithThere may be variants of the "get" routines that give more limited access to the obtained object. For example, 10077f296bb3SBarry Smith 10087f296bb3SBarry Smith``` 10097f296bb3SBarry Smithconst PetscScalar *x; 10107f296bb3SBarry Smith 10117f296bb3SBarry Smith// specialized variant of VecGetArray() 10127f296bb3SBarry SmithVecGetArrayRead(vec, &x); 10137f296bb3SBarry Smith// one can read but not write with x[] 10147f296bb3SBarry SmithPetscReal y = 2*x[0]; 10157f296bb3SBarry Smith// don't forget to restore x after you are done with it 10167f296bb3SBarry SmithVecRestoreArrayRead(vec, &x); 10177f296bb3SBarry Smith``` 10187f296bb3SBarry Smith 10197f296bb3SBarry SmithObjects can be displayed (in a large number of ways) with 10207f296bb3SBarry Smith 10217f296bb3SBarry Smith``` 10227f296bb3SBarry SmithObjectView(obj,PetscViewer viewer); 10237f296bb3SBarry SmithObjectViewFromOptions(obj,...); 10247f296bb3SBarry Smith``` 10257f296bb3SBarry Smith 10267f296bb3SBarry SmithWhere `PetscViewer` is an abstract object that can represent standard output, an ASCII or binary file, a graphical window, etc. The second 10277f296bb3SBarry Smithvariant 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. 10287f296bb3SBarry Smith 10297f296bb3SBarry SmithObjects are destroyed with 10307f296bb3SBarry Smith 10317f296bb3SBarry Smith``` 10327f296bb3SBarry SmithObjectDestroy(&obj) 10337f296bb3SBarry Smith``` 10347f296bb3SBarry Smith 10357f296bb3SBarry Smith:::{figure} /images/manual/objectlife.svg 10367f296bb3SBarry Smith:name: fig_objectlife 10377f296bb3SBarry Smith 10387f296bb3SBarry SmithSample lifetime of a PETSc object 10397f296bb3SBarry Smith::: 10407f296bb3SBarry Smith 10417f296bb3SBarry Smith## User Callbacks 10427f296bb3SBarry Smith 10437f296bb3SBarry SmithThe 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: 10447f296bb3SBarry Smith 10457f296bb3SBarry Smith``` 1046*2a8381b2SBarry SmithObjectCallbackSetter(obj, callbackfunction(), PetscCtx ctx, contextdestroy(PetscCtx ctx)); 10477f296bb3SBarry Smith``` 10487f296bb3SBarry Smith 10497f296bb3SBarry Smithwhere `ObjectCallbackSetter()` is a callback setter such as `SNESSetFunction()`. `callbackfunction()` is what will be called 10507f296bb3SBarry Smithby the library, `ctx` is an optional data structure (array, struct, PETSc object) that is used by `callbackfunction()` 1051*2a8381b2SBarry Smithand `contextdestroy(PetscCtx ctx)` is an optional function that will be called when `obj` is destroyed to free 10527f296bb3SBarry Smithanything in `ctx`. The use of the `contextdestroy()` allows users to "set and forget" 10537f296bb3SBarry Smithdata 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 10547f296bb3SBarry Smith 10557f296bb3SBarry Smith``` 10567f296bb3SBarry SmithTS ts; 10577f296bb3SBarry SmithTSMonitorLGCtx *ctx; 10587f296bb3SBarry Smith 10597f296bb3SBarry SmithTSMonitorLGCtxCreate(..., &ctx) 10607f296bb3SBarry SmithTSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy); 10617f296bb3SBarry SmithTSSolve(ts); 10627f296bb3SBarry Smith``` 10637f296bb3SBarry Smith 10647f296bb3SBarry SmithOccasionally, 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, 10657f296bb3SBarry Smith 10667f296bb3SBarry Smith``` 10677f296bb3SBarry SmithSNES obj; 10687f296bb3SBarry SmithVec r; 1069*2a8381b2SBarry SmithPetscCtx ctx; 10707f296bb3SBarry Smith 1071*2a8381b2SBarry SmithSNESSetFunction(snes, r, UserApplyFunction(SNES, Vec, Vec, PetscCtx ctx), ctx); 10727f296bb3SBarry Smith``` 10737f296bb3SBarry Smith 10747f296bb3SBarry SmithThe `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 10757f296bb3SBarry Smithto 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 10767f296bb3SBarry SmithPETSc accepts callback functions in different places in the code. 10777f296bb3SBarry Smith 10787f296bb3SBarry SmithSee {any}`fig_taocallbacks` for a cartoon on callbacks in `Tao`. 10797f296bb3SBarry Smith 10807f296bb3SBarry Smith(sec_directory)= 10817f296bb3SBarry Smith 10827f296bb3SBarry Smith# Directory Structure 10837f296bb3SBarry Smith 10847f296bb3SBarry SmithWe conclude this introduction with an overview of the organization of 10857f296bb3SBarry Smiththe PETSc software. The root directory of PETSc contains the following 10867f296bb3SBarry Smithdirectories: 10877f296bb3SBarry Smith 10887f296bb3SBarry Smith- `doc` The source code and Python scripts for building the website and documentation 10897f296bb3SBarry Smith 10907f296bb3SBarry Smith- `lib/petsc/conf` - Base PETSc configuration files that define the standard 10917f296bb3SBarry Smith make variables and rules used by PETSc 10927f296bb3SBarry Smith 10937f296bb3SBarry Smith- `include` - All include files for PETSc that are visible to the 10947f296bb3SBarry Smith user. 10957f296bb3SBarry Smith 10967f296bb3SBarry Smith- `include/petsc/finclude` - PETSc Fortran include files. 10977f296bb3SBarry Smith 10987f296bb3SBarry Smith- `include/petsc/private` - Private PETSc include files that should 10997f296bb3SBarry Smith *not* need to be used by application programmers. 11007f296bb3SBarry Smith 11017f296bb3SBarry Smith- `share` - Some small test matrices and other data files 11027f296bb3SBarry Smith 11037f296bb3SBarry Smith- `src` - The source code for all PETSc libraries, which currently 11047f296bb3SBarry Smith includes 11057f296bb3SBarry Smith 11067f296bb3SBarry Smith - `vec` - vectors, 11077f296bb3SBarry Smith 11087f296bb3SBarry Smith - `is` - index sets, 11097f296bb3SBarry Smith 11107f296bb3SBarry Smith - `mat` - matrices, 11117f296bb3SBarry Smith 11127f296bb3SBarry Smith - `ksp` - complete linear equations solvers, 11137f296bb3SBarry Smith 11147f296bb3SBarry Smith - `ksp` - Krylov subspace accelerators, 11157f296bb3SBarry Smith - `pc` - preconditioners, 11167f296bb3SBarry Smith 11177f296bb3SBarry Smith - `snes` - nonlinear solvers 11187f296bb3SBarry Smith 11197f296bb3SBarry Smith - `ts` - ODE/DAE solvers and timestepping, 11207f296bb3SBarry Smith 11217f296bb3SBarry Smith - `tao` - optimizers, 11227f296bb3SBarry Smith 112334b254c5SRichard Tran Mills - `ml` - Machine Learning 112434b254c5SRichard Tran Mills 112534b254c5SRichard Tran Mills - `regressor` - Regression solvers 112634b254c5SRichard Tran Mills 11277f296bb3SBarry Smith - `dm` - data management between meshes and solvers, vectors, and 11287f296bb3SBarry Smith matrices, 11297f296bb3SBarry Smith 11307f296bb3SBarry Smith - `sys` - general system-related routines, 11317f296bb3SBarry Smith 11327f296bb3SBarry Smith - `logging` - PETSc logging and profiling routines, 11337f296bb3SBarry Smith 11347f296bb3SBarry Smith - `classes` - low-level classes 11357f296bb3SBarry Smith 11367f296bb3SBarry Smith - `draw` - simple graphics, 11377f296bb3SBarry Smith - `viewer` - a mechanism for printing and visualizing PETSc 11387f296bb3SBarry Smith objects, 11397f296bb3SBarry Smith - `bag` - mechanism for saving and loading from disk user 11407f296bb3SBarry Smith data stored in C structs. 11417f296bb3SBarry Smith - `random` - random number generators. 11427f296bb3SBarry Smith 11437f296bb3SBarry SmithEach PETSc source code library directory has the following subdirectories: 11447f296bb3SBarry Smith 11457f296bb3SBarry Smith- `tutorials` - Programs designed to teach users about PETSc. 11467f296bb3SBarry Smith These codes can serve as templates for applications. 11477f296bb3SBarry Smith- `tests` - Programs designed for thorough testing of PETSc. As 11487f296bb3SBarry Smith such, these codes are not intended for examination by users. 11497f296bb3SBarry Smith- `interface` - Provides the abstract base classes for the objects. 11507f296bb3SBarry Smith The code here does not know about particular implementations and does not perform 11517f296bb3SBarry Smith operations on the underlying numerical data. 11527f296bb3SBarry Smith- `impls` - Source code for one or more implementations of the class for particular 11537f296bb3SBarry Smith data structures or algorithms. 11547f296bb3SBarry Smith- `utils` - Utility routines. The source here may know about the 11557f296bb3SBarry Smith implementations, but ideally, will not know about implementations for 11567f296bb3SBarry Smith other components. 11577f296bb3SBarry Smith 11587f296bb3SBarry Smith```{rubric} Footnotes 11597f296bb3SBarry Smith``` 11607f296bb3SBarry Smith 11617f296bb3SBarry Smith[^debug-footnote]: Configure PETSc with `--with-debugging`. 11627f296bb3SBarry Smith 11637f296bb3SBarry Smith```{eval-rst} 11647f296bb3SBarry Smith.. bibliography:: /petsc.bib 11657f296bb3SBarry Smith :filter: docname in docnames 11667f296bb3SBarry Smith``` 1167