This project simulates a producer-consumer system using MPI (Message Passing Interface) in C, where producers generate work items and send them to a broker, which then distributes them to consumers.
The program consists of three main components:
- Producer: Each producer generates work and sends it to the broker.
- Broker: The broker receives work from producers and dispatches it to consumers upon request.
- Consumer: Each consumer requests work from the broker and processes the received work.
This simulation demonstrates basic principles of parallel computing and communication using MPI.
- Asynchronous Communication: The program uses non-blocking MPI operations (
MPI_IsendandMPI_Recv) to send and receive messages between processes. - Time Management: The broker checks the elapsed time during communication and can abort the process if it exceeds a predefined threshold.
- Work Production and Consumption: Producers generate work based on a formula and send it to the broker. Consumers request and process work from the broker.
- Synchronization: The program synchronizes communication between producers, consumers, and the broker using MPI.
- MPI Library: You need to have an MPI implementation installed. For example, you can install OpenMPI or MPICH.
- C Compiler: A C compiler like GCC is required to compile the code.
-
Install MPI: Ensure that you have an MPI library installed. For OpenMPI, you can install it on Linux systems using:
sudo apt-get install openmpi-bin openmpi-common libopenmpi-dev
-
Compile the Program: Use the
mpicccompiler to compile the code:mpicc -o producer_consumer producer_consumer.c
-
Run the Program: Use
mpirunormpiexecto run the program with multiple processes. For example, to run with 4 processes:mpirun -np 4 ./producer_consumer <time_threshold>
Where
<time_threshold>is the time limit (in seconds) after which the broker will abort the communication.
- Producers generate work units and send them to the broker.
- Broker receives work from producers and stores it. The broker then sends work to consumers upon request.
- Consumers request work from the broker, process the work, and continue until the communication time limit is reached or all work is consumed.
- time_rec: Defines the time threshold (in seconds) after which the broker sends an "ABORT" message to the processes.
- MPI_Isend and MPI_Recv: Used for asynchronous communication between producers, the broker, and consumers.
- MPI_Waitany: Waits for any of the non-blocking send/receive operations to complete.
For producers:
Producer 1 sent message 5 to Broker
Producer 1 received message ACK from Broker
For consumers:
Consumer 2 sent message Req to Broker
Consumer 2 received message 7 from Broker