add: PID and ID specific identifier
This commit is contained in:
parent
47c72eec7e
commit
c656c74fce
@ -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
|
||||
|
||||
47
task1.cpp
47
task1.cpp
@ -2,6 +2,7 @@
|
||||
#include <vector>
|
||||
#include <mpi.h>
|
||||
#include <unistd.h> // Required for gethostname() and getpid()
|
||||
#include <cstdlib> // 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<int> 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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user