Unified header for the scratchpad memory management system.
This is the main include file for the scratchpad subsystem. It provides a single include point for all scratchpad-related functionality, including the abstract base class, concrete implementations, type definitions, the StateBlob container, utility functions, and formatters for debugging.
- What are Scratchpads?
- Scratchpads are temporary working memory containers used by computational engines during ODE integration and reaction network calculations. They serve several critical purposes:
- Performance: Pre-allocate memory once and reuse across iterations, avoiding repeated heap allocations during time-critical computations.
- Caching: Store intermediate results (Jacobians, derivatives, etc.) that can be reused across solver steps.
- Thread Safety: Provide thread-local storage for parallel execution, where each thread operates on its own independent scratchpad instance.
- State Management: Encapsulate engine-specific working state separate from the engine's persistent configuration.
- Architecture Overview
- The scratchpad system consists of:
- AbstractScratchPad: Base interface defining
is_initialized() and clone()
- Concrete Scratchpads: Engine-specific implementations
GraphEngineScratchPad: CppAD ADFun and Jacobian caches
AdaptiveEngineViewScratchPad: Active species/reactions for adaptive networks
DefinedEngineViewScratchPad: Species/reactions for static networks
MultiscalePartitioningEngineViewScratchPad: QSE groups and SUNDIALS context
- StateBlob: Container managing multiple scratchpads with type-safe access
- Utilities: Helper functions for exception-based retrieval
- Formatters: std::format specializations for debugging output
- Why Use Scratchpads?
- During numerical integration of stiff reaction networks, engines must:
- Evaluate right-hand side functions (species derivatives)
- Compute sparse Jacobian matrices
- Solve linear systems within Newton iterations
These operations require substantial temporary memory. Without scratchpads, each evaluation would allocate and deallocate working buffers, causing:
- Memory fragmentation
- Cache thrashing
- Unnecessary allocation overhead
Scratchpads solve this by providing persistent, reusable working memory that lives for the duration of an integration step (or longer).
- Thread Safety Model
- The scratchpad system is designed for thread-local usage:
- Scratchpads are not thread-safe by design
- Each thread must have its own scratchpad instances
- Use
StateBlob::clone_structure() to create independent copies for workers
- The original scratchpad/blob remains usable by the main thread
- Examples
auto& adfun =
scratch->rhsADFun.value();
std::vector<std::unique_ptr<StateBlob>> worker_blobs;
for (int i = 0; i < num_threads; ++i) {
}
#pragma omp parallel for
for (int i = 0; i < work_items; ++i) {
int tid = omp_get_thread_num();
compute_with_scratchpad(*my_scratch);
}
std::cout << std::format("Blob state: {}\n", blob);
}
Container for managing a collection of typed scratchpad instances.
Definition blob.h:114
ScratchPadStatus get_status() const
Get the status of a specific scratchpad type.
Definition blob.h:445
@ ENROLLED_NOT_INITIALIZED
Scratchpad enrolled but not yet initialized.
Definition blob.h:164
void enroll()
Enroll a new scratchpad type into the blob.
Definition blob.h:196
std::unique_ptr< StateBlob > clone_structure() const
Create a deep copy of this blob with all enrolled scratchpads.
Definition blob.h:361
Scratchpad memory management for computational engines.
Definition blob.h:69
CTX * get_state(StateBlob &ctx)
Retrieve a scratchpad from a StateBlob, throwing on error.
Definition utils.h:84
Definition dynamic_engine_diagnostics.h:39
Unified header for the scratchpad memory management system.
Scratchpad for storing working memory used by AdaptiveEngineView computations.
Definition engine_adaptive_scratchpad.h:66
void initialize(const AdaptiveEngineView &engine)
Initialize the scratchpad from an AdaptiveEngineView.
Definition engine_adaptive_scratchpad.h:102
Scratchpad for storing CppAD automatic differentiation state for GraphEngine.
Definition engine_graph_scratchpad.h:83
- Included Headers
- This header includes:
- See also
- AbstractScratchPad
-
StateBlob
-
ScratchPadType