A header-only differential debugging framework for C++ that helps detect numerical differences between runs (e.g., after code optimizations or compiler changes).
- Zero-dependency: Single header file, C++17 compatible
- Multiple validation strategies: Full, First, Hash, and Stats
- MPI support: Works with any launcher (no MPI headers required)
- Flexible context tracking: Thread indices through loops or use RAII scopes
- Debugger integration: Traps on mismatch for interactive debugging
- Tolerance-based comparison: Configurable relative/absolute tolerances
- Comprehensive tests: Unit tests with Catch2 for validation strategies
#include "diffdbg.hpp"
int main(int argc, char** argv) {
diffdbg::init(diffdbg::modeFromEnv(), diffdbg::fileFromEnv(), diffdbg::strategyFromEnv());
// Your code here
for (int step = 0; step < nSteps; ++step) {
// ... compute something ...
DIFFDBG_CHECK_VEC(velocity, step);
DIFFDBG_CHECK_VEC(pressure, step);
}
diffdbg::finalize();
return 0;
}DIFFDBG_MODE=record DIFFDBG_STRATEGY=full DIFFDBG_FILE=ref.bin ./your_appThis creates ref.bin containing the reference data.
DIFFDBG_MODE=check DIFFDBG_FILE=ref.bin ./your_appIf any value differs, the program prints the location and traps into the debugger (SIGTRAP).
| Strategy | Description | Best For |
|---|---|---|
full (default) |
Element-wise comparison with tolerance | Floating-point data, pinpointing bad elements |
first |
Only compare first element | Quick checks, tiny reference files |
hash |
64-bit FNV-1a hash of raw bytes | Integer data, bitwise reproducibility |
stats |
min/max/mean comparison | Noisy floating-point, robust to rounding |
| Variable | Description | Default |
|---|---|---|
DIFFDBG_MODE |
off|record|check |
off |
DIFFDBG_FILE |
Path to reference file | diffdbg_reference.bin |
DIFFDBG_STRATEGY |
full|first|hash|stats |
full |
DIFFDBG_RANK |
Override process rank | Auto-detected |
DIFFDBG_ONLY_RANK |
Only this rank runs diffdbg | All ranks |
For nested loops or multi-dimensional indices:
DIFFDBG_CHECK_VEC(velocity, step, substep); // Multi-dimensional keyOr using scoped context:
for (int step = 0; step < nSteps; ++step) {
DIFFDBG_SCOPE(step);
for (int substep = 0; substep < nSub; ++substep) {
DIFFDBG_SCOPE(substep);
solveInnerKernel(...); // Checks automatically tagged
}
}Works with any launcher without MPI dependencies:
# Auto-detects rank from environment
DIFFDBG_MODE=record DIFFDBG_FILE=ref.bin mpirun -n 4 ./app
DIFFDBG_MODE=check DIFFDBG_FILE=ref.bin mpirun -n 4 ./appOr explicitly set rank:
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
diffdbg::setRank(rank); // Call BEFORE init()
diffdbg::init(...);mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
# Run example
DIFFDBG_MODE=record DIFFDBG_FILE=ref.bin ./examples/example
DIFFDBG_MODE=check DIFFDBG_FILE=ref.bin ./examples/example| Option | Description | Default |
|---|---|---|
DIFFDBG_BUILD_EXAMPLES |
Build example programs | ON |
To disable examples:
cmake .. -DCMAKE_BUILD_TYPE=Release -DDIFFDBG_BUILD_EXAMPLES=OFFvoid diffdbg::init(Mode mode, const std::string& file, Strategy strategy);
void diffdbg::finalize();DIFFDBG_CHECK_VEC(vector) // Check std::vector
DIFFDBG_CHECK_ARR(ptr, count) // Check raw array
DIFFDBG_CHECK_SCALAR(value) // Check scalar value
// With explicit indices
DIFFDBG_CHECK_VEC(vec, i, j, k)DIFFDBG_SCOPE(value); // RAII context markerdiffdbg::Config& diffdbg::config(); // Access configurationConfiguration fields:
mode:Mode::Off,Mode::Record, orMode::Checkstrategy: Validation strategyrank: Process rank (-1 for single process)referenceFile: Path to reference filerelTol: Relative tolerance (default: 1e-9)absTol: Absolute tolerance (default: 1e-12)breakOnMismatch: Trap into debugger (default: true)stopAtFirstElement: Stop scanning on first mismatch (default: true)verboseOk: Log successful checks (default: false)
See LICENSE file.
Portions of this software were developed with the assistance of AI language models.