13.5. Data Structure Reuse

Up: Contents Next: Numerical Experiments Previous: PetscMalloc() Calls

Data structures should be reused whenever possible. For example, if a code often creates new matrices or vectors, there often may be a way to reuse some of them. Very significant performance improvements can be achieved by reusing matrix data structures with the same nonzero pattern. If a code creates thousands of matrix or vector objects, performance will be degraded. For example, when solving a nonlinear problem or timestepping, reusing the matrices and their nonzero structure for many steps when appropriate can make the code run significantly faster.

A simple technique for saving work vectors, matrices, etc. is employing a user-defined context. In C and C++ such a context is merely a structure in which various objects can be stashed; in Fortran a user context can be an integer array that contains both parameters and pointers to PETSc objects. See ${}PETSC_DIR/snes/examples/tutorials/ex5.c and ${}PETSC_DIR/snes/examples/tutorials/ex5f.F for examples of user-defined application contexts in C and Fortran, respectively.


Up: Contents Next: Numerical Experiments Previous: PetscMalloc() Calls