fork download
  1. #include "mpi.h"
  2. #include <iostream>
  3.  
  4. int main(int argc, char *argv[]) {
  5. int numtasks, rank, dest, source, tag = 1;
  6. MPI_Status Stat;
  7. int inmsg, outmsg;
  8. MPI_Init(&argc, &argv);
  9. MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
  10. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  11.  
  12. outmsg = rank;
  13. if (rank % 2 == 0) {
  14. dest = (rank + 1) % numtasks;
  15. MPI_Send(&outmsg, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
  16. source = (rank == 0) ? numtasks - 1 : rank - 1;
  17. MPI_Recv(&inmsg, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
  18. } else {
  19. source = (rank == 0) ? numtasks - 1 : rank - 1;
  20. MPI_Recv(&inmsg, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
  21. dest = (rank + 1) % numtasks;
  22. MPI_Send(&outmsg, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
  23. }
  24.  
  25. std::cout << "Task " << rank << ": Received " << inmsg << std::endl;
  26.  
  27. MPI_Finalize();
  28. return 0;
  29. }
  30.  
Success #stdin #stdout #stderr 0.3s 40772KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted