A 2D pollutant-diffusion simulation written in CUDA C and driven from Python with PyCUDA. The simulation steps, the per-cell danger tracking and the per-region statistics all run on the GPU.
Every cell of the grid holds a concentration. Some cells are sources (their concentration is reset to a fixed value in every step) and some are absorbers (they lose a fixed amount in every step). Each simulation step:
- Diffusion. Every cell becomes the average of itself and its four neighbours (a 3×3 filter), multiplied by
1 - decay. Cells outside the grid count as 0. - Sources and absorbers are applied.
- Danger tracking. A cell is dangerous when its concentration is above the danger threshold.
danger_countcounts in how many steps each cell has been dangerous. - Ever-dangerous cells. The number of cells that have been dangerous at least once.
- Hotspots. The grid is divided into equal rectangular regions. For every region, the mean and the standard deviation of
danger_countare computed, and a cell is a hotspot when its count is abovemean + alfa * stdof its region.
bonus.py adds region profiles: every region has a type, and every type has its own danger threshold and alfa.
| File | Contents |
|---|---|
simulation.py |
the simulation; loads the kernels from the .cu files |
bonus.py |
the same simulation with region profiles (kernels inside the script) |
diffusion_step_kernel.cu |
3×3 stencil with decay; one thread per cell, the filter is in __constant__ memory |
apply_sources_kernel.cu |
sources and absorbers; one thread per cell |
count_danger_kernel.cu |
per-cell danger count; in bonus.py the threshold of each region comes from an array of structs in __constant__ memory |
count_ever_dangerous_kernel.cu |
number of ever-dangerous cells, with atomicAdd |
region_hotspot_stats_kernel.cu |
one thread block per region: two tree reductions in dynamic shared memory (sum → mean, sum of squared deviations → standard deviation), then the hotspot test; the region size does not have to be a power of two |
- Every kernel is compiled once at startup, and all GPU buffers are allocated once.
- The grids stay in GPU memory for the whole run. Data is copied to the host only when it is printed.
- The concentration is double-buffered on the GPU: the diffusion step writes into a second buffer and the two buffers swap, so there is no host-side padding and no copying between steps.
- An NVIDIA GPU and the CUDA Toolkit (PyCUDA compiles the kernels with
nvcc) - Python 3 with NumPy and PyCUDA:
pip install -r requirements.txtRun the scripts from the repository folder (the kernel files are loaded with relative paths):
python simulation.py
python bonus.pyThe parameters are at the top of the if __name__ == '__main__': block: grid size W, H, number of steps T, decay, source_value, absorb_value, danger_threshold, alfa and the region size block_W, block_H (it must divide the grid and have at most 1024 cells, since a region is one thread block). With print_steps = True the state is printed after every step; for large grids set print_steps = False, so that only the running time is printed. In bonus.py, the profiles are in h_region_profiles and the type of every region in region_types.