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