From d6c9f5fe253edaec41290b9c3b6286d7d726835a Mon Sep 17 00:00:00 2001 From: Saad Mohsin Date: Thu, 11 Sep 2025 12:20:58 +0500 Subject: [PATCH] add: task2 --- .env | 1 + Dockerfile | 5 ++- docker-compose.yml | 5 ++- task1.cpp | 2 ++ task2.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 .env create mode 100644 task2.cpp diff --git a/.env b/.env new file mode 100644 index 0000000..2fbd278 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +TASK_TO_RUN=task2 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 622c012..c0c1be2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ No newline at end of file +RUN mpic++ -O2 -std=c++17 -o task1 task1.cpp +RUN mpic++ -O2 -std=c++17 -o task2 task2.cpp # <-- ADD THIS LINE \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8af0db5..460e5bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/task1.cpp b/task1.cpp index 66ae3e1..23da51f 100644 --- a/task1.cpp +++ b/task1.cpp @@ -4,6 +4,8 @@ #include // Required for gethostname() and getpid() #include // Required for getenv() +// Roll numbers: 22i-1601, 22i-1554, 22i-1689 + int main(int argc, char** argv) { MPI_Init(&argc, &argv); diff --git a/task2.cpp b/task2.cpp new file mode 100644 index 0000000..593785a --- /dev/null +++ b/task2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include // Required for gethostname() and getpid() +#include // Required for getenv() +#include // 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 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; +} \ No newline at end of file