Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include<mpi.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include<stdio.h>
  5.  
  6. /**
  7. * @author cristian.chilipirea
  8. * Run: mpirun -np 4 ./a.out
  9. */
  10.  
  11. int graph[][2] = { { 0, 1 },
  12. { 1, 0 }, { 1, 2 }, { 1, 3 },
  13. { 2, 1 }, { 2, 3 },
  14. { 3, 1 }, { 3, 2 } };
  15.  
  16. void set(int *a, int c, int sz)
  17. {
  18. for (int i = 0; i < sz; ++ i) a[i] = c;
  19. }
  20.  
  21. int main(int argc, char * argv[]) {
  22. int rank;
  23. int nProcesses;
  24. int parent = -1;
  25. int i, j;
  26.  
  27. int topology[4];
  28. int null[4];
  29. int recv[4];
  30. set(recv, 0, 4);
  31. set(null, -1, 4);
  32. set(topology, -1, 4);
  33.  
  34.  
  35.  
  36. MPI_Init(&argc, &argv);
  37. MPI_Status status;
  38. MPI_Request request;
  39.  
  40.  
  41. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  42. MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
  43. printf("Hello from %i/%i\n", rank, nProcesses);
  44.  
  45. if (rank == 0)
  46. {
  47. for (i = 0; i < 8; ++ i)
  48. {
  49. if (graph[i][0] == rank)
  50. {
  51. MPI_Send(null, 4, MPI_INT, graph[i][1], 0, MPI_COMM_WORLD);
  52. }
  53. }
  54.  
  55. int temp_topo[4];
  56. for (i = 0; i < 8; ++ i)
  57. {
  58. set(temp_topo, -1, 4);
  59. if (graph[i][0] == rank)
  60. {
  61. MPI_Recv(temp_topo, 4, MPI_INT, graph[i][1], 0, MPI_COMM_WORLD, &status);
  62. }
  63. for (j = 0; j < 4; ++ j)
  64. {
  65. if (topology[j] == -1 && temp_topo[j] != -1) topology[j] = temp_topo[j];
  66. }
  67. }
  68.  
  69. for (j = 0; j < 4; ++ j)
  70. {
  71. printf("Found parent: %d for rank: %d\n", topology[j], j);
  72. }
  73. }
  74. else
  75. {
  76. int temp_topo[4];
  77. MPI_Recv(temp_topo, 4, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
  78. parent = status.MPI_SOURCE;
  79.  
  80. for (i = 0; i < 8; ++ i)
  81. {
  82. if (graph[i][0] == rank && graph[i][1] != parent)
  83. {
  84. MPI_Send(null, 4, MPI_INT, graph[i][1], 0, MPI_COMM_WORLD);
  85. }
  86. }
  87.  
  88. for (i = 0; i < 8; ++ i)
  89. {
  90. set(temp_topo, -1, 4);
  91. if (graph[i][0] == rank && graph[i][1] != parent)
  92. {
  93. MPI_Recv(temp_topo, 4, MPI_INT, graph[i][1], 0, MPI_COMM_WORLD, &status);
  94. }
  95. for (j = 0; j < 4; ++ j)
  96. {
  97. if (topology[j] == -1 && temp_topo[j] != -1) topology[j] = temp_topo[j];
  98. }
  99. }
  100. topology[rank] = parent;
  101.  
  102. MPI_Send(topology, 4, MPI_INT, parent, 0, MPI_COMM_WORLD);
  103. }
  104.  
  105. if (rank == 0)
  106. {
  107. int next = topology[3];
  108. while (topology[next] != rank) next = topology[next];
  109.  
  110. int vect[5];
  111. vect[0] = topology[0];
  112. vect[1] = topology[1];
  113. vect[2] = topology[2];
  114. vect[3] = topology[3];
  115. vect[4] = 123;
  116. MPI_Send(vect, 5, MPI_INT, next, 0, MPI_COMM_WORLD);
  117. }
  118. else if (rank != 3)
  119. {
  120. int vect[5];
  121. MPI_Recv(vect, 5, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
  122. printf("Rank %d received: %d\n", rank, vect[4]);
  123.  
  124. int next = vect[3];
  125. while (vect[next] != rank && next != rank && next != -1)
  126. {
  127. next = vect[next];
  128. }
  129. if (next == -1) next = parent;
  130.  
  131. if (next == rank)
  132. {
  133. MPI_Send(vect, 5, MPI_INT, 3, 0, MPI_COMM_WORLD);
  134. }
  135. else
  136. {
  137. MPI_Send(vect, 5, MPI_INT, next, 0, MPI_COMM_WORLD);
  138. }
  139. printf("Rank %d sent to: %d\n", rank, next);
  140. }
  141. else
  142. {
  143. int vect[5];
  144. MPI_Recv(vect, 5, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
  145. printf("Rank: %d received: %d\n", rank, vect[4]);
  146. }
  147.  
  148.  
  149. printf("Bye from %i/%i\n", rank, nProcesses);
  150. MPI_Finalize();
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement