Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. /**
  6. * @author cristian.chilipirea
  7. * Run: mpirun -np 12 ./a.out
  8. */
  9.  
  10. int graph[][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 11}, {11, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {10, 9}, {9, 8}, {8, 7}, {7, 6}, {6, 5}, {5, 11}, {11, 4}, {4, 3}, {3, 2}, {2, 1}, {1, 0}, {9, 5}, {5, 9}, {5, 3}, {3, 5}, {0, 2}, {2, 0}, {9, 7}, {7, 9}};
  11.  
  12. int *getNeighbors(int myRank)
  13. {
  14. int *neigh = malloc(sizeof(int) * 100);
  15. int i;
  16. int j = 1;
  17. for (i = 0; i < 30; i++)
  18. {
  19. if (graph[i][0] == myRank)
  20. {
  21. neigh[j] = graph[i][1];
  22. j++;
  23. }
  24. }
  25. neigh[0] = j;
  26. return neigh;
  27. }
  28.  
  29. int *getLocalTopology(int myRank, int *neigh)
  30. {
  31. int *topology = malloc(sizeof(int) * 100);
  32. int i;
  33. int j = 1;
  34. for (i = 1; i < neigh[0]; i++)
  35. {
  36. topology[2 * j] = myRank;
  37. topology[2 * j + 1] = neigh[i];
  38. j++;
  39. }
  40. topology[0] = j;
  41. return topology;
  42. }
  43.  
  44. int IsSame(int *vec1, int *vec2)
  45. {
  46.  
  47. if (vec1[0] == vec2[0] && vec1[1] == vec2[1])
  48. {
  49. return 1;
  50. }
  51. else if (vec1[1] == vec2[0] && vec1[0] == vec2[1])
  52. {
  53. return 1;
  54. }
  55. return 0;
  56. }
  57.  
  58. void Concat(int *Top1, int *Top2)
  59. {
  60. int count = Top1[0];
  61. int bool;
  62.  
  63. for (int i = 1; i < Top2[0]; i++)
  64. {
  65. bool = 0;
  66.  
  67. for (int j = 1; j < Top1[0]; j++)
  68. {
  69. if (IsSame(Top2 + (2 * i), Top1 + (2 * j)))
  70. {
  71. bool = 1;
  72. }
  73. }
  74. if (bool == 0)
  75. {
  76. Top1[2 * count] = Top2[2 * i];
  77. Top1[2 * count + 1] = Top2[2 * i + 1];
  78. count++;
  79. }
  80. }
  81.  
  82. Top1[0] = count;
  83. }
  84.  
  85. int main(int argc, char *argv[])
  86. {
  87. int rank;
  88. int nProcesses;
  89.  
  90. MPI_Init(&argc, &argv);
  91. MPI_Status status;
  92. MPI_Request request;
  93.  
  94. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  95. MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
  96. printf("Hello from %i/%i\n", rank, nProcesses);
  97.  
  98. int *neigh = getNeighbors(rank);
  99. int i;
  100. char aux[1000];
  101. aux[0] = 0;
  102. sprintf(aux + strlen(aux), "Rank %i neighbors: ", rank);
  103. for (i = 1; i < neigh[0]; i++)
  104. {
  105. sprintf(aux + strlen(aux), " %i,", neigh[i]);
  106. }
  107. sprintf(aux + strlen(aux) - 1, "\n");
  108. printf("%s", aux);
  109.  
  110. // YOU ARE ONLY ALLOWED TO COMMUNICATE WITH NEIGHBOR PROCESSES
  111. // The goal of the first exercise is to pretend you don't have graph and rebuild it in a distributed manner
  112. // The goal of the second exercise is to build routing tables. Routing tables are a list of destination + next hope.
  113. // Routing table for 5 is:
  114. // 0 -> 3 ; 1 -> 3 ; 2 -> 3 ; 3 -> 3 ; 4 -> 3 ; 5 -> 5 ; 6 -> 6 ; 7 -> 6 ; 8 -> 9 ; 9 -> 9 ; 10 -> 9 ; 11 -> 11
  115.  
  116. int *LocTop = getLocalTopology(rank, neigh);
  117.  
  118. for (int i = 0; i < 100; i++)
  119. {
  120. for (int j = 1; j < neigh[0]; j++)
  121. {
  122. MPI_Send(LocTop, 100, MPI_INT, neigh[j], 0, MPI_COMM_WORLD);
  123. }
  124. for (int j = 1; j < neigh[0]; j++)
  125. {
  126. int *NewTop = (int *)malloc(100 * sizeof(int));
  127. MPI_Recv(NewTop, 100, MPI_INT, neigh[j], 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  128. Concat(LocTop, NewTop);
  129. }
  130. }
  131.  
  132. printf("Bye from %i/%i\n", rank, nProcesses);
  133.  
  134. if (rank == 0)
  135. {
  136. for (i = 1; i < LocTop[0]; i++)
  137. {
  138. printf("%d -> %d\n", LocTop[2 * i], LocTop[2 * i + 1]);
  139. }
  140. }
  141.  
  142. int Parent[12] = {-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  143.  
  144. int *Q = malloc(12 * sizeof(int));
  145. Q[0] = 0;
  146. int T = 1;
  147. int H = 0;
  148.  
  149. while (H < T)
  150. {
  151. int *neigh = getNeighbors(Q[H]);
  152.  
  153. for (int i = 1; i < neigh[0]; i++)
  154. {
  155. if (Parent[neigh[i]] == -1)
  156. {
  157. Parent[neigh[i]] = Q[H];
  158. Q[T] = neigh[i];
  159. T++;
  160. }
  161. }
  162. H++;
  163. }
  164.  
  165. int *Parent2 = malloc(12 * sizeof(int));
  166. for (int i = 0; i < 12; i++)
  167. {
  168. Parent2[i] = -1;
  169. }
  170.  
  171. int num;
  172. if (rank == 0)
  173. {
  174. for (int i = 0; i < 12; i++)
  175. {
  176. printf("%d ", Parent[i]);
  177. }
  178. printf("\n");
  179. num = 22;
  180.  
  181. int find = 10;
  182.  
  183. while (find != 0)
  184. {
  185. Parent2[find] = Parent[find];
  186. find = Parent[find];
  187. }
  188. for (int i = 0; i < 12; i++)
  189. {
  190. printf("%d ", Parent2[i]);
  191. }
  192. printf("\n");
  193. }
  194.  
  195. MPI_Bcast(Parent2, 12, MPI_INT, 0, MPI_COMM_WORLD);
  196.  
  197. if (rank == 0)
  198. {
  199. int index;
  200. for (int i = 0; i < 12; i++)
  201. {
  202. if (Parent2[i] == 0)
  203. {
  204. index = i;
  205. }
  206. }
  207.  
  208. printf("Eu sunt %d si ii trimit lui %d\n", rank, index);
  209. MPI_Send(&num, 1, MPI_INT, index, 0, MPI_COMM_WORLD);
  210. }
  211.  
  212. if (Parent2[rank] != -1)
  213. {
  214. int index;
  215. for (int i = 0; i < 12; i++)
  216. {
  217. if (Parent2[i] == rank)
  218. {
  219. index = i;
  220. }
  221. }
  222.  
  223. printf("Eu sunt %d si am primit de la %d\n", rank, Parent2[rank]);
  224.  
  225. MPI_Recv(&num, 1, MPI_INT, Parent2[rank], 0, MPI_COMM_WORLD, NULL);
  226. if (rank != 10)
  227. {
  228. printf("Eu sunt %d si ii trimit lui %d\n", rank, index);
  229. MPI_Send(&num, 1, MPI_INT, index, 0, MPI_COMM_WORLD);
  230. }
  231. }
  232.  
  233. if (rank == 10)
  234. {
  235. printf("%d: %d\n", rank, num);
  236. }
  237.  
  238. MPI_Finalize();
  239. return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement