This project provides a CUDA-accelerated Python script that generates an ordered list of prime numbers using an NVIDIA GPU via Numba. It streams primes to a text file and supports safe stop/restart.
- CUDA-accelerated prime checking using a segmented sieve approach.
- Ordered output: primes are written in ascending order.
- Graceful shutdown: press Ctrl+C to stop; the script saves the current state.
- Resume capability: on restart, the script continues generating from the last prime.
- Configurable segment size: adjust how many numbers are tested per GPU batch.
-
Python 3.9+
-
CUDA Toolkit (Runtime + NVVM) or conda
cudapackage- Recommended:
conda create -n primegpu python=3.11 numba cuda -c nvidia -c conda-forge conda activate primegpu pip install numpy
- Alternatively, install the CUDA Toolkit from NVIDIA (ensure
nvvm.dllis on yourPATH).
- Recommended:
-
Numba (0.59+ for Python 3.12; 0.61+ for Python 3.13)
pip install numba
-
NumPy
pip install numpy
Verify that Numba sees your GPU:
from numba import cuda
print(cuda.detect())-
Clone this repository or download the code:
git clone https://github.com/yourusername/gpu-prime-generator cd gpu-prime-generator -
Ensure your Python environment has the prerequisites (see above).
Run the script with default settings:
python gpu_prime_generator.py- Creates (or appends to)
primes.txtin the current folder. - Press Ctrl+C to stop. The script prints a summary, for example:
Stopped by user. Found 784,984 primes in 12.4 s (63 k p/s)
-
-o, --output <file>
Destination file (default:primes.txt). If the file exists, the script resumes from the last prime. -
-n, --segment <count>
How many integers to test per GPU batch (default:1000000).
Example:
python gpu_prime_generator.py -o my_primes.txt -n 5000000-
CPU sieve (bootstrap)
- Computes all primes up to √N for each segment using a standard Sieve of Eratosthenes.
-
GPU kernel
- Each CUDA thread tests one odd candidate number by checking divisibility against the pre-sieved primes.
- Results are streamed back, filtered, and written to the output file in ascending order.
-
Segmented loop
- Numbers are processed in chunks (segments) of configurable size to manage memory usage.
- After each segment, the script flushes new primes to disk and prints progress.
-
Graceful shutdown / resume
- On startup, the script reads the last prime in the output file (if any) and keeps a running total.
- Pressing Ctrl+C triggers a
KeyboardInterrupt, closing the file and printing a final summary.
- Increase
--segmentfor fewer kernel launches (uses more GPU memory). - Adjust
threads_per_blockin the source code (128–512 threads is typical). - For best write performance, use an SSD or fast NVMe drive.
- Ensure only one CUDA installation is on your
PATHto avoid conflicts.
-
nvvm.dll not found
Ensure the CUDA Toolkit (runtime + NVVM) is installed and itsbindirectories are onPATH.
For conda users, install thecudameta-package:conda install -c nvidia -c conda-forge cuda
-
CUDA driver mismatch
Make sure your GPU driver version is ≥ the CUDA Toolkit version you installed. -
Python / Numba compatibility
- Python 3.12 requires Numba 0.59+
- Python 3.13 requires Numba 0.61+
This project is released under the MIT License. See LICENSE for details.