fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mpi.h>
  4.  
  5. #define MAX_MESSAGE_LENGTH 20
  6.  
  7. int main(int argc, char **argv) {
  8. int rank, size;
  9. char message[MAX_MESSAGE_LENGTH];
  10. MPI_Status status;
  11.  
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &size);
  16. char* messages[] = {"mepco", "schlenk", "engineering"};
  17.  
  18. if (rank == 0) {
  19. // Process 0 sends "mepco" to process 1
  20. MPI_Send(messages[0], strlen(messages[0]) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
  21. // Process 0 receives from the last process
  22. MPI_Recv(message, MAX_MESSAGE_LENGTH, MPI_CHAR, size - 1, 0, MPI_COMM_WORLD, &status);
  23. printf("Process %d received: %s\n", rank, message);
  24. } else if (rank == size - 1) {
  25. MPI_Recv(message, MAX_MESSAGE_LENGTH, MPI_CHAR, rank - 1, 0, MPI_COMM_WORLD, &status);
  26. printf("Process %d received: %s\n", rank, message);
  27. MPI_Send(messages[1], strlen(messages[1]) + 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  28. } else {
  29. MPI_Recv(message, MAX_MESSAGE_LENGTH, MPI_CHAR, rank - 1, 0, MPI_COMM_WORLD, &status);
  30. printf("Process %d received: %s\n", rank, message);
  31. MPI_Send(messages[2], strlen(messages[2]) + 1, MPI_CHAR, rank + 1, 0, MPI_COMM_WORLD);
  32. }
  33. MPI_Finalize(); // Finalize MPI environment
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout #stderr 0.29s 40816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted