diff --git a/Dockerfile b/Dockerfile index 284ed2f..622c012 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM debian:stable-slim -# Install g++, make, OpenMPI, AND the OpenSSH server +# ... (all the apt-get install and ssh setup lines are unchanged) ... RUN apt-get update && apt-get install -y --no-install-recommends \ g++ \ make \ @@ -9,28 +9,17 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ openssh-server && \ rm -rf /var/lib/apt/lists/* -# Create a directory for the SSH daemon to run (use -p to avoid errors) RUN mkdir -p /var/run/sshd - -# --- SSH Key Generation and Configuration --- -# Generate a passwordless SSH key pair RUN ssh-keygen -t rsa -f /root/.ssh/id_rsa -N '' - -# Add the public key to the list of authorized keys for passwordless login RUN cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys RUN chmod 600 /root/.ssh/authorized_keys - -# --- THIS IS THE NEW LINE --- -# Create an SSH config file to disable host key checking RUN echo "Host *\n\tStrictHostKeyChecking no" >> /root/.ssh/config -# --- END OF NEW LINE --- - -# Allow root login via SSH RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config WORKDIR /app COPY . /app/ -# Compile using the MPI C++ compiler wrapper -RUN mpic++ -O2 -std=c++17 -o mpi_task task.cpp \ No newline at end of file +# 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 diff --git a/docker-compose.yml b/docker-compose.yml index 54dc97e..9342d96 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,16 +2,18 @@ services: master: build: . container_name: pdc-master + hostname: pdc-master # <-- ADD THIS LINE volumes: - ./hostfile:/app/hostfile command: > - sh -c "/usr/sbin/sshd & sleep 5 && mpirun -np 4 --hostfile /app/hostfile --allow-run-as-root /app/mpi_task" + sh -c "/usr/sbin/sshd & sleep 5 && mpirun -np 5 --hostfile /app/hostfile --allow-run-as-root /app/task1" networks: - pdcn worker1: build: . container_name: pdc-worker1 + hostname: pdc-worker1 # <-- ADD THIS LINE command: /usr/sbin/sshd -D networks: - pdcn @@ -19,6 +21,7 @@ services: worker2: build: . container_name: pdc-worker2 + hostname: pdc-worker2 # <-- ADD THIS LINE command: /usr/sbin/sshd -D networks: - pdcn @@ -26,6 +29,15 @@ services: worker3: build: . container_name: pdc-worker3 + hostname: pdc-worker3 # <-- ADD THIS LINE + command: /usr/sbin/sshd -D + networks: + - pdcn + + worker4: + build: . + container_name: pdc-worker4 + hostname: pdc-worker4 # <-- ADD THIS LINE command: /usr/sbin/sshd -D networks: - pdcn diff --git a/hostfile b/hostfile index 9c17876..78e3a4f 100644 --- a/hostfile +++ b/hostfile @@ -1,4 +1,5 @@ master slots=1 worker1 slots=1 worker2 slots=1 -worker3 slots=1 \ No newline at end of file +worker3 slots=1 +worker4 slots=1 \ No newline at end of file diff --git a/task1.cpp b/task1.cpp new file mode 100644 index 0000000..516b0d1 --- /dev/null +++ b/task1.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include // Required for gethostname() and getpid() + +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); + + // Get the hostname of the container/machine the process is running on. + char hostname[256]; + gethostname(hostname, sizeof(hostname)); + + // 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); + + 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; + + 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); + } + + 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); + results[i] = received_result; + } + + // Updated print statement + std::cout << "Master (rank 0, PID " << pid << " on " << hostname << "): collected 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 = received_number * 10; + + // Updated print statement to include PID and Hostname + std::cout << "Worker (rank " << world_rank << ", PID " << pid << " on " << hostname + << "): received " << received_number + << ", computed " << 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