Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1.  
  2. #include <mpi.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7.  
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. int N = atoi(argv[1]); // number of items each worker should generate
  12. int H = atoi(argv[2]); // highest value, the range 1:H
  13.  
  14. //0 shares N & H with all workers
  15. //less than 20 workers in all cases
  16.  
  17. //workers each generate a list of N values from 1 to H
  18. //0 gathers these lists into a single BigList
  19.  
  20. //0 shares BigList with all workers
  21.  
  22. //workers each computer local sum, max, min for their data
  23. //boss collects sum, max, min to find global values
  24.  
  25. //boss reports global values
  26. MPI_Init(NULL, NULL);
  27.  
  28. // Find out rank, size
  29. int my_rank;
  30. int world_size;
  31. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  32. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  33. MPI_Status status;
  34. /////////////////////////////////////////////
  35. int NHArray[2];
  36. NHArray[0] =N;
  37. NHArray[1] = H;
  38. int list[N];
  39. int calcs[3];
  40. int globalSum = 0;
  41. int globalMax = 1;
  42. int globalMin = H;
  43.  
  44. int bigN =(N * (world_size - 1));
  45. int bigList[bigN];
  46.  
  47. if (my_rank == 0)
  48. {
  49. int offset;
  50. MPI_Bcast(NHArray, 2, MPI_INT, 0, MPI_COMM_WORLD);
  51. printf("Broadcasted from 0\n");
  52.  
  53. for(int i = 1; i < world_size; i++)
  54. {
  55. MPI_Recv(list, N, MPI_INT, i , 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  56. for(int j = 0; j < N; j++)
  57. {
  58. bigList[j + offset] = list[j];
  59. }
  60. offset += N;
  61. }
  62. printf("Recv from 0\n");
  63.  
  64. MPI_Bcast(bigList, bigN, MPI_INT, 0, MPI_COMM_WORLD);
  65. printf("Broadcasted again from 0\n");
  66.  
  67. for(int k = 1; k < world_size; k++)
  68. {
  69. MPI_Recv(calcs, 3, MPI_INT, k, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  70.  
  71. globalSum += calcs[0];
  72. if(calcs[1] > globalMax)
  73. {
  74. globalMax = calcs[1];
  75. }
  76.  
  77. if(calcs[2] < globalMin)
  78. {
  79. globalMin = calcs[2];
  80. }
  81.  
  82. }
  83. printf("Calculations done in 0\n");
  84.  
  85. printf("Global Sum: %d\n", globalSum);
  86. printf("Global Min: %d\n", globalMin);
  87. printf("Global Max: %d\n", globalMax);
  88. }
  89. else
  90. {
  91. int offsetW = 0;
  92. int sum = 0;
  93. int max = 1;
  94. int min = H;
  95.  
  96. MPI_Bcast(&NHArray, 2, MPI_INT, 0, MPI_COMM_WORLD);
  97. printf("Bcast send from worker \n");
  98.  
  99. for(int i = 0; i < N; i++)
  100. {
  101. list[i] = rand() %H + 1;
  102. }
  103.  
  104. MPI_Send(list, N, MPI_INT, 0, 0, MPI_COMM_WORLD);
  105. printf("Send list from worker \n");
  106.  
  107. MPI_Bcast(bigList, bigN, MPI_INT, 0, MPI_COMM_WORLD);
  108. printf("Bcast from worker\n");
  109.  
  110. for (int j = 0; j < N; j++)
  111. {
  112. sum += bigList[j + offsetW];
  113. if (bigList[j + offsetW] > max)
  114. {
  115. max = bigList[j + offsetW];
  116.  
  117. }
  118.  
  119. if(bigList[j + offsetW] < min)
  120. {
  121. min = bigList[j + offsetW];
  122. }
  123. }
  124.  
  125. offsetW += N;
  126. calcs[0] = sum;
  127. calcs[1] = max;
  128. calcs[2] = min;
  129.  
  130. MPI_Send(calcs, 3, MPI_INT, 0,0,MPI_COMM_WORLD);
  131.  
  132. printf("Sending from worker\n");
  133.  
  134. }
  135. ////////////////////////////////////////////
  136. MPI_Finalize();
  137. printf("Finalize");
  138. return 0;
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement