This Python project finds all prime numbers up to a user-defined limit using three different methods for comparison:
- 🧠 Single-threaded CPU (Numba JIT)
- ⚙️ Multi-threaded CPU (Python multiprocessing)
- 🚀 GPU acceleration using CUDA (via Numba)
- Python 3.8+
- An NVIDIA GPU with CUDA support (for GPU mode)
pip
orconda
package manager
git clone https://github.com/rustinm/primes-cuda.git
cd primes-cuda
python3 -m venv .venv
Then activate it:
-
On Windows:
.venv\Scripts\activate
-
On macOS/Linux:
source .venv/bin/activate
First, make sure pip
is up to date:
pip install --upgrade pip
Then install dependencies from requirements.txt
:
pip install -r requirements.txt
If GPU acceleration gives errors related to CUDA:
conda install cudatoolkit=11.8
Make sure your NVIDIA driver and CUDA Toolkit (e.g., 11.8) are installed. To check:
nvcc --version
If nvcc
isn't found, install CUDA Toolkit from: https://developer.nvidia.com/cuda-downloads
Once everything is set up, run:
python primes.py
You will be prompted to enter a number:
Enter the upper limit to find primes up to: 1000000000
The program will:
- Run the single-threaded CPU version.
- Wait for you to press Enter to continue.
- Run the multi-threaded CPU version.
- Wait for another Enter.
- Run the GPU-accelerated version.
Enter the upper limit to find primes up to: 100
🔍 Finding primes up to 100...
🧠 CPU single-thread version:
✅ Found 25 primes in 0.01 seconds
🔁 Press Enter to continue to multi-threaded CPU...
primes-finder/
│
├── primes.py # Main script
├── requirements.txt # Dependencies
└── README.md # You are here!
- On systems without a compatible GPU, the GPU step will likely fail or be slow.
- For large limits (e.g.,
1_000_000_000
), only use multi-threaded CPU or GPU versions. - This tool is educational and suitable for comparing performance across methods.
MIT License. Feel free to use and modify!