1*4bcd95a3SBarry Smith# The PETSc Kernel 2*4bcd95a3SBarry Smith 3*4bcd95a3SBarry SmithPETSc provides a variety of basic services for writing scalable, 4*4bcd95a3SBarry Smithcomponent-based libraries; these are referred to as the PETSc kernel {cite}`bgms98`. 5*4bcd95a3SBarry SmithThe source code for the kernel is in `src/sys`. It contains systematic 6*4bcd95a3SBarry Smithsupport for 7*4bcd95a3SBarry Smith 8*4bcd95a3SBarry Smith- managing PETSc types, 9*4bcd95a3SBarry Smith- error handling, 10*4bcd95a3SBarry Smith- memory management, 11*4bcd95a3SBarry Smith- profiling, 12*4bcd95a3SBarry Smith- object management, 13*4bcd95a3SBarry Smith- Fortran interfaces (see {cite}`balaybrownknepleymcinnessmith2015`) 14*4bcd95a3SBarry Smith- mechanism for generating appropriate citations for algorithms and software used in PETSc (see {cite}`knepley2013accurately`) 15*4bcd95a3SBarry Smith- file I/O, 16*4bcd95a3SBarry Smith- an options database, and 17*4bcd95a3SBarry Smith- objects and code for viewing, drawing, and displaying data and solver objects. 18*4bcd95a3SBarry Smith 19*4bcd95a3SBarry SmithEach of these is discussed in a section below. 20*4bcd95a3SBarry Smith 21*4bcd95a3SBarry Smith## PETSc Types 22*4bcd95a3SBarry Smith 23*4bcd95a3SBarry SmithFor maximum flexibility, the basic data types `int`, `double`, and 24*4bcd95a3SBarry Smithso on are not used in PETSc source code. Rather, it has 25*4bcd95a3SBarry Smith 26*4bcd95a3SBarry Smith- `PetscScalar`, 27*4bcd95a3SBarry Smith- `PetscInt`, 28*4bcd95a3SBarry Smith- `PetscMPIInt`, 29*4bcd95a3SBarry Smith- `PetscBLASInt`, 30*4bcd95a3SBarry Smith- `PetscBool`, and 31*4bcd95a3SBarry Smith- `PetscBT` - bit storage of logical true and false. 32*4bcd95a3SBarry Smith 33*4bcd95a3SBarry Smith`PetscInt` can be set using `configure` to be either `int` (32 34*4bcd95a3SBarry Smithbit, the default) or `long long` (64-bit, with 35*4bcd95a3SBarry Smith`configure –with-64-bit-indices`) to allow indexing into very large 36*4bcd95a3SBarry Smitharrays. `PetscMPIInt` is used for integers passed to MPI as counts and 37*4bcd95a3SBarry Smithsizes. These are always `int` since that is what the MPI standard 38*4bcd95a3SBarry Smithuses. Similarly, `PetscBLASInt` is for counts, and so on passed to 39*4bcd95a3SBarry SmithBLAS and LAPACK routines. These are almost always `int` unless one is 40*4bcd95a3SBarry Smithusing a special “64-bit integer” BLAS/LAPACK (this is available, for 41*4bcd95a3SBarry Smithexample, with Intel’s MKL and OpenBLAS). 42*4bcd95a3SBarry Smith 43*4bcd95a3SBarry SmithIn addition, there are special types: 44*4bcd95a3SBarry Smith 45*4bcd95a3SBarry Smith- `PetscClassId` 46*4bcd95a3SBarry Smith- `PetscErrorCode` 47*4bcd95a3SBarry Smith- `PetscLogEvent` 48*4bcd95a3SBarry Smith 49*4bcd95a3SBarry SmithThese are currently always `int`, but their use clarifies the code. 50*4bcd95a3SBarry Smith 51*4bcd95a3SBarry Smith## Implementation of Error Handling 52*4bcd95a3SBarry Smith 53*4bcd95a3SBarry SmithPETSc uses a “call error handler; then (depending on result) return 54*4bcd95a3SBarry Smitherror code” model when problems are detected in the running code. The 55*4bcd95a3SBarry Smithpublic include file for error handling is 56*4bcd95a3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscerror.h.html">include/petscerror.h</a> 57*4bcd95a3SBarry Smithand the source code for the PETSc error handling is in 58*4bcd95a3SBarry Smith`src/sys/error/`. 59*4bcd95a3SBarry Smith 60*4bcd95a3SBarry Smith### Simplified Interface 61*4bcd95a3SBarry Smith 62*4bcd95a3SBarry SmithThe simplified macro-based interface consists of the following two 63*4bcd95a3SBarry Smithcalls: 64*4bcd95a3SBarry Smith 65*4bcd95a3SBarry Smith- `SETERRQ(comm,error code,Error message);` 66*4bcd95a3SBarry Smith- `PetscCall(ierr);` 67*4bcd95a3SBarry Smith 68*4bcd95a3SBarry SmithThe macro `SETERRQ()` is given by 69*4bcd95a3SBarry Smith 70*4bcd95a3SBarry Smith```{literalinclude} /../include/petscerror.h 71*4bcd95a3SBarry Smith:end-at: '#define SETERRQ' 72*4bcd95a3SBarry Smith:language: c 73*4bcd95a3SBarry Smith:start-at: '#define SETERRQ' 74*4bcd95a3SBarry Smith``` 75*4bcd95a3SBarry Smith 76*4bcd95a3SBarry SmithIt calls the error handler with the current function name and location: 77*4bcd95a3SBarry Smithline number, and file, plus an error code and an error message. Normally 78*4bcd95a3SBarry Smith`comm` is `PETSC_COMM_SELF`; it can be another communicator only if 79*4bcd95a3SBarry Smithone is absolutely sure the same error will be generated on all processes 80*4bcd95a3SBarry Smithin the communicator. This feature is to prevent the same error message 81*4bcd95a3SBarry Smithfrom being printed by many processes. 82*4bcd95a3SBarry Smith 83*4bcd95a3SBarry SmithThe macro `PetscCall()` is defined by 84*4bcd95a3SBarry Smith 85*4bcd95a3SBarry Smith```{literalinclude} /../include/petscerror.h 86*4bcd95a3SBarry Smith:end-at: '#define PetscCall' 87*4bcd95a3SBarry Smith:language: c 88*4bcd95a3SBarry Smith:start-at: '#define PetscCall' 89*4bcd95a3SBarry Smith``` 90*4bcd95a3SBarry Smith 91*4bcd95a3SBarry SmithThe message passed to `SETERRQ()` is treated as a `printf()`-style 92*4bcd95a3SBarry Smithformat string, with all additional parameters passed after the string as 93*4bcd95a3SBarry Smithits arguments. For example: 94*4bcd95a3SBarry Smith 95*4bcd95a3SBarry Smith``` 96*4bcd95a3SBarry SmithSETERRQ(comm,PETSC_ERR,"Iteration overflow: its %" PetscInt_FMT " norm %g",its,(double)norm); 97*4bcd95a3SBarry Smith``` 98*4bcd95a3SBarry Smith 99*4bcd95a3SBarry Smith### Error Handlers 100*4bcd95a3SBarry Smith 101*4bcd95a3SBarry SmithThe error-handling function `PetscError()` calls the “current” error 102*4bcd95a3SBarry Smithhandler with the code 103*4bcd95a3SBarry Smith 104*4bcd95a3SBarry Smith```{literalinclude} /../src/sys/error/err.c 105*4bcd95a3SBarry Smith:append: '}' 106*4bcd95a3SBarry Smith:end-at: PetscFunctionReturn 107*4bcd95a3SBarry Smith:language: c 108*4bcd95a3SBarry Smith:start-at: PetscErrorCode PetscError( 109*4bcd95a3SBarry Smith``` 110*4bcd95a3SBarry Smith 111*4bcd95a3SBarry SmithYou can set a new error handler with the command 112*4bcd95a3SBarry Smith`PetscPushErrorHandler()`, which maintains a linked list of error 113*4bcd95a3SBarry Smithhandlers. The most recent error handler is removed via 114*4bcd95a3SBarry Smith`PetscPopErrorHandler()`. 115*4bcd95a3SBarry Smith 116*4bcd95a3SBarry SmithPETSc provides several default error handlers: 117*4bcd95a3SBarry Smith 118*4bcd95a3SBarry Smith- `PetscTraceBackErrorHandler()`, the default; 119*4bcd95a3SBarry Smith- `PetscAbortErrorHandler()`, called with `-onerrorabort`, useful when running in the debugger; 120*4bcd95a3SBarry Smith- `PetscReturnErrorHandler()`, which returns up the stack without printing error messages; 121*4bcd95a3SBarry Smith- `PetscEmacsClientErrorHandler()`; 122*4bcd95a3SBarry Smith- `PetscMPIAbortErrorHandler()`, which calls `MPIAbort()` after printing the error message; and 123*4bcd95a3SBarry Smith- `PetscAttachDebuggerErrorHandler()`, called with `-onerrorattachdebugger`. 124*4bcd95a3SBarry Smith 125*4bcd95a3SBarry Smith### Error Codes 126*4bcd95a3SBarry Smith 127*4bcd95a3SBarry SmithThe PETSc error handler takes an error code. The generic error codes are 128*4bcd95a3SBarry Smithdefined in 129*4bcd95a3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscerror.h.html">include/petscerror.h</a> 130*4bcd95a3SBarry SmithThe same error code is used many times in the libraries. For example, 131*4bcd95a3SBarry Smiththe error code `PETSCERRMEM` is used whenever a requested memory 132*4bcd95a3SBarry Smithallocation is not available. 133*4bcd95a3SBarry Smith 134*4bcd95a3SBarry Smith### Detailed Error Messages 135*4bcd95a3SBarry Smith 136*4bcd95a3SBarry SmithIn a modern parallel component-oriented application code, it does not 137*4bcd95a3SBarry Smithalways make sense to simply print error messages to the terminal (and 138*4bcd95a3SBarry Smithmore than likely there is no “terminal”, for example, with Microsoft 139*4bcd95a3SBarry SmithWindows or Apple iPad applications). PETSc provides the replaceable 140*4bcd95a3SBarry Smithfunction pointer 141*4bcd95a3SBarry Smith 142*4bcd95a3SBarry Smith``` 143*4bcd95a3SBarry Smith(*PetscErrorPrintf)("Format",...); 144*4bcd95a3SBarry Smith``` 145*4bcd95a3SBarry Smith 146*4bcd95a3SBarry Smithwhich, by default, prints to standard out. Thus, error messages should 147*4bcd95a3SBarry Smithnot be printed with `printf()` or `fprintf()`. Rather, they should 148*4bcd95a3SBarry Smithbe printed with `(*PetscErrorPrintf)()`. You can direct all error 149*4bcd95a3SBarry Smithmessages to `stderr`, instead of the default `stdout`, with the 150*4bcd95a3SBarry Smithcommand line option `-erroroutputstderr`. 151*4bcd95a3SBarry Smith 152*4bcd95a3SBarry Smith### C++ Exceptions 153*4bcd95a3SBarry Smith 154*4bcd95a3SBarry SmithIn PETSc code, when one calls C++ functions that do not return with an error code but might 155*4bcd95a3SBarry Smithinstead throw C++ exceptions, one can use `CHKERRCXX(func)`, which catches the exceptions 156*4bcd95a3SBarry Smithin *func* and then calls `SETERRQ()`. The macro `CHKERRCXX(func)` is given by 157*4bcd95a3SBarry Smith 158*4bcd95a3SBarry Smith```{literalinclude} /../include/petscerror.h 159*4bcd95a3SBarry Smith:end-at: '#define CHKERRCXX' 160*4bcd95a3SBarry Smith:language: c 161*4bcd95a3SBarry Smith:start-at: '#define CHKERRCXX' 162*4bcd95a3SBarry Smith``` 163*4bcd95a3SBarry Smith 164*4bcd95a3SBarry Smith## Memory Management 165*4bcd95a3SBarry Smith 166*4bcd95a3SBarry SmithPETSc provides simple wrappers for the system `malloc(), calloc()`, 167*4bcd95a3SBarry Smithand `free()` routines. The public interface for these is provided in 168*4bcd95a3SBarry Smith`petscsys.h`, while the implementation code is in `src/sys/memory`. 169*4bcd95a3SBarry SmithThe most basic interfaces are 170*4bcd95a3SBarry Smith 171*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 172*4bcd95a3SBarry Smith:end-at: '#define PetscMalloc' 173*4bcd95a3SBarry Smith:language: c 174*4bcd95a3SBarry Smith:start-at: '#define PetscMalloc' 175*4bcd95a3SBarry Smith``` 176*4bcd95a3SBarry Smith 177*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 178*4bcd95a3SBarry Smith:end-at: '#define PetscFree' 179*4bcd95a3SBarry Smith:language: c 180*4bcd95a3SBarry Smith:start-at: '#define PetscFree' 181*4bcd95a3SBarry Smith``` 182*4bcd95a3SBarry Smith 183*4bcd95a3SBarry Smith```{literalinclude} /../src/sys/memory/mal.c 184*4bcd95a3SBarry Smith:append: '}' 185*4bcd95a3SBarry Smith:end-at: PetscFunctionReturn(PETSC_SUCCESS) 186*4bcd95a3SBarry Smith:language: c 187*4bcd95a3SBarry Smith:start-at: PetscErrorCode PetscMallocA( 188*4bcd95a3SBarry Smith``` 189*4bcd95a3SBarry Smith 190*4bcd95a3SBarry Smith```{literalinclude} /../src/sys/memory/mal.c 191*4bcd95a3SBarry Smith:append: '}' 192*4bcd95a3SBarry Smith:end-at: PetscFunctionReturn(PETSC_SUCCESS) 193*4bcd95a3SBarry Smith:language: c 194*4bcd95a3SBarry Smith:start-at: PetscErrorCode PetscFreeA( 195*4bcd95a3SBarry Smith``` 196*4bcd95a3SBarry Smith 197*4bcd95a3SBarry Smithwhich allow the use of any number of profiling and error-checking 198*4bcd95a3SBarry Smithwrappers for `malloc(), calloc()`, and `free()`. Both 199*4bcd95a3SBarry Smith`PetscMallocA()` and `PetscFreeA()` call the function pointer values 200*4bcd95a3SBarry Smith`(*PetscTrMalloc)` and `(*PetscTrFree)`. `PetscMallocSet()` is 201*4bcd95a3SBarry Smithused to set these function pointers. The functions are guaranteed to 202*4bcd95a3SBarry Smithsupport requests for zero bytes of memory correctly. Freeing memory 203*4bcd95a3SBarry Smithlocations also sets the pointer value to zero, preventing later code 204*4bcd95a3SBarry Smithfrom accidentally using memory that has been freed. All PETSc memory 205*4bcd95a3SBarry Smithallocation calls are memory aligned on at least double-precision 206*4bcd95a3SBarry Smithboundaries; the macro generated by configure `PETSCMEMALIGN` indicates 207*4bcd95a3SBarry Smithin bytes what alignment all allocations have. This can be controlled at 208*4bcd95a3SBarry Smithconfigure time with the option `-with-memalign=<4,8,16,32,64>`. 209*4bcd95a3SBarry Smith 210*4bcd95a3SBarry Smith`PetscMallocA()` supports a request for up to 7 distinct memory 211*4bcd95a3SBarry Smithlocations of possibly different types. This serves two purposes: it 212*4bcd95a3SBarry Smithreduces the number of system `malloc()` calls, thus potentially 213*4bcd95a3SBarry Smithincreasing performance, and it clarifies in the code related memory 214*4bcd95a3SBarry Smithallocations that should be freed together. 215*4bcd95a3SBarry Smith 216*4bcd95a3SBarry SmithThe following macros are the preferred way to obtain and release memory 217*4bcd95a3SBarry Smithin the PETSc source code. They automatically manage calling 218*4bcd95a3SBarry Smith`PetscMallocA()` and `PetscFreeA()` with the appropriate location 219*4bcd95a3SBarry Smithinformation. 220*4bcd95a3SBarry Smith 221*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 222*4bcd95a3SBarry Smith:end-at: '#define PetscMalloc1' 223*4bcd95a3SBarry Smith:language: c 224*4bcd95a3SBarry Smith:start-at: '#define PetscMalloc1' 225*4bcd95a3SBarry Smith``` 226*4bcd95a3SBarry Smith 227*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 228*4bcd95a3SBarry Smith:end-at: '#define PetscMalloc2' 229*4bcd95a3SBarry Smith:language: c 230*4bcd95a3SBarry Smith:start-at: '#define PetscMalloc2' 231*4bcd95a3SBarry Smith``` 232*4bcd95a3SBarry Smith 233*4bcd95a3SBarry Smith... 234*4bcd95a3SBarry Smith 235*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 236*4bcd95a3SBarry Smith:end-at: '#define PetscMalloc7' 237*4bcd95a3SBarry Smith:language: c 238*4bcd95a3SBarry Smith:start-at: '#define PetscMalloc7' 239*4bcd95a3SBarry Smith``` 240*4bcd95a3SBarry Smith 241*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 242*4bcd95a3SBarry Smith:end-at: '#define PetscFree' 243*4bcd95a3SBarry Smith:language: c 244*4bcd95a3SBarry Smith:start-at: '#define PetscFree' 245*4bcd95a3SBarry Smith``` 246*4bcd95a3SBarry Smith 247*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 248*4bcd95a3SBarry Smith:end-at: '#define PetscFree2' 249*4bcd95a3SBarry Smith:language: c 250*4bcd95a3SBarry Smith:start-at: '#define PetscFree2' 251*4bcd95a3SBarry Smith``` 252*4bcd95a3SBarry Smith 253*4bcd95a3SBarry Smith... 254*4bcd95a3SBarry Smith 255*4bcd95a3SBarry Smith```{literalinclude} /../include/petscsys.h 256*4bcd95a3SBarry Smith:end-at: '#define PetscFree7' 257*4bcd95a3SBarry Smith:language: c 258*4bcd95a3SBarry Smith:start-at: '#define PetscFree7' 259*4bcd95a3SBarry Smith``` 260*4bcd95a3SBarry Smith 261*4bcd95a3SBarry SmithSimilar routines, `PetscCalloc1()` to `PetscCalloc7()`, provide 262*4bcd95a3SBarry Smithmemory initialized to zero. The size requests for these macros are in 263*4bcd95a3SBarry Smithnumber of data items requested, not in bytes. This decreases the number 264*4bcd95a3SBarry Smithof errors in the code since the compiler determines their sizes from the 265*4bcd95a3SBarry Smithobject type instead of requiring the user to provide the correct value 266*4bcd95a3SBarry Smithwith `sizeof()`. 267*4bcd95a3SBarry Smith 268*4bcd95a3SBarry SmithThe routines `PetscTrMallocDefault()` and `PetscTrFreeDefault()`, 269*4bcd95a3SBarry Smithwhich are set with the routine `PetscSetUseTrMallocPrivate()` (and are 270*4bcd95a3SBarry Smithused by default for the debug version of PETSc), provide simple logging 271*4bcd95a3SBarry Smithand error checking versions of memory allocation. 272*4bcd95a3SBarry Smith 273*4bcd95a3SBarry Smith## Implementation of Profiling 274*4bcd95a3SBarry Smith 275*4bcd95a3SBarry SmithThis section provides details about the implementation of event logging 276*4bcd95a3SBarry Smithand profiling within the PETSc kernel. The interface for profiling in 277*4bcd95a3SBarry SmithPETSc is contained in the file 278*4bcd95a3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petsclog.h.html">include/petsclog.h</a> 279*4bcd95a3SBarry SmithThe source code for the profile logging is in `src/sys/plog/`. 280*4bcd95a3SBarry Smith 281*4bcd95a3SBarry Smith### Profiling Object Creation and Destruction 282*4bcd95a3SBarry Smith 283*4bcd95a3SBarry SmithThe creation of objects is profiled with the command 284*4bcd95a3SBarry Smith`PetscLogObjectCreate()` 285*4bcd95a3SBarry Smith 286*4bcd95a3SBarry Smith``` 287*4bcd95a3SBarry SmithPetscLogObjectCreate(PetscObject h); 288*4bcd95a3SBarry Smith``` 289*4bcd95a3SBarry Smith 290*4bcd95a3SBarry Smithwhich logs the creation of any PETSc object. Just before an object is 291*4bcd95a3SBarry Smithdestroyed, it should be logged with `PetscLogObjectDestroy()` 292*4bcd95a3SBarry Smith 293*4bcd95a3SBarry Smith``` 294*4bcd95a3SBarry SmithPetscLogObjectDestroy(PetscObject h); 295*4bcd95a3SBarry Smith``` 296*4bcd95a3SBarry Smith 297*4bcd95a3SBarry SmithThese are called automatically by `PetscHeaderCreate()` and 298*4bcd95a3SBarry Smith`PetscHeaderDestroy()`, which are used in creating all objects 299*4bcd95a3SBarry Smithinherited from the basic object. Thus, these logging routines need never 300*4bcd95a3SBarry Smithbe called directly. 301*4bcd95a3SBarry Smith 302*4bcd95a3SBarry SmithIt is also useful to log information about the state of an object, as 303*4bcd95a3SBarry Smithcan be done with the command 304*4bcd95a3SBarry Smith 305*4bcd95a3SBarry Smith``` 306*4bcd95a3SBarry SmithPetscLogObjectState(PetscObject h,const char *format,...); 307*4bcd95a3SBarry Smith``` 308*4bcd95a3SBarry Smith 309*4bcd95a3SBarry SmithFor example, for sparse matrices we usually log the matrix dimensions 310*4bcd95a3SBarry Smithand number of nonzeros. 311*4bcd95a3SBarry Smith 312*4bcd95a3SBarry Smith### Profiling Events 313*4bcd95a3SBarry Smith 314*4bcd95a3SBarry SmithEvents are logged by using the pair `PetscLogEventBegin()` and `PetscLogEventEnd()`. 315*4bcd95a3SBarry Smith 316*4bcd95a3SBarry SmithThis logging is usually done in the abstract interface file for the 317*4bcd95a3SBarry Smithoperations, for example, 318*4bcd95a3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/interface/matrix.c.html">src/mat/interface/matrix.c</a> 319*4bcd95a3SBarry Smith 320*4bcd95a3SBarry Smith### Controlling Profiling 321*4bcd95a3SBarry Smith 322*4bcd95a3SBarry SmithRoutines that control the default profiling available in PETSc include 323*4bcd95a3SBarry Smiththe following 324*4bcd95a3SBarry Smith 325*4bcd95a3SBarry Smith- `PetscLogDefaultBegin();` 326*4bcd95a3SBarry Smith- `PetscLogAllBegin();` 327*4bcd95a3SBarry Smith- `PetscLogDump(const char *filename);` 328*4bcd95a3SBarry Smith- `PetscLogView(PetscViewer);` 329*4bcd95a3SBarry Smith 330*4bcd95a3SBarry SmithThese routines are normally called by the `PetscInitialize()` and 331*4bcd95a3SBarry Smith`PetscFinalize()` routines when the option `-logview` is given. 332*4bcd95a3SBarry Smith 333*4bcd95a3SBarry Smith## References 334*4bcd95a3SBarry Smith 335*4bcd95a3SBarry Smith```{bibliography} /petsc.bib 336*4bcd95a3SBarry Smith:filter: docname in docnames 337*4bcd95a3SBarry Smith``` 338