From 974729a0662b7b13fa826c782b796176d92bb58d Mon Sep 17 00:00:00 2001 From: Saad Mohsin Date: Thu, 18 Sep 2025 12:11:20 +0500 Subject: [PATCH] add: task4 --- Dockerfile | 7 +++---- task3.cpp | 0 task4.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 task3.cpp create mode 100644 task4.cpp diff --git a/Dockerfile b/Dockerfile index c0c1be2..2103e1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,6 @@ WORKDIR /app COPY . /app/ -# 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 -RUN mpic++ -O2 -std=c++17 -o task2 task2.cpp # <-- ADD THIS LINE \ No newline at end of file +# Compile ONLY task4 during the build to avoid unrelated compile failures +RUN mpic++ -O2 -std=c++17 -o task4 task4.cpp +RUN chmod +x /app/task4 || true \ No newline at end of file diff --git a/task3.cpp b/task3.cpp new file mode 100644 index 0000000..e69de29 diff --git a/task4.cpp b/task4.cpp new file mode 100644 index 0000000..b77d41c --- /dev/null +++ b/task4.cpp @@ -0,0 +1,46 @@ +#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