fork download
  1. #include <mpi.h>
  2. #include <iostream>
  3.  
  4. int main(int argc, char** argv) {
  5. MPI_Init(&argc, &argv);
  6.  
  7. int rank, size;
  8. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  9. MPI_Comm_size(MPI_COMM_WORLD, &size);
  10.  
  11. int message;
  12. MPI_Status status;
  13.  
  14. if (rank == 0) {
  15. // Initialize message to be sent
  16. message = rank;
  17.  
  18. // Send message to the next process in a ring
  19. MPI_Send(&message, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD);
  20.  
  21. // Receive message from the last process in a ring
  22. MPI_Recv(&message, 1, MPI_INT, size - 1, 0, MPI_COMM_WORLD, &status);
  23.  
  24. // Output the received message
  25. std::cout << "Process " << rank << " received message: " << message << std::endl;
  26. } else {
  27. // Receive message from the previous process in a ring
  28. MPI_Recv(&message, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &status);
  29.  
  30. // Increment message
  31. message = (message + 1) % size;
  32.  
  33. // Send message to the next process in a ring
  34. MPI_Send(&message, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD);
  35.  
  36. // Output the received message
  37. std::cout << "Process " << rank << " received message: " << message << std::endl;
  38. }
  39.  
  40. MPI_Finalize();
  41. return 0;
  42. }
  43.  
Success #stdin #stdout #stderr 0.33s 40312KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted