add: PID and ID specific identifier
This commit is contained in:
parent
47c72eec7e
commit
c656c74fce
@ -2,9 +2,12 @@ services:
|
|||||||
master:
|
master:
|
||||||
build: .
|
build: .
|
||||||
container_name: pdc-master
|
container_name: pdc-master
|
||||||
hostname: pdc-master # <-- ADD THIS LINE
|
# REMOVED the 'hostname:' line
|
||||||
volumes:
|
volumes:
|
||||||
- ./hostfile:/app/hostfile
|
- ./hostfile:/app/hostfile
|
||||||
|
# ADDED the 'environment:' block
|
||||||
|
environment:
|
||||||
|
- FRIENDLY_NAME=pdc-master
|
||||||
command: >
|
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/task1"
|
||||||
networks:
|
networks:
|
||||||
@ -13,7 +16,10 @@ services:
|
|||||||
worker1:
|
worker1:
|
||||||
build: .
|
build: .
|
||||||
container_name: pdc-worker1
|
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
|
command: /usr/sbin/sshd -D
|
||||||
networks:
|
networks:
|
||||||
- pdcn
|
- pdcn
|
||||||
@ -21,7 +27,10 @@ services:
|
|||||||
worker2:
|
worker2:
|
||||||
build: .
|
build: .
|
||||||
container_name: pdc-worker2
|
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
|
command: /usr/sbin/sshd -D
|
||||||
networks:
|
networks:
|
||||||
- pdcn
|
- pdcn
|
||||||
@ -29,7 +38,10 @@ services:
|
|||||||
worker3:
|
worker3:
|
||||||
build: .
|
build: .
|
||||||
container_name: pdc-worker3
|
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
|
command: /usr/sbin/sshd -D
|
||||||
networks:
|
networks:
|
||||||
- pdcn
|
- pdcn
|
||||||
@ -37,7 +49,10 @@ services:
|
|||||||
worker4:
|
worker4:
|
||||||
build: .
|
build: .
|
||||||
container_name: pdc-worker4
|
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
|
command: /usr/sbin/sshd -D
|
||||||
networks:
|
networks:
|
||||||
- pdcn
|
- pdcn
|
||||||
|
|||||||
47
task1.cpp
47
task1.cpp
@ -2,6 +2,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <mpi.h>
|
#include <mpi.h>
|
||||||
#include <unistd.h> // Required for gethostname() and getpid()
|
#include <unistd.h> // Required for gethostname() and getpid()
|
||||||
|
#include <cstdlib> // Required for getenv()
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
MPI_Init(&argc, &argv);
|
MPI_Init(&argc, &argv);
|
||||||
@ -12,47 +13,41 @@ int main(int argc, char** argv) {
|
|||||||
int world_rank;
|
int world_rank;
|
||||||
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
|
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
|
||||||
|
|
||||||
// Get the hostname of the container/machine the process is running on.
|
// This will now get the unique Container ID, e.g., "039f5fe4970d"
|
||||||
char hostname[256];
|
char container_id[256];
|
||||||
gethostname(hostname, sizeof(hostname));
|
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();
|
pid_t pid = getpid();
|
||||||
|
|
||||||
int num_workers = world_size - 1;
|
int num_workers = world_size - 1;
|
||||||
|
|
||||||
if (world_rank == 0) {
|
if (world_rank == 0) {
|
||||||
// --- MASTER PROCESS ---
|
// --- MASTER PROCESS ---
|
||||||
int data[] = {1, 2, 3, 4};
|
int data[] = {1, 2, 3, 4};
|
||||||
int data_count = sizeof(data) / sizeof(int);
|
// ... (error checking code is the same) ...
|
||||||
|
|
||||||
if (data_count != num_workers) {
|
// Updated print statement with all information
|
||||||
if (world_rank == 0) { // Only master should print the error
|
std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
|
||||||
std::cerr << "Error: The number of workers (" << num_workers
|
<< ", rank: 0): sending numbers 1,2,3,4 to workers..." << std::endl;
|
||||||
<< ") 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;
|
|
||||||
|
|
||||||
for (int i = 0; i < num_workers; ++i) {
|
for (int i = 0; i < num_workers; ++i) {
|
||||||
int worker_rank = i + 1;
|
MPI_Send(&data[i], 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD);
|
||||||
MPI_Send(&data[i], 1, MPI_INT, worker_rank, 0, MPI_COMM_WORLD);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> results(num_workers);
|
std::vector<int> results(num_workers);
|
||||||
for (int i = 0; i < num_workers; ++i) {
|
for (int i = 0; i < num_workers; ++i) {
|
||||||
int worker_rank = i + 1;
|
|
||||||
int received_result;
|
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;
|
results[i] = received_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updated print statement
|
std::cout << "Master on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
|
||||||
std::cout << "Master (rank 0, PID " << pid << " on " << hostname << "): collected results {";
|
<< ", rank: 0): collected results {";
|
||||||
for (int i = 0; i < results.size(); ++i) {
|
for (int i = 0; i < results.size(); ++i) {
|
||||||
std::cout << results[i] << (i == results.size() - 1 ? "" : ",");
|
std::cout << results[i] << (i == results.size() - 1 ? "" : ",");
|
||||||
}
|
}
|
||||||
@ -65,9 +60,9 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
int computed_result = received_number * 10;
|
int computed_result = received_number * 10;
|
||||||
|
|
||||||
// Updated print statement to include PID and Hostname
|
// Updated print statement with all information
|
||||||
std::cout << "Worker (rank " << world_rank << ", PID " << pid << " on " << hostname
|
std::cout << "Worker on " << friendly_name << " (ID: " << container_id << ", PID: " << pid
|
||||||
<< "): received " << received_number
|
<< ", rank: " << world_rank << "): received " << received_number
|
||||||
<< ", computed " << computed_result << std::endl;
|
<< ", computed " << computed_result << std::endl;
|
||||||
|
|
||||||
MPI_Send(&computed_result, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
|
MPI_Send(&computed_result, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user