diff --git a/Dockerfile b/Dockerfile index 2103e1c..29e5f24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,5 +23,5 @@ WORKDIR /app COPY . /app/ # Compile ONLY task4 during the build to avoid unrelated compile failures -RUN mpic++ -O2 -std=c++17 -o task4 task4.cpp +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/task5.cpp b/task5.cpp new file mode 100644 index 0000000..5c32f61 --- /dev/null +++ b/task5.cpp @@ -0,0 +1,62 @@ +#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