Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <math.h>
  4. #define PRECISION 0.0000001
  5. #define RANGESIZE 0.1
  6. #define DATA 0
  7. #define RESULT 1
  8. #define FINISH 2
  9. #define PI 3.14159265359
  10. //#define DEBUG
  11. struct moja
  12. {
  13. double range1;
  14. double range2;
  15. int dlakogo;
  16. int czyProcessed;
  17. }kolejka[100];
  18. int countKolejki = 0;
  19.  
  20. void wlozNaKolejke(double wartosc, double wartosc2, int proc)
  21. {
  22. kolejka[countKolejki].czyProcessed = 0;
  23. kolejka[countKolejki].range1 = wartosc;
  24. kolejka[countKolejki].range2 = wartosc2;
  25. kolejka[countKolejki].dlakogo = proc;
  26. countKolejki++;
  27. }
  28.  
  29. int getYourElement(int rank)
  30. {
  31. for (int i = 0; i < countKolejki + 1; i++)
  32. {
  33. if (kolejka[i].czyProcessed == 0 && kolejka[i].dlakogo == rank)
  34. {
  35. kolejka[i].czyProcessed = 1;
  36. return i;
  37. }
  38. }
  39. return -1;
  40. }
  41.  
  42. double f(double x) {
  43. return sin(x);
  44. }
  45.  
  46.  
  47. double SimpleIntegration(double a, double b) {
  48. double i;
  49. double sum = 0;
  50. int liczbaOdebranych = 0;
  51. for (i = a; i < b; i += PRECISION)
  52. sum += f(i)*PRECISION;
  53. return sum;
  54. }
  55. int main(int argc, char **argv) {
  56. int myrank, proccount;
  57. double a = 1, b = 10*PI;
  58. double range[2];
  59. double result = 0, resulttemp;
  60. int sentcount = 0;
  61. int receivedcount = 0;
  62. int i;
  63. MPI_Status status;
  64. MPI_Request r;
  65. // Initialize MPI
  66. MPI_Init(&argc, &argv);
  67. // find out my rank
  68. MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  69. // find out the number of processes in MPI_COMM_WORLD
  70. MPI_Comm_size(MPI_COMM_WORLD, &proccount);
  71. if (proccount < 2) {
  72. printf("Run with at least 2 processes");
  73. MPI_Finalize();
  74. return -1;
  75. }
  76. if (((b - a) / RANGESIZE) < 2 * (proccount - 1)) {
  77. printf("More subranges needed");
  78. MPI_Finalize();
  79. return -1;
  80. }
  81. // now the master will distribute the data and slave processes will perform computations
  82.  
  83. if (myrank == 0) {
  84. range[0] = a;
  85.  
  86. //moje
  87.  
  88. i = 0;
  89. do
  90. {
  91. i++;
  92. if (i == proccount)
  93. {
  94. i = 1;
  95. }
  96. range[1] = range[0] + RANGESIZE;
  97. MPI_Send(&range, 2, MPI_DOUBLE, i, DATA, MPI_COMM_WORLD);
  98. // printf("Wyslalem range %f,%f do nr: %d\n", range[0], range[1], i);
  99. wlozNaKolejke(range[0], range[1], i);
  100. sentcount++;
  101. range[0] = range[1];
  102. } while (range[1]+ RANGESIZE < b);
  103. range[0] = range[1];
  104. range[1] = b;
  105. MPI_Send(&range, 2, MPI_DOUBLE, i, DATA, MPI_COMM_WORLD);
  106. while (sentcount != receivedcount) {
  107. MPI_Recv(&resulttemp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, RESULT, MPI_COMM_WORLD, &status);
  108. receivedcount++;
  109. printf("\nMaster dostal wynik %f from process %d", resulttemp, status.MPI_SOURCE);
  110. result += resulttemp;
  111. }
  112.  
  113. // shut down the slaves
  114. for (i = 1; i < proccount; i++) {
  115. MPI_Send(NULL, 0, MPI_DOUBLE, i, FINISH, MPI_COMM_WORLD);
  116. }
  117. // now display the result
  118. printf("\n\n\n\n\n\n\nHi, I am process 0, the result is %f\n", result);
  119.  
  120. }
  121. else
  122. {
  123. double lastEndRange;
  124. double range3[3];
  125. int flag = 0;
  126. do {
  127. MPI_Iprobe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);
  128. MPI_Irecv(range3, 2, MPI_DOUBLE, 0, DATA, MPI_COMM_WORLD, &r);
  129. if (lastEndRange != range3[1])
  130. {
  131. // printf("Slave %d received subrange %f %f\n", myrank, range3[0], range3[1]);
  132. if(range3[0] !=0 && range3[1] != 0)
  133. wlozNaKolejke(range3[0], range3[1], myrank);
  134.  
  135. //licz dziwko
  136. int indeks = getYourElement(myrank);
  137. if (indeks != -1)
  138. {
  139. resulttemp = SimpleIntegration(kolejka[i].range1, kolejka[i].range2);
  140. printf("Slave %d sending result %f %f", myrank, range3[0], range3[1]);
  141. lastEndRange = range3[1];
  142. MPI_Isend(&resulttemp, 1, MPI_DOUBLE, 0, RESULT, MPI_COMM_WORLD, &r);
  143. }
  144. }
  145.  
  146. /* for (int i = 0; i<countKolejki; i++)
  147. {
  148. if (kolejka[i].dlakogo == myrank)
  149. {
  150. printf("Otrzymalem zadanie dla %d\n", myrank);
  151. }
  152. }*/
  153. } while (status.MPI_TAG != FINISH);
  154. /*
  155.  
  156. do {
  157. MPI_Iprobe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);
  158. if (status.MPI_TAG == DATA)
  159. {
  160. MPI_Irecv(range, 2, MPI_DOUBLE, 0, DATA, MPI_COMM_WORLD, &status);
  161. printf("Otrzymalem range %f,%f w nr: %d\n", range[0], range[1], myrank);
  162.  
  163. //wloz na kolejke
  164. //oblicz nastepny element ktory nie jest processed
  165.  
  166. //odeslij
  167.  
  168. // resulttemp = SimpleIntegration(range[0], range[1]);
  169. }
  170. }
  171. while (status.MPI_TAG != FINISH);
  172. */
  173. }
  174. MPI_Finalize();
  175. return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement