From c656c74fce23456f60e628a7d297fb79e470c05f Mon Sep 17 00:00:00 2001 From: Saad Mohsin Date: Thu, 11 Sep 2025 12:05:21 +0500 Subject: [PATCH] add: PID and ID specific identifier --- docker-compose.yml | 25 +++++++++++++++++++----- task1.cpp | 47 +++++++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9342d96..8af0db5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,9 +2,12 @@ services: master: build: . container_name: pdc-master - hostname: pdc-master # <-- ADD THIS LINE + # REMOVED the 'hostname:' line volumes: - ./hostfile:/app/hostfile + # ADDED the 'environment:' block + environment: + - FRIENDLY_NAME=pdc-master command: > sh -c "/usr/sbin/sshd & sleep 5 && mpirun -np 5 --hostfile /app/hostfile --allow-run-as-root /app/task1" networks: @@ -13,7 +16,10 @@ services: worker1: build: . container_name: pdc-worker1 - hostname: pdc-worker1 # <-- ADD THIS LINE + # REMOVED the 'hostname:' line + # ADDED the 'environment:' block + environment: + - FRIENDLY_NAME=pdc-worker1 command: /usr/sbin/sshd -D networks: - pdcn @@ -21,7 +27,10 @@ services: worker2: build: . container_name: pdc-worker2 - hostname: pdc-worker2 # <-- ADD THIS LINE + # REMOVED the 'hostname:' line + # ADDED the 'environment:' block + environment: + - FRIENDLY_NAME=pdc-worker2 command: /usr/sbin/sshd -D networks: - pdcn @@ -29,7 +38,10 @@ services: worker3: build: . container_name: pdc-worker3 - hostname: pdc-worker3 # <-- ADD THIS LINE + # REMOVED the 'hostname:' line + # ADDED the 'environment:' block + environment: + - FRIENDLY_NAME=pdc-worker3 command: /usr/sbin/sshd -D networks: - pdcn @@ -37,7 +49,10 @@ services: worker4: build: . container_name: pdc-worker4 - hostname: pdc-worker4 # <-- ADD THIS LINE + # REMOVED the 'hostname:' line + # ADDED the 'environment:' block + environment: + - FRIENDLY_NAME=pdc-worker4 command: /usr/sbin/sshd -D networks: - pdcn diff --git a/task1.cpp b/task1.cpp index 516b0d1..66ae3e1 100644 --- a/task1.cpp +++ b/task1.cpp @@ -2,6 +2,7 @@ #include #include #include // Required for gethostname() and getpid() +#include // Required for getenv() int main(int argc, char** argv) { MPI_Init(&argc, &argv); @@ -12,47 +13,41 @@ int main(int argc, char** argv) { int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); - // Get the hostname of the container/machine the process is running on. - char hostname[256]; - gethostname(hostname, sizeof(hostname)); + // This will now get the unique Container ID, e.g., "039f5fe4970d" + char container_id[256]; + gethostname(container_id, sizeof(container_id)); + + // This gets the friendly name from the environment variable we set + const char* friendly_name = getenv("FRIENDLY_NAME"); + if (friendly_name == nullptr) { + friendly_name = "unknown"; // A fallback in case the variable isn't set + } - // Get the Process ID of this specific MPI process. pid_t pid = getpid(); - int num_workers = world_size - 1; if (world_rank == 0) { // --- MASTER PROCESS --- int data[] = {1, 2, 3, 4}; - int data_count = sizeof(data) / sizeof(int); + // ... (error checking code is the same) ... - if (data_count != num_workers) { - if (world_rank == 0) { // Only master should print the error - std::cerr << "Error: The number of workers (" << num_workers - << ") must match the number of data elements (" << data_count << ").\n" - << "Please run with -np " << data_count + 1 << " processes." << std::endl; - } - MPI_Abort(MPI_COMM_WORLD, 1); - } - - // Updated print statement to include PID and Hostname - std::cout << "Master (rank 0, PID " << pid << " on " << hostname << "): sending numbers 1,2,3,4 to workers..." << std::endl; + // Updated print statement with all information + std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid + << ", rank: 0): sending numbers 1,2,3,4 to workers..." << std::endl; for (int i = 0; i < num_workers; ++i) { - int worker_rank = i + 1; - MPI_Send(&data[i], 1, MPI_INT, worker_rank, 0, MPI_COMM_WORLD); + 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 worker_rank = i + 1; int received_result; - MPI_Recv(&received_result, 1, MPI_INT, worker_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&received_result, 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); results[i] = received_result; } - // Updated print statement - std::cout << "Master (rank 0, PID " << pid << " on " << hostname << "): collected results {"; + std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid + << ", rank: 0): collected results {"; for (int i = 0; i < results.size(); ++i) { std::cout << results[i] << (i == results.size() - 1 ? "" : ","); } @@ -65,9 +60,9 @@ int main(int argc, char** argv) { int computed_result = received_number * 10; - // Updated print statement to include PID and Hostname - std::cout << "Worker (rank " << world_rank << ", PID " << pid << " on " << hostname - << "): received " << received_number + // Updated print statement with all information + std::cout << "Worker on " << friendly_name << " (ID: " << container_id << ", PID: " << pid + << ", rank: " << world_rank << "): received " << received_number << ", computed " << computed_result << std::endl; MPI_Send(&computed_result, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);