This project implements Conway's Game of Life using CUDA 12.6 for parallel processing and SDL for graphical rendering. The Game of Life is a cellular automaton where cells live, die, or multiply based on certain rules.
To run this project, you'll need:
- CUDA 12.6: Ensure that you have CUDA 12.6 installed on your system. You can download it from the NVIDIA website.
- Visual Studio 2022: This project was built using Visual Studio 2022. You can download it from Microsoft's website.
- SDL2: Install the Simple DirectMedia Layer (SDL) for graphical rendering. You can get it from SDL's official website.
- Install CUDA 12.6 on your system and configure the environment paths.
- Open Visual Studio 2022 and create a new project:
- Go to File > New > Project.
- Select CUDA 12.6 Runtime from the available templates.
- Add SDL2 to your project:
- Download the SDL2 development libraries.
- Include the SDL headers in your project and link the SDL2 libraries.
- Clone this repository and open the project in Visual Studio.
This project uses CUDA to update the state of the Game of Life grid and SDL to render the grid on a window.
- The grid is a 2D array where each cell is either alive (
1
) or dead (0
). - CUDA is used to calculate the next generation of the grid in parallel, which speeds up the computation for large grids.
- SDL is used to draw the grid on the screen, with each live cell represented as a green square.
- GameOfLife.cu: Contains the main logic for the Game of Life, including CUDA kernels for updating the grid and SDL code for rendering.
The core of the simulation is the CUDA kernel updateGrid
, which computes the next state of each cell based on its neighbors:
__global__ void updateGrid(bool* d_grid, bool* d_newGrid) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= GRID_WIDTH || y >= GRID_HEIGHT) return;
int aliveNeighbors = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
int nx = (x + dx + GRID_WIDTH) % GRID_WIDTH;
int ny = (y + dy + GRID_HEIGHT) % GRID_HEIGHT;
aliveNeighbors += d_grid[ny * GRID_WIDTH + nx];
}
}
bool currentCell = d_grid[y * GRID_WIDTH + x];
bool newState = (currentCell && (aliveNeighbors == 2 || aliveNeighbors == 3)) ||
(!currentCell && aliveNeighbors == 3);
d_newGrid[y * GRID_WIDTH + x] = newState;
}
- Build the project in Visual Studio 2022.
- Make sure that you have CUDA and SDL properly configured in your environment.
- Run the project, and a window will appear displaying the Game of Life simulation.
- Green squares represent live cells.
- The grid updates every 100 milliseconds.