Advertisement
superjorj

Untitled

Dec 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "mpi.h"
  3.  
  4. int main(int argc, char* argv[]) {
  5. int myrank, count;
  6. MPI_Status status;
  7. MPI_Comm graph;
  8.  
  9. int i, j;
  10. int id_max, id_vecin;
  11. char* stare = "";
  12. int diametru = 3;
  13. int nnodes = 9;
  14. int index[] = { 4, 6, 9, 12, 15, 18, 21, 25, 28 };
  15. int edges[] = { 1, 4, 5, 6, 0, 6, 4, 7, 8, 5, 6, 8, 0, 2, 7, 0, 3, 7, 0, 1, 3, 2, 4, 5, 8, 2, 3, 7 };
  16.  
  17. int neighbours[10];
  18. int nneighbours;
  19.  
  20. MPI_Init(&argc, &argv);
  21. MPI_Comm_size(MPI_COMM_WORLD, &count);
  22. MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  23.  
  24. MPI_Graph_create(MPI_COMM_WORLD,nnodes,index,edges,0,&graph);
  25. MPI_Graph_neighbors_count(graph,myrank,&nneighbours);
  26. MPI_Graph_neighbors(graph,myrank,nneighbours,neighbours);
  27.  
  28.  
  29. id_max=myrank;
  30. for(i=1;i<=diametru;i++)
  31. {
  32. for(j=0;j<nneighbours;j++)
  33. {
  34. MPI_Send(&id_max,1,MPI_INT,neighbours[j],1,graph);
  35. }
  36. for(j=0;j<nneighbours;j++)
  37. {
  38. MPI_Recv(&id_vecin,1,MPI_INT,neighbours[j],1,graph,&status);
  39. if(id_max<id_vecin)
  40. id_max=id_vecin;
  41. }
  42. MPI_Barrier(graph);
  43. }
  44.  
  45. if(id_max==myrank)
  46. stare="Lider";
  47. else
  48. stare="Non-lider";
  49.  
  50. printf("Rank = %d : Status = %s\n",myrank, stare);
  51.  
  52. MPI_Finalize();
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement