Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. //////////////////////////
  2. // Неделимое оповещение //
  3. //////////////////////////
  4.  
  5. // TO-DO
  6. // вывод информации
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <mpi.h>
  11. #include <math.h>
  12.  
  13. #define n 4 // число получателей
  14. #define b 4 // число ячеек
  15. #define m 3 // число сообшений
  16. #define iters 5 //число итераций
  17.  
  18. /*
  19. typedef struct cell
  20. {
  21. int data;
  22. struct cell* next;
  23. } cell;
  24.  
  25. */
  26.  
  27. int main (int argc, char ** argv){
  28. //шапочка
  29. int size, rank;
  30. if (MPI_Init (&argc, &argv) != MPI_SUCCESS) return 1;
  31. if (MPI_Comm_size(MPI_COMM_WORLD, &size) != MPI_SUCCESS)
  32. {MPI_Finalize();
  33. return 2;
  34. }
  35. if (MPI_Comm_rank (MPI_COMM_WORLD, &rank) != MPI_SUCCESS)
  36. {MPI_Finalize();
  37. return 3;
  38. }
  39. //конец шапочки
  40.  
  41.  
  42. //int cells[4] = {5,5,5,5};
  43.  
  44. //в main
  45.  
  46. /*
  47. FILE *f;
  48. f = fopen("result", "w");
  49. if (!rank)
  50. {
  51. fprintf(f, "jweofjweoifj");
  52. };
  53.  
  54. */
  55.  
  56.  
  57.  
  58. /*
  59. cell * msg_cells[4];
  60.  
  61. msg_cells[3] = new struct cell;
  62. msg_cells[3] -> data = 4; msg_cells[3] -> next = NULL;
  63. msg_cells[2] = new struct cell;
  64. msg_cells[2] -> data = 4; msg_cells[2] -> next = msg_cells[3];
  65. msg_cells[1] = new struct cell;
  66. msg_cells[1] -> data = 4; msg_cells[1] -> next = msg_cells[2];
  67. msg_cells[0] = new struct cell;
  68. msg_cells[0] -> data = 4; msg_cells[0] -> next = msg_cells[1];
  69. //если закольцевать
  70. //d -> next = a;
  71. */
  72. ////////////// ИНИЦИАЛИЗАЦИЯ!!!! ////////////////////////////
  73. int current[4] = {0,0,0,0};
  74. int cells[4] = {4,4,4,4};
  75.  
  76. int k;
  77. int sender;
  78.  
  79. MPI_Status status;
  80.  
  81. int i,j;
  82. double a;
  83. int temp[1];
  84. /////////////////////////////////////////////////////////////
  85.  
  86.  
  87. //тулово
  88. for (i = 0; i < iters; i++)
  89. {
  90.  
  91. printf("iteration: %d \n", i); //выводим номер итерации
  92. if (!rank) //процесс-производитель
  93. {
  94. for (j = 0; j < b; j++) // пробегаем по ячейкам
  95. {
  96. // ПЕРЕДЕЛАТЬ ПОД ЛИНЕЙНЫЙ СПИСОК !!!! X_X
  97. if (!cells[j] /*(msg_cells[j] -> data)*/) // если ячейка пуста
  98. {
  99. /*msg_cells[j] -> data = 4;*/ // добавляем сообщения в ячейку
  100. cells[j] = 4;
  101. printf(" add messages to: %d \n", j);
  102. };
  103. };
  104.  
  105. for (k = 0; k < n; k++) // рассылаем сообщения
  106. {
  107. printf("Sending message to %d \n", k+1);
  108. MPI_Send(&current[k], 1, MPI_INT, k+1, 0, MPI_COMM_WORLD);
  109. };
  110.  
  111. // получаем сообщения от потребителей
  112. MPI_Recv(&sender, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  113. /*
  114. msg_cells[current[sender-1]] -> data -= 1; // забираем 1 сообшение из ячейки
  115. */
  116. cells[current[sender-1]] -= 1;
  117. current[sender-1] = (current[sender-1] + 1) % n; // меняем номер текущей ячейки для потребителя
  118. printf(" consumer: %d", (sender));
  119. printf(" next cell: %d \n", current[sender-1]);
  120. }
  121.  
  122. else //процессы-потребители
  123.  
  124. {
  125. printf(" %d is receiving message \n", rank);
  126. MPI_Recv(temp, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); //получаем сообщение от производителя
  127. printf(" %d has received message \n", rank);
  128.  
  129. for (k = 0; k<10; k++)
  130. {
  131. a = cos(k); //считаем косинус
  132. };
  133.  
  134. MPI_Send(&rank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // отправляем сообщение о завершении
  135.  
  136. for (k = 0; k<11; k++)
  137. {
  138. a = cos(k); //считаем косинус
  139. };
  140.  
  141. printf(" %d has finished calculations \n", rank);
  142. }
  143. MPI_Barrier(MPI_COMM_WORLD);
  144. };
  145.  
  146. //тапочки
  147. // fclose(f);
  148. MPI_Finalize();
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement