fork download
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include "mpi.h"
  6.  
  7. #include <time.h>
  8.  
  9.  
  10.  
  11. int main(int argc, char *argv[]){
  12.  
  13. int npes, myrank;
  14.  
  15. MPI_Init(&argc, &argv);
  16.  
  17.  
  18.  
  19. MPI_Comm_size(MPI_COMM_WORLD, &npes);
  20.  
  21. MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. int playing = 1;
  30.  
  31. int winner = -1;
  32.  
  33. int coin_value;
  34.  
  35. int total_value = 0;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. while (1){
  44.  
  45. srand((unsigned int)(time(NULL) + myrank));
  46.  
  47. coin_value = rand() % 2;
  48.  
  49. MPI_Allreduce(&coin_value, &total_value, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
  50.  
  51. MPI_Barrier(MPI_COMM_WORLD);
  52.  
  53. printf("%d and %d and %d\n", myrank, coin_value, total_value);
  54.  
  55. if (total_value > 0){
  56.  
  57. if (coin_value == 0){
  58.  
  59. playing = 0;
  60.  
  61. break;
  62.  
  63. }
  64.  
  65. }else if(total_value == 1){
  66.  
  67. if (coin_value == 0){
  68.  
  69. playing = 0;
  70.  
  71. break;
  72.  
  73. }else {
  74.  
  75. // winner
  76.  
  77. break;
  78.  
  79. }
  80.  
  81. }
  82.  
  83. };
  84.  
  85.  
  86.  
  87. if (playing == 1){
  88.  
  89. winner = myrank;
  90.  
  91. MPI_Bcast(&winner, 1, MPI_INT, myrank, MPI_COMM_WORLD);
  92.  
  93. };
  94.  
  95.  
  96.  
  97. MPI_Barrier(MPI_COMM_WORLD);
  98.  
  99. printf("Sloužím ti %d, můj vládče, slunce naše jasné\n", winner);
  100.  
  101.  
  102.  
  103. MPI_Finalize();
  104.  
  105. return 0;
  106.  
  107. }
  108.  
  109.  
Success #stdin #stdout #stderr 0.25s 40848KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted