Advertisement
Guest User

Untitled

a guest
May 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1.     int size, rank, rc;
  2.     int ers=0, i, k, nNeighbour, sum=0;
  3.     int index[] = {4,5,7,8,11,12,14,15,19,20,22,23,26,27,29,30};
  4.     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};
  5.     MPI_Status status;
  6.     rc = MPI_Init (&argc, &argv);
  7.     MPI_Comm comm1, comm2;
  8.     MPI_Comm_rank (MPI_COMM_WORLD, &rank);
  9.     MPI_Comm_size (MPI_COMM_WORLD, &size);
  10.  
  11.     if(!index || !edges) {
  12.         printf("unable to allocate %d words for index or edges\n", 3*size);
  13.         fflush(stdout);
  14.         MPI_Abort(MPI_COMM_WORLD, 1);
  15.     }
  16.  
  17.     MPI_Graph_create(MPI_COMM_WORLD, size, index, edges, 0, &comm1);
  18.  
  19.     srand(rank);
  20.     int a=rand()%100, tmp;
  21.     sum+=a;
  22.     MPI_Graph_neighbors_count(comm1, rank, &nNeighbour);
  23.     int *neighbours;
  24.     neighbours = (int*)malloc(nNeighbour * sizeof(int));
  25.     MPI_Graph_neighbors(comm1, rank, nNeighbour, neighbours);
  26.  
  27.     if(nNeighbour > 1 && rank != 0) {
  28.         for(i=1; i<nNeighbour; i++) {
  29.             MPI_Recv(&tmp, 1, MPI_INTEGER, neighbours[i], neighbours[i], MPI_COMM_WORLD, &status);
  30.             sum+=tmp;
  31.         }
  32.     }
  33.     if(rank == 0) {
  34.         for(i=0; i<nNeighbour; i++) {
  35.             MPI_Recv(&tmp, 1, MPI_INTEGER, neighbours[i], neighbours[i], MPI_COMM_WORLD, &status);
  36.             sum+=tmp;
  37.         }
  38.  
  39.         printf("rank %d ha numero %d la somma totale e' %d \n",rank, a, sum);
  40.     }
  41.     else {
  42.         MPI_Send(&sum, 1, MPI_INTEGER, neighbours[0], rank, MPI_COMM_WORLD);
  43.         printf("rank %d ha numero %d la somma parziale e' %d \n", rank, a, sum);
  44.     }
  45.  
  46.     MPI_Finalize();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement