Remove old code files from talat branch

This commit is contained in:
root 2025-09-20 13:47:36 +05:00
parent ccc19d3074
commit 443081b156
11 changed files with 0 additions and 443 deletions

1
.env
View File

@ -1 +0,0 @@
TASK_TO_RUN=task2

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,5 +0,0 @@
master slots=1
worker1 slots=1
worker2 slots=1
worker3 slots=1
worker4 slots=1

View File

@ -1,67 +0,0 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <mpi.h>
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<std::string> 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;
}

View File

@ -1,76 +0,0 @@
#include <iostream>
#include <vector>
#include <mpi.h>
#include <unistd.h> // Required for gethostname() and getpid()
#include <cstdlib> // 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<int> 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;
}

View File

@ -1,93 +0,0 @@
#include <iostream>
#include <vector>
#include <mpi.h>
#include <unistd.h> // Required for gethostname() and getpid()
#include <cstdlib> // Required for getenv()
#include <string> // 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<int> 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;
}

View File

View File

@ -1,46 +0,0 @@
#include <mpi.h>
#include <cstdio>
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;
}

View File

@ -1,62 +0,0 @@
#include <mpi.h>
#include <cstdio>
#include <vector>
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<int> 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<int> 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;
}