Skip to content

rustinm/primes-cuda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🔍 Prime Finder with CPU, Multi-threading, and GPU Acceleration

This Python project finds all prime numbers up to a user-defined limit using three different methods for comparison:

  1. 🧠 Single-threaded CPU (Numba JIT)
  2. ⚙️ Multi-threaded CPU (Python multiprocessing)
  3. 🚀 GPU acceleration using CUDA (via Numba)

📦 Requirements

  • Python 3.8+
  • An NVIDIA GPU with CUDA support (for GPU mode)
  • pip or conda package manager

🧪 Setup Instructions (Step-by-Step)

1. 🔁 Clone the Repository

git clone https://github.com/rustinm/primes-cuda.git
cd primes-cuda

2. 🧰 Create and Activate a Virtual Environment (Recommended)

📦 Using Python’s built-in venv:

python3 -m venv .venv

Then activate it:

  • On Windows:

    .venv\Scripts\activate
  • On macOS/Linux:

    source .venv/bin/activate

3. 📥 Install Required Packages

First, make sure pip is up to date:

pip install --upgrade pip

Then install dependencies from requirements.txt:

pip install -r requirements.txt

4. ⚠️ Fix for CUDA Toolkit (If GPU Doesn't Work)

If GPU acceleration gives errors related to CUDA:

✅ Option A: Install CUDA toolkit using conda (recommended)

conda install cudatoolkit=11.8

✅ Option B: Ensure system-wide CUDA is correctly installed

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


🚀 How to Run the Program

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:

  1. Run the single-threaded CPU version.
  2. Wait for you to press Enter to continue.
  3. Run the multi-threaded CPU version.
  4. Wait for another Enter.
  5. Run the GPU-accelerated version.

🧾 Example Output

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

📄 File Structure

primes-finder/
│
├── primes.py            # Main script
├── requirements.txt     # Dependencies
└── README.md            # You are here!

📌 Notes

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

📝 License

MIT License. Feel free to use and modify!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published