#include <mpi.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int rank, size;
MPI_Init(&argc, &argv); // Initialize MPI environment
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the rank of the process
MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the total number of processes
char message[20];
if (rank % 2 == 0) { // Even rank
strcpy(message
, "Hello World"); if (rank + 1 < size) { // Ensure the corresponding odd rank exists
MPI_Send
(message
, strlen(message
) + 1, MPI_CHAR
, rank
+ 1, 0, MPI_COMM_WORLD
); // Send the message to the next odd rank printf("Even process %d sent message '%s' to odd process %d\n", rank
, message
, rank
+ 1); }
} else { // Odd rank
MPI_Recv(message, 20, MPI_CHAR, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receive the message from the previous even rank
printf("Odd process %d received message '%s' from even process %d\n", rank
, message
, rank
- 1); }
MPI_Finalize(); // Finalize MPI environment
return 0;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgKmFyZ3ZbXSkgewogICAgaW50IHJhbmssIHNpemU7CiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOyAgICAgICAgICAgLy8gSW5pdGlhbGl6ZSBNUEkgZW52aXJvbm1lbnQKICAgIE1QSV9Db21tX3JhbmsoTVBJX0NPTU1fV09STEQsICZyYW5rKTsgICAvLyBHZXQgdGhlIHJhbmsgb2YgdGhlIHByb2Nlc3MKICAgIE1QSV9Db21tX3NpemUoTVBJX0NPTU1fV09STEQsICZzaXplKTsgICAvLyBHZXQgdGhlIHRvdGFsIG51bWJlciBvZiBwcm9jZXNzZXMKCiAgICBjaGFyIG1lc3NhZ2VbMjBdOwogICAgaWYgKHJhbmsgJSAyID09IDApIHsgICAvLyBFdmVuIHJhbmsKICAgICAgICBzdHJjcHkobWVzc2FnZSwgIkhlbGxvIFdvcmxkIik7C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