Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "mpi.h"
- int main(int argc, char* argv[]) {
- int myrank, count;
- MPI_Status status;
- MPI_Comm graph;
- int i, j;
- int id_max, id_vecin;
- char* stare = "";
- int diametru = 3;
- int nnodes = 9;
- int index[] = { 4, 6, 9, 12, 15, 18, 21, 25, 28 };
- 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 };
- int neighbours[10];
- int nneighbours;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &count);
- MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
- MPI_Graph_create(MPI_COMM_WORLD,nnodes,index,edges,0,&graph);
- MPI_Graph_neighbors_count(graph,myrank,&nneighbours);
- MPI_Graph_neighbors(graph,myrank,nneighbours,neighbours);
- id_max=myrank;
- for(i=1;i<=diametru;i++)
- {
- for(j=0;j<nneighbours;j++)
- {
- MPI_Send(&id_max,1,MPI_INT,neighbours[j],1,graph);
- }
- for(j=0;j<nneighbours;j++)
- {
- MPI_Recv(&id_vecin,1,MPI_INT,neighbours[j],1,graph,&status);
- if(id_max<id_vecin)
- id_max=id_vecin;
- }
- MPI_Barrier(graph);
- }
- if(id_max==myrank)
- stare="Lider";
- else
- stare="Non-lider";
- printf("Rank = %d : Status = %s\n",myrank, stare);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement