diff --git a/.env b/.env deleted file mode 100644 index 2fbd278..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -TASK_TO_RUN=task2 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 29e5f24..0000000 --- a/Dockerfile +++ /dev/null @@ -1,27 +0,0 @@ -FROM debian:stable-slim - -# ... (all the apt-get install and ssh setup lines are unchanged) ... -RUN apt-get update && apt-get install -y --no-install-recommends \ - g++ \ - make \ - openmpi-bin \ - libopenmpi-dev \ - openssh-server && \ - rm -rf /var/lib/apt/lists/* - -RUN mkdir -p /var/run/sshd -RUN ssh-keygen -t rsa -f /root/.ssh/id_rsa -N '' -RUN cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys -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 ONLY task4 during the build to avoid unrelated compile failures -RUN mpic++ -O2 -std=c++17 -o task5 task5.cpp -RUN chmod +x /app/task4 || true \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index d34f9d8..0000000 --- a/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# command to run: docker-compose up --build - -# command to shut down: docker-compose down - -# Roll numbers: 22i-1601, 22i-1554, 22i-1689 diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 460e5bf..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,61 +0,0 @@ -services: - master: - build: . - container_name: pdc-master - volumes: - - ./hostfile:/app/hostfile - 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/${TASK_TO_RUN:-task1}" - networks: - - pdcn - - worker1: - build: . - container_name: pdc-worker1 - # REMOVED the 'hostname:' line - # ADDED the 'environment:' block - environment: - - FRIENDLY_NAME=pdc-worker1 - command: /usr/sbin/sshd -D - networks: - - pdcn - - worker2: - build: . - container_name: pdc-worker2 - # REMOVED the 'hostname:' line - # ADDED the 'environment:' block - environment: - - FRIENDLY_NAME=pdc-worker2 - command: /usr/sbin/sshd -D - networks: - - pdcn - - worker3: - build: . - container_name: pdc-worker3 - # REMOVED the 'hostname:' line - # ADDED the 'environment:' block - environment: - - FRIENDLY_NAME=pdc-worker3 - command: /usr/sbin/sshd -D - networks: - - pdcn - - worker4: - build: . - container_name: pdc-worker4 - # REMOVED the 'hostname:' line - # ADDED the 'environment:' block - environment: - - FRIENDLY_NAME=pdc-worker4 - command: /usr/sbin/sshd -D - networks: - - pdcn - -networks: - pdcn: - driver: bridge \ No newline at end of file diff --git a/hostfile b/hostfile deleted file mode 100644 index 78e3a4f..0000000 --- a/hostfile +++ /dev/null @@ -1,5 +0,0 @@ -master slots=1 -worker1 slots=1 -worker2 slots=1 -worker3 slots=1 -worker4 slots=1 \ No newline at end of file diff --git a/task.cpp b/task.cpp deleted file mode 100644 index 2d6c7e3..0000000 --- a/task.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include - -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); - - if (world_rank == 0) { - // Master process - std::string message = "HELLOWORLDFROMMPI"; // A longer string to see the effect - std::cout << "Original Message: " << message << std::endl; - std::cout << "Total processes (world size): " << world_size << std::endl; - - int msg_len = message.length(); - int part_size = msg_len / world_size; - - // Send each worker its part of the string - for (int i = 1; i < world_size; ++i) { - int start_index = i * part_size; - // The last worker gets any remaining characters - int current_part_size = (i == world_size - 1) ? (msg_len - start_index) : part_size; - std::string part = message.substr(start_index, current_part_size); - MPI_Send(part.c_str(), part.size() + 1, MPI_CHAR, i, 0, MPI_COMM_WORLD); - } - - // Master processes its own part - std::string master_part = message.substr(0, part_size); - std::reverse(master_part.begin(), master_part.end()); - - // A vector to store results in order - std::vector results(world_size); - results[0] = master_part; - - // Receive reversed parts from workers - for (int i = 1; i < world_size; ++i) { - char buffer[256]; - MPI_Recv(buffer, 256, MPI_CHAR, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - results[i] = std::string(buffer); - } - - // Combine and print the final result - std::string final_result = ""; - for (int i = 0; i < world_size; ++i) { - final_result += results[i]; - } - std::cout << "Final Result: " << final_result << std::endl; - - } else { - // Worker processes - char buffer[256]; - MPI_Recv(buffer, 256, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - std::string part(buffer); - std::reverse(part.begin(), part.end()); - MPI_Send(part.c_str(), part.size() + 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD); - } - - MPI_Finalize(); - return 0; -} \ No newline at end of file diff --git a/task1.cpp b/task1.cpp deleted file mode 100644 index f91fca2..0000000 --- a/task1.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -#include // Required for gethostname() and getpid() -#include // Required for getenv() - -// Roll numbers: 22i-1601, 22i-1554, 22i-1689 -// Question 1 - -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); - - // 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 - } - - pid_t pid = getpid(); - int num_workers = world_size - 1; - - if (world_rank == 0) { - // --- MASTER PROCESS --- - int data[] = {1, 2, 3, 4}; - // ... (error checking code is the same) ... - - // 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) { - 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): 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 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); - } - - MPI_Finalize(); - return 0; -} \ No newline at end of file diff --git a/task2.cpp b/task2.cpp deleted file mode 100644 index 8badc3a..0000000 --- a/task2.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include -#include -#include // Required for gethostname() and getpid() -#include // Required for getenv() -#include // Required for std::string - -// Roll numbers: 22i-1601, 22i-1554, 22i-1689 -// Question 2 - -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 diff --git a/task3.cpp b/task3.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/task4.cpp b/task4.cpp deleted file mode 100644 index b77d41c..0000000 --- a/task4.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -int main(int argc, char** argv) { - int rank; - int worldSize; - - MPI_Init(&argc, &argv); - MPI_Comm_size(MPI_COMM_WORLD, &worldSize); - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - - const int elementsPerProcess = 5; - int sendBuffer[elementsPerProcess]; - for (int i = 0; i < elementsPerProcess; ++i) { - sendBuffer[i] = rank; // Each process fills its buffer with its rank - } - - int* receiveBuffer = nullptr; - if (rank == 0) { - receiveBuffer = new int[elementsPerProcess * worldSize]; - } - - MPI_Gather( - sendBuffer, // send buffer - elementsPerProcess, // number of elements sent - MPI_INT, // type of each element sent - receiveBuffer, // receive buffer (root only) - elementsPerProcess, // number of elements received from each proc - MPI_INT, // type of each element received - 0, // root rank - MPI_COMM_WORLD // communicator - ); - - if (rank == 0) { - for (int p = 0; p < worldSize; ++p) { - for (int i = 0; i < elementsPerProcess; ++i) { - std::printf("Process %d sent: %d\n", p, receiveBuffer[p * elementsPerProcess + i]); - } - } - delete[] receiveBuffer; - } - - std::printf("Bye from %d\n", rank); - MPI_Finalize(); - return 0; -} \ No newline at end of file diff --git a/task5.cpp b/task5.cpp deleted file mode 100644 index 5c32f61..0000000 --- a/task5.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include - -int main(int argc, char** argv) { - MPI_Init(&argc, &argv); - - int worldSize = 0; - int worldRank = 0; - MPI_Comm_size(MPI_COMM_WORLD, &worldSize); - MPI_Comm_rank(MPI_COMM_WORLD, &worldRank); - - // Enforce exactly 5 processes (hard-coded layout below assumes 5 ranks) - if (worldSize != 5) { - if (worldRank == 0) { - std::printf("This program must be run with exactly 5 MPI processes.\n"); - } - MPI_Abort(MPI_COMM_WORLD, 1); - } - - // Hard-coded total elements and source data on root - const int totalElements = 17; - std::vector sendBuffer; - if (worldRank == 0) { - sendBuffer.resize(totalElements); - for (int i = 0; i < totalElements; ++i) { - sendBuffer[i] = i; // 0..16 - } - } - - // Hard-coded sendCounts and displacements for 5 ranks and 17 elements - // Rank 0 -> 4 elems [0..3] - // Rank 1 -> 4 elems [4..7] - // Rank 2 -> 3 elems [8..10] - // Rank 3 -> 3 elems [11..13] - // Rank 4 -> 3 elems [14..16] - const int sendCounts[5] = {4, 4, 3, 3, 3}; - const int displacements[5] = {0, 4, 8, 11, 14}; - - const int myReceiveCount = sendCounts[worldRank]; - std::vector receiveBuffer(myReceiveCount, -1); - - MPI_Scatterv( - worldRank == 0 ? sendBuffer.data() : nullptr, // sendbuf (root only) - sendCounts, // sendcounts (root only) - displacements, // displs (root only) - MPI_INT, // send type - receiveBuffer.data(), // recvbuf (all ranks) - myReceiveCount, // recvcount for this rank - MPI_INT, // recv type - 0, // root - MPI_COMM_WORLD // communicator - ); - - for (int i = 0; i < myReceiveCount; ++i) { - std::printf("Rank %d received element %d: %d\n", worldRank, i, receiveBuffer[i]); - } - - std::printf("Bye from %d\n", worldRank); - MPI_Finalize(); - return 0; -} \ No newline at end of file