Skip to content

utkan0602/MPI-Process

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation


🪨📄✂️ Rock-Paper-Scissors with MPI

A distributed-memory Rock-Paper-Scissors game implemented in C using MPI (Message Passing Interface) for exactly 2 processes.


📖 Table of Contents


🎯 About the Project

This program simulates a game of Rock-Paper-Scissors between two parallel processes. Process 0 acts as the referee: it prints all outputs, determines the winner of each round, keeps the score, and ends the game when one process reaches 5 points. If both processes choose the same move, the round is a draw and is replayed.

✨ Features

  • Language: Written entirely in C.
  • Framework: Utilizes MPI for distributed process communication.
  • Constraints: Runs with exactly 2 processes.
  • Modes: - 0 → Point-to-Point Communication
    • 1 → Collective Communication
  • Mechanics: Random move generation on both processes seeded uniquely to avoid mirrored plays.
  • Environment: Compatible with Linux environments using mpicc and mpirun.
  • Clean: No file reading or writing. Includes a Makefile for easy compilation.

📂 Project Structure

.
├── game.c        # Main source code
├── Makefile      # Build instructions
└── README.md     # Project documentation

🚀 Getting Started

Prerequisites

To compile and run this project, you need:

  • A Linux environment (intended target).
  • A C compiler that supports MPI (typically mpicc).
  • An MPI runtime launcher (e.g., mpirun or mpiexec).

Check if MPI is installed:

mpicc --version
mpirun --version

If both commands work, your environment is ready.

Installation

If MPI is not installed, use the appropriate command for your OS:

Ubuntu / Debian

sudo apt update
sudo apt install openmpi-bin libopenmpi-dev

Fedora

sudo dnf install openmpi openmpi-devel

Arch Linux

sudo pacman -S openmpi

macOS (Homebrew)

brew install open-mpi

Note: On macOS, behavior may vary slightly depending on your shell and MPI configuration. Linux is the intended target environment.


🛠 Compilation & Execution

Compilation

Compile the project using the included Makefile:

make

If successful, this generates the game executable using a command similar to mpicc -Wall -O2 -o game game.c.

Running the Program

Run the game with exactly 2 MPI processes:

mpirun -n 2 ./game

The program will prompt you to select a mode:

Select mode: 0 (P2P) or 1 (Collective)

Enter 0 or 1.

Example Run

The game starts
Select mode: 0 (P2P) or 1 (Collective)
0
Mode: 0
Turn 1, Process-0: ROCK, Process-1: PAPER
Process-1 wins, Score: 0 - 1
Turn 2, Process-0: SCISSORS, Process-1: ROCK
Process-1 wins, Score: 0 - 2
Turn 3, Process-0: PAPER, Process-1: PAPER
Draw, replay the round.
...
Process-0 has won the game with score: 5 - 4 in 11 turns.
The game ends

📡 Communication Modes

0 - Point-to-Point Mode

This mode uses direct communication between processes.

  • Process 1 generates its move and sends it to Process 0.
  • Process 0 generates its own move, receives Process 1’s move, determines the winner, and updates the score.
  • Key MPI Functions: MPI_Send, MPI_Recv

1 - Collective Mode

This mode uses collective MPI communication.

  • Each process generates its move.
  • Moves are collected at Process 0.
  • Process 0 determines the round result and broadcasts the updated scores back to all processes.
  • Key MPI Functions: MPI_Gather, MPI_Bcast

⚖️ Game Rules & Logic

Basic Rules:

  • Rock 🪨 beats Scissors ✂️
  • Scissors ✂️ beats Paper 📄
  • Paper 📄 beats Rock 🪨

Scoring & Game Flow:

  • The round winner gets 1 point.
  • A tie results in a draw (no points, round is replayed).
  • The game immediately ends when a process hits 5 points.

Randomness: Each MPI process seeds its random number generator separately using time and rank information. This ensures runs aren't identical and processes don't blindly mimic each other.


🧪 Testing

A complete test sequence to verify your build:

make clean
make
mpirun -n 2 ./game

Recommended Test Cases:

  1. P2P Mode: Run mpirun -n 2 ./game and enter 0. Verify Process 0 prints output and the game ends at 5 points.
  2. Collective Mode: Run mpirun -n 2 ./game and enter 1. Verify collective logic functions and ends at 5 points.
  3. Invalid Process Count: Run mpirun -n 3 ./game. The program should print an error message and exit safely.
  4. Invalid Mode: Run mpirun -n 2 ./game and enter 7. The program should report an invalid mode and exit cleanly without crashing.

🆘 Troubleshooting

Issue Solution
make: mpicc: No such file or directory MPI compiler is not installed or not in your PATH. Install MPI first.
mpirun: command not found MPI runtime is missing. Install OpenMPI or MPICH.
Program does not compile Ensure the file is exactly game.c, Makefile is Makefile, and Makefile commands use tabs, not spaces.
Error about number of processes You must run with exactly 2 processes: mpirun -n 2 ./game.
Output is different every run This is expected! Moves are randomly generated.
Both processes print messages Only Process 0 should print. Ensure your printf statements are guarded by if (rank == 0).

⚠️ Academic Integrity

This project was prepared for an academic homework assignment. Demonstrating MPI process management, point-to-point/collective communication, and distributed state synchronization.

Notice: If you are a student taking the same course, do not copy or submit this work as your own. Use it only within your course rules and academic integrity guidelines.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors