Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : dstw2.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello MPI World in C
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "mpi.h"
  13.  
  14. #define M_TAG 1
  15. #define PARENT_TAG 2
  16. #define ALREADY_TAG 3
  17.  
  18. void print_spanning_tree(int* spanningTree, int nnodes, int root, int depth) {
  19. int i;
  20.  
  21. for (i = 0; i < depth; ++i) {
  22. printf("\t");
  23. }
  24.  
  25. printf("%d\n", root);
  26.  
  27. for (i = 0; i < nnodes; ++i) {
  28. if (spanningTree[i] == root) {
  29. print_spanning_tree(spanningTree, nnodes, i, depth + 1);
  30. }
  31. }
  32. }
  33.  
  34. int main(int argc, char* argv[]){
  35. int nnodes = 7;
  36. /*
  37. int index[] = {2, 3, 4, 6};
  38. int edges[] = {1, 3, 0, 3, 0, 2};
  39. */
  40. int index[] = {3,7,11,14,15,17,18};
  41. int edges[] = {1,2,6,0,2,3,4,0,1,3,5,1,2,5,1,2,3,0};
  42.  
  43. int pr = 0;/* the root processing unit */
  44. int pi; /* rank of process */
  45. int np; /* number of processes */
  46. MPI_Comm G;
  47.  
  48. int parent = -1;
  49.  
  50. int i = 0;
  51. int terminate = 0;
  52.  
  53. int pj; /* rank of sender */
  54.  
  55. int tag = 0; /* tag for messages */
  56. MPI_Status status ; /* return status for receive */
  57.  
  58. /* start up MPI */
  59.  
  60. MPI_Init(&argc, &argv);
  61.  
  62. /* find out process rank */
  63. MPI_Comm_rank(MPI_COMM_WORLD, &pi);
  64.  
  65. /* find out number of processes */
  66. MPI_Comm_size(MPI_COMM_WORLD, &np);
  67. if (np != nnodes) {
  68. if (pi == 0) {
  69. printf("Use -np %d!\n", nnodes);
  70. }
  71. } else {
  72. MPI_Graph_create(MPI_COMM_WORLD, nnodes, index, edges, 0, &G);
  73.  
  74. int neighborsCount = 0;
  75. MPI_Graph_neighbors_count(G, pi, &neighborsCount);
  76.  
  77. int *neighbors = 0;
  78. int *children = 0;
  79. int *other = 0;
  80. if (neighborsCount != 0) {
  81. neighbors = (int *)malloc(neighborsCount * sizeof(int));
  82. MPI_Graph_neighbors(G, pi, neighborsCount, neighbors);
  83.  
  84. children = (int *)malloc(nnodes * sizeof(int));
  85. other = (int *)malloc(nnodes * sizeof(int));
  86.  
  87. for (i = 0; i < nnodes; ++i) {
  88. children[i] = -1;
  89. other[i] = -1;
  90. }
  91.  
  92. for (i = 0; i < neighborsCount; ++i) {
  93. children[neighbors[i]] = 0;
  94. other[neighbors[i]] = 0;
  95. }
  96. }
  97.  
  98. int true = 1;
  99. while (true) {
  100. switch (tag) {
  101. case 0:
  102. if (pi == pr && parent == -1) {
  103. parent = pi;
  104. if (neighborsCount > 0) {
  105. for (i = 0; i < neighborsCount; ++i) {
  106. MPI_Send(&pi, 1, MPI_INT, neighbors[i], M_TAG, G);
  107. }
  108. } else {
  109. true = 0;
  110. }
  111. }
  112. break;
  113. case M_TAG:
  114. if (parent == -1) {
  115. parent = pj;
  116. MPI_Send(&pi, 1, MPI_INT, parent, PARENT_TAG, G);
  117. if (neighborsCount > 1) {
  118. for (i = 0; i < neighborsCount; ++i) {
  119. if (neighbors[i] != parent)
  120. MPI_Send(&pi, 1, MPI_INT, neighbors[i], M_TAG, G);
  121. }
  122. } else {
  123. true = 0;
  124. }
  125. } else {
  126. MPI_Send(&pi, 1, MPI_INT, pj, ALREADY_TAG, G);
  127. }
  128. break;
  129. case PARENT_TAG:
  130. children[pj] = 1;
  131.  
  132. terminate = 1;
  133. for (i = 0; i < nnodes; ++i) {
  134. if (i != parent && children[i] == 0 && other[i] == 0) {
  135. terminate = 0;
  136. break;
  137. }
  138. }
  139.  
  140. if (terminate) {
  141. true = 0;
  142. }
  143.  
  144. break;
  145. case ALREADY_TAG:
  146. other[pj] = 1;
  147.  
  148. terminate = 1;
  149. for (i = 0; i < nnodes; ++i) {
  150. if (i != parent && children[i] == 0 && other[i] == 0) {
  151. terminate = 0;
  152. break;
  153. }
  154. }
  155.  
  156. if (terminate) {
  157. true = 0;
  158. }
  159.  
  160. break;
  161. }
  162.  
  163. if (true) {
  164. MPI_Recv(&pj, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, G, &status);
  165. tag = status.MPI_TAG;
  166. }
  167. }
  168. }
  169.  
  170. if (parent != -1) {
  171. if (pi == parent) {
  172.  
  173. int *spanningTree = (int *)malloc(nnodes * sizeof(int));
  174. for (i = 0; i < nnodes; ++i)
  175. {
  176. if (i != pi) {
  177. MPI_Recv(spanningTree + i, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  178. }
  179. }
  180.  
  181. print_spanning_tree(spanningTree, nnodes, pr, 0);
  182. }
  183. else
  184. MPI_Send(&parent, 1, MPI_INT, pr, 0, MPI_COMM_WORLD);
  185. }
  186.  
  187. /* shut down MPI */
  188. MPI_Finalize();
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement