fork download
  1.  
  2. #include "mpi.h"
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7. int testWinner(char player1, char player2);
  8.  
  9. const char* rsp = "rsp";
  10.  
  11. int main(int argc, char* argv[])
  12. {
  13. int size, rank, play, otherPlay, tag1 = 1, tag2 = 2, winner = -1000;
  14. MPI_Status Stat;
  15.  
  16. MPI_Init(&argc, &argv);
  17. MPI_Comm_size(MPI_COMM_WORLD, &size);
  18. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19.  
  20. srand(rank * time(0));
  21. play = rand() % 3;
  22.  
  23. if (rank == 0) {
  24. MPI_Send(&play, 1, MPI_INT, 1, tag1, MPI_COMM_WORLD);
  25. MPI_Recv(&otherPlay, 1, MPI_INT, 1, tag2, MPI_COMM_WORLD, &Stat);
  26. winner = testWinner(rsp[play], rsp[otherPlay]);
  27. }
  28. else if (rank == 1) {
  29.  
  30. MPI_Recv(&otherPlay, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &Stat);
  31. MPI_Send(&play, 1, MPI_INT, 0, tag2, MPI_COMM_WORLD);
  32. winner = testWinner(rsp[otherPlay], rsp[play]);
  33. }
  34.  
  35. printf("The winner from processor %d is %d\n", rank, winner);
  36. MPI_Finalize();
  37. return 0;
  38. }
  39.  
  40. int testWinner(char player1, char player2)
  41. {
  42. if (player1 == player2)
  43. return -1;
  44. if ((player1 == 'r' && player2 == 's') ||
  45. (player1 == 's' && player2 == 'p') ||
  46. (player1 == 'p' && player2 == 'r'))
  47. return 0;
  48. return 1;
  49. }
  50.  
Success #stdin #stdout #stderr 0.33s 40784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int testWinner"
Execution halted