#include <mpi.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
int rank, size, n, x, parent, slot;
int array[n + 1];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Initialize variables
n = 5; // Number of child nodes
x = 0;
for (int i = 0; i <= n; ++i) {
array[i] = 0;
}
// Phase 2: Barrier loop
while (1) {
x++;
// Initialize flags
for (int j = 0; j <= n; ++j) {
if (rank >= (rank / n) + 1 + j) {
array[j] = 0;
} else {
array[j] = 1;
}
}
array[n] = 0;
// Repeat until barrier is reached
do {
parent = (rank - 1) / n;
slot = (rank - 1) % n;
if (array[0] + array[1] + array[2] + array[3] + array[4] == 4) {
if (rank == 0) {
array[n] = 1;
} else {
MPI_Send(&array[slot], 1, MPI_INT, parent, 0, MPI_COMM_WORLD);
}
}
} while (array[n] != 1);
// Phase 3: Broadcast barrier reached flag
if (rank == 0) {
for (int i = 1; i < size; ++i) {
MPI_Send(&array[n], 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
} else {
MPI_Recv(&array[n], 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCmludCBtYWluKGludCBhcmdjLCBjaGFyKiBhcmd2W10pIHsKICAgIGludCByYW5rLCBzaXplLCBuLCB4LCBwYXJlbnQsIHNsb3Q7CiAgICBpbnQgYXJyYXlbbiArIDFdOwoKICAgIE1QSV9Jbml0KCZhcmdjLCAmYXJndik7CiAgICBNUElfQ29tbV9yYW5rKE1QSV9DT01NX1dPUkxELCAmcmFuayk7CiAgICBNUElfQ29tbV9zaXplKE1QSV9DT01NX1dPUkxELCAmc2l6ZSk7CgogICAgLy8gSW5pdGlhbGl6ZSB2YXJpYWJsZXMKICAgIG4gPSA1OyAvLyBOdW1iZXIgb2YgY2hpbGQgbm9kZXMKICAgIHggPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbjsgKytpKSB7CiAgICAgICAgYXJyYXlbaV0gPSAwOwogICAgfQoKICAgIC8vIFBoYXNlIDI6IEJhcnJpZXIgbG9vcAogICAgd2hpbGUgKDEpIHsKICAgICAgICB4Kys7CgogICAgICAgIC8vIEluaXRpYWxpemUgZmxhZ3MKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8PSBuOyArK2opIHsKICAgICAgICAgICAgaWYgKHJhbmsgPj0gKHJhbmsgLyBuKSArIDEgKyBqKSB7CiAgICAgICAgICAgICAgICBhcnJheVtqXSA9IDA7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBhcnJheVtqXSA9IDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgYXJyYXlbbl0gPSAwOwoKICAgICAgICAvLyBSZXBlYXQgdW50aWwgYmFycmllciBpcyByZWFjaGVkCiAgICAgICAgZG8gewogICAgICAgICAgICBwYXJlbnQgPSAocmFuayAtIDEpIC8gbjsKICAgICAgICAgICAgc2xvdCA9IChyYW5rIC0gMSkgJSBuOwoKICAgICAgICAgICAgaWYgKGFycmF5WzBdICsgYXJyYXlbMV0gKyBhcnJheVsyXSArIGFycmF5WzNdICsgYXJyYXlbNF0gPT0gNCkgewogICAgICAgICAgICAgICAgaWYgKHJhbmsgPT0gMCkgewogICAgICAgICAgICAgICAgICAgIGFycmF5W25dID0gMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgTVBJX1NlbmQoJmFycmF5W3Nsb3RdLCAxLCBNUElfSU5ULCBwYXJlbnQsIDAsIE1QSV9DT01NX1dPUkxEKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKGFycmF5W25dICE9IDEpOwoKICAgICAgICAvLyBQaGFzZSAzOiBCcm9hZGNhc3QgYmFycmllciByZWFjaGVkIGZsYWcKICAgICAgICBpZiAocmFuayA9PSAwKSB7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgc2l6ZTsgKytpKSB7CiAgICAgICAgICAgICAgICBNUElfU2VuZCgmYXJyYXlbbl0sIDEsIE1QSV9JTlQsIGksIDAsIE1QSV9DT01NX1dPUkxEKTsKICAgICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIE1QSV9SZWN2KCZhcnJheVtuXSwgMSwgTVBJX0lOVCwgMCwgMCwgTVBJX0NPTU1fV09STEQsIE1QSV9TVEFUVVNfSUdOT1JFKTsKICAgICAgICB9CiAgICB9CgogICAgTVBJX0ZpbmFsaXplKCk7CiAgICByZXR1cm4gMDsKfQ==