add: task2

This commit is contained in:
Saad Mohsin 2025-09-11 12:20:58 +05:00
parent c656c74fce
commit d6c9f5fe25
5 changed files with 99 additions and 4 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
TASK_TO_RUN=task2

View File

@ -16,10 +16,13 @@ RUN chmod 600 /root/.ssh/authorized_keys
RUN echo "Host *\n\tStrictHostKeyChecking no" >> /root/.ssh/config
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# ... (all previous lines are the same) ...
WORKDIR /app
COPY . /app/
# Compile ALL the necessary tasks during the build
RUN mpic++ -O2 -std=c++17 -o mpi_task task.cpp
RUN mpic++ -O2 -std=c++17 -o task1 task1.cpp
RUN mpic++ -O2 -std=c++17 -o task1 task1.cpp
RUN mpic++ -O2 -std=c++17 -o task2 task2.cpp # <-- ADD THIS LINE

View File

@ -2,14 +2,13 @@ services:
master:
build: .
container_name: pdc-master
# REMOVED the 'hostname:' line
volumes:
- ./hostfile:/app/hostfile
# ADDED the 'environment:' block
environment:
- FRIENDLY_NAME=pdc-master
# This line automatically reads from the .env file
command: >
sh -c "/usr/sbin/sshd & sleep 5 && mpirun -np 5 --hostfile /app/hostfile --allow-run-as-root /app/task1"
sh -c "/usr/sbin/sshd & sleep 5 && mpirun -np 5 --hostfile /app/hostfile --allow-run-as-root /app/${TASK_TO_RUN:-task1}"
networks:
- pdcn

View File

@ -4,6 +4,8 @@
#include <unistd.h> // Required for gethostname() and getpid()
#include <cstdlib> // Required for getenv()
// Roll numbers: 22i-1601, 22i-1554, 22i-1689
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);

90
task2.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <iostream>
#include <vector>
#include <mpi.h>
#include <unistd.h> // Required for gethostname() and getpid()
#include <cstdlib> // Required for getenv()
#include <string> // Required for std::string
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char container_id[256];
gethostname(container_id, sizeof(container_id));
const char* friendly_name = getenv("FRIENDLY_NAME");
if (friendly_name == nullptr) { friendly_name = "unknown"; }
pid_t pid = getpid();
int num_workers = world_size - 1;
if (world_rank == 0) {
// --- MASTER PROCESS ---
// Q2. Master has an array {2,3,4,5}.
int data[] = {2, 3, 4, 5};
int data_count = sizeof(data) / sizeof(int);
if (data_count != num_workers) {
std::cerr << "Error: This task requires exactly 4 workers (" << num_workers
<< " found). Please run with -np 5." << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
<< ", rank: 0): distributing array {2,3,4,5} to workers." << std::endl;
for (int i = 0; i < num_workers; ++i) {
MPI_Send(&data[i], 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD);
}
std::vector<int> results(num_workers);
for (int i = 0; i < num_workers; ++i) {
int received_result;
MPI_Recv(&received_result, 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
results[i] = received_result;
}
std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
<< ", rank: 0): gathered results {";
for (int i = 0; i < results.size(); ++i) {
std::cout << results[i] << (i == results.size() - 1 ? "" : ",");
}
std::cout << "}" << std::endl;
} else {
// --- WORKER PROCESSES ---
int received_number;
MPI_Recv(&received_number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int computed_result = 0;
std::string operation_string;
// Q2. Different workers perform different computations based on their rank.
if (world_rank == 1) {
computed_result = received_number * received_number;
operation_string = "squared ->";
} else if (world_rank == 2) {
computed_result = received_number * received_number * received_number;
operation_string = "cubed ->";
} else if (world_rank == 3) {
computed_result = received_number * 2;
operation_string = "doubled ->";
} else if (world_rank == 4) {
computed_result = received_number - 1;
operation_string = "subtracted 1 ->";
}
std::cout << "Worker on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
<< ", rank: " << world_rank << "): received " << received_number
<< ", " << operation_string << " " << computed_result << std::endl;
MPI_Send(&computed_result, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}