Skip to content

About

2D pollutant-diffusion simulation in CUDA driven from PyCUDA, with per-cell danger tracking and per-region hotspot detection using shared-memory reductions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

CUDA Diffusion Simulation with Hotspot Detection

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.

What it computes

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:

  1. 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.
  2. Sources and absorbers are applied.
  3. Danger tracking. A cell is dangerous when its concentration is above the danger threshold. danger_count counts in how many steps each cell has been dangerous.
  4. Ever-dangerous cells. The number of cells that have been dangerous at least once.
  5. Hotspots. The grid is divided into equal rectangular regions. For every region, the mean and the standard deviation of danger_count are computed, and a cell is a hotspot when its count is above mean + alfa * std of its region.

bonus.py adds region profiles: every region has a type, and every type has its own danger threshold and alfa.

Files

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

Implementation notes

  • 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.

Requirements

  • An NVIDIA GPU and the CUDA Toolkit (PyCUDA compiles the kernels with nvcc)
  • Python 3 with NumPy and PyCUDA:
pip install -r requirements.txt

Running

Run the scripts from the repository folder (the kernel files are loaded with relative paths):

python simulation.py
python bonus.py

The 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.

About

2D pollutant-diffusion simulation in CUDA driven from PyCUDA, with per-cell danger tracking and per-region hotspot detection using shared-memory reductions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages