Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int size, rank, rc;
- int ers=0, i, k, nNeighbour, sum=0;
- int index[] = {4,5,7,8,11,12,14,15,19,20,22,23,26,27,29,30};
- int edges[] = {1,2,4,8,0,0,3,2,0,5,6,4,4,7,6,0,9,10,12,8,8,11,10,8,13,14,12,12,15,14};
- MPI_Status status;
- rc = MPI_Init (&argc, &argv);
- MPI_Comm comm1, comm2;
- MPI_Comm_rank (MPI_COMM_WORLD, &rank);
- MPI_Comm_size (MPI_COMM_WORLD, &size);
- if(!index || !edges) {
- printf("unable to allocate %d words for index or edges\n", 3*size);
- fflush(stdout);
- MPI_Abort(MPI_COMM_WORLD, 1);
- }
- MPI_Graph_create(MPI_COMM_WORLD, size, index, edges, 0, &comm1);
- srand(rank);
- int a=rand()%100, tmp;
- sum+=a;
- MPI_Graph_neighbors_count(comm1, rank, &nNeighbour);
- int *neighbours;
- neighbours = (int*)malloc(nNeighbour * sizeof(int));
- MPI_Graph_neighbors(comm1, rank, nNeighbour, neighbours);
- if(nNeighbour > 1 && rank != 0) {
- for(i=1; i<nNeighbour; i++) {
- MPI_Recv(&tmp, 1, MPI_INTEGER, neighbours[i], neighbours[i], MPI_COMM_WORLD, &status);
- sum+=tmp;
- }
- }
- if(rank == 0) {
- for(i=0; i<nNeighbour; i++) {
- MPI_Recv(&tmp, 1, MPI_INTEGER, neighbours[i], neighbours[i], MPI_COMM_WORLD, &status);
- sum+=tmp;
- }
- printf("rank %d ha numero %d la somma totale e' %d \n",rank, a, sum);
- }
- else {
- MPI_Send(&sum, 1, MPI_INTEGER, neighbours[0], rank, MPI_COMM_WORLD);
- printf("rank %d ha numero %d la somma parziale e' %d \n", rank, a, sum);
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement