fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6. int rank, size;
  7. MPI_Init(&argc, &argv); // Initialize MPI environment
  8. MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the rank of the process
  9. MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the total number of processes
  10.  
  11. char message[20];
  12. if (rank % 2 == 0) { // Even rank
  13. strcpy(message, "Hello World");
  14. if (rank + 1 < size) { // Ensure the corresponding odd rank exists
  15. MPI_Send(message, strlen(message) + 1, MPI_CHAR, rank + 1, 0, MPI_COMM_WORLD); // Send the message to the next odd rank
  16. printf("Even process %d sent message '%s' to odd process %d\n", rank, message, rank + 1);
  17. }
  18. } else { // Odd rank
  19. MPI_Recv(message, 20, MPI_CHAR, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receive the message from the previous even rank
  20. printf("Odd process %d received message '%s' from even process %d\n", rank, message, rank - 1);
  21. }
  22.  
  23. MPI_Finalize(); // Finalize MPI environment
  24. return 0;
  25. }
  26.  
Success #stdin #stdout #stderr 0.29s 40704KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted