fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(int argc, char** argv) {
  5. const int PING_PONG_LIMIT = 10;
  6. // Initialize the MPI environment
  7. MPI_Init(NULL, NULL);
  8. // Find out rank, size
  9. int world_rank;
  10. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  11. int world_size;
  12. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  13. // We are assuming 2 processes for this task
  14. if (world_size != 2) {
  15. fprintf(stderr, "World size must be two for %s\n", argv[0]);
  16. MPI_Abort(MPI_COMM_WORLD, 1);
  17. }
  18. int ping_pong_count = 0;
  19. int partner_rank = (world_rank + 1) % 2;
  20. while (ping_pong_count < PING_PONG_LIMIT) {
  21. if (world_rank == ping_pong_count % 2) {
  22. // Increment the ping pong count before you send it
  23. ping_pong_count++;
  24. MPI_Send(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD);
  25. printf("%d sent and incremented ping_pong_count %d to %d\n",
  26. world_rank, ping_pong_count, partner_rank);
  27. } else {
  28. MPI_Recv(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD,
  29. MPI_STATUS_IGNORE);
  30. printf("%d received ping_pong_count %d from %d\n",
  31. world_rank, ping_pong_count, partner_rank);
  32. }
  33. }
  34. MPI_Finalize();
  35. }
  36.  
Success #stdin #stdout #stderr 0.28s 40684KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted