add: task4
This commit is contained in:
parent
2aa6d67112
commit
974729a066
@ -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
|
||||
# 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
|
||||
46
task4.cpp
Normal file
46
task4.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user