Heat2D is a modern C++ project that simulates heat diffusion on a 2D grid.
The goal of this project is to build a clean and extensible C++ codebase while progressively introducing concepts related to high-performance computing, memory layout optimization, benchmarking and parallel programming.
This project starts with a sequential implementation and is designed to evolve toward multithreaded and OpenMP-based versions.
The simulation represents a 2D metallic plate as a grid of temperature values.
At each iteration, the temperature of each inner cell is updated using the average of its four direct neighbors:
new[i][j] = 0.25 * (
old[i-1][j] +
old[i+1][j] +
old[i][j-1] +
old[i][j+1]
)Boundary cells are kept fixed.
- Modern C++ implementation
- Object-oriented project structure
- Cache-friendly 1D memory layout for the 2D grid
- Sequential heat diffusion solver
- Configurable grid size and number of iterations
- CSV export of the final grid
- Basic execution time measurement
- Python visualization script
heat2d/
├── include/
│ ├── Grid.hpp
│ ├── HeatSolver.hpp
│ └── Timer.hpp
│
├── src/
│ ├── Grid.cpp
│ ├── HeatSolver.cpp
│ └── main.cpp
│
├── tests/
├── plot_heat.py
├── CMakeLists.txt
└── README.md-
C++20 compatible compiler
-
CMake 3.16 or higher
-
Python 3
-
Python packages for visualization:
- pandas
- matplotlib
Install the Python dependencies with:
pip install pandas matplotlibFrom the root of the project:
mkdir build
cd build
cmake ..
cmake --build .Default execution:
./heat2dThis runs the simulation with default parameters:
Rows: 500
Columns: 500
Iterations: 500You can also provide custom parameters:
./heat2d <rows> <cols> <iterations>Example:
./heat2d 1000 1000 500Example output:
Heat2D simulation completed
Grid: 1000 x 1000
Iterations: 500
Execution time: 1.83 seconds
Output file: heat_output.csvAfter running the simulation, a CSV file is generated:
build/heat_output.csvTo visualize the final heat distribution:
python3 plot_heat.pyThe script loads the generated CSV file and displays the heat map using matplotlib.
Instead of using:
std::vector<std::vector<double>>the grid is stored as a single contiguous 1D vector:
std::vector<double> data_;A cell (i, j) is accessed internally with:
data_[i * cols_ + j]This representation improves memory locality and is closer to the layout typically used in numerical and high-performance computing applications.
The current version includes:
Grid: a 2D grid abstraction backed by contiguous memoryHeatSolver: a sequential heat diffusion solverTimer: a small utility for measuring execution time- CSV export for result visualization
- command-line parameters for grid size and iteration count
Planned improvements:
- Add unit tests
- Add benchmark scripts
- Compare different grid sizes
- Implement a
std::threadparallel version - Implement an OpenMP version
- Measure speedup and parallel efficiency
- Add performance plots
- Add MPI-based domain decomposition as an advanced extension
Future experiments will compare execution time across different grid sizes:
500 x 500
1000 x 1000
2000 x 2000
4000 x 4000And across different implementations:
Sequential
std::thread
OpenMP
MPIUseful metrics:
Execution time
Speedup
Parallel efficiency
Memory usageThis project is designed to practice:
- Modern C++
- Header/source file separation
- Classes and encapsulation
- Memory layout optimization
- Numerical simulation
- Performance measurement
- Parallel programming fundamentals
- HPC-oriented software design
This project is released under the MIT License.