SHARE
TWEET

Untitled

a guest Nov 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <mpi.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. /*
  6. void Circle(int rank, int size)
  7. {
  8.     if (rank == 0)
  9.     {
  10.         MPI_Status status;
  11.         int msg = 777;
  12.         MPI_Send(&msg, 1, MPI_INT, rank + 1, 1, MPI_COMM_WORLD);
  13.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, rank + 1);
  14.         MPI_Recv(&msg, 1, MPI_INT, size - 1, 1, MPI_COMM_WORLD, &status);
  15.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, size - 1);
  16.         msg++;
  17.         MPI_Send(&msg, 1, MPI_INT, rank + 1, 1, MPI_COMM_WORLD);
  18.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, rank + 1);
  19.         MPI_Recv(&msg, 1, MPI_INT, size - 1, 1, MPI_COMM_WORLD, &status);
  20.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, size - 1);
  21.     }
  22.  
  23.     if (rank == size - 1)
  24.     {
  25.         MPI_Status status;
  26.         int msg = 0;
  27.         MPI_Recv(&msg, 1, MPI_INT, rank - 1, 1, MPI_COMM_WORLD, &status);
  28.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, rank - 1);
  29.          msg++;
  30.         MPI_Send(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
  31.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, 0);
  32.  
  33.         MPI_Recv(&msg, 1, MPI_INT, rank - 1, 1, MPI_COMM_WORLD, &status);
  34.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, rank - 1);
  35.          msg++;
  36.         MPI_Send(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
  37.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, 0);
  38.     }
  39.  
  40.     if (rank > 0 && rank < size - 1)
  41.     {
  42.         MPI_Status status;
  43.         int msg = 0;
  44.         MPI_Recv(&msg, 1, MPI_INT, rank - 1, 1, MPI_COMM_WORLD, &status);
  45.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, rank - 1);
  46.         MPI_Send(&msg, 1, MPI_INT, rank + 1, 1, MPI_COMM_WORLD);
  47.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, rank + 1);
  48.  
  49.         MPI_Recv(&msg, 1, MPI_INT, rank - 1, 1, MPI_COMM_WORLD, &status);
  50.         printf("Processor #%d got message %d from processor #%d\n", rank, msg, rank - 1);
  51.         MPI_Send(&msg, 1, MPI_INT, rank + 1, 1, MPI_COMM_WORLD);
  52.         printf("Processor #%d send message %d to processor #%d\n", rank, msg, rank + 1);
  53.     }
  54. }*/
  55. int k =0;
  56. void Circle(int rank, int size){
  57.     k++;
  58.         if(rank==0){
  59.         int msg = 123;
  60.         MPI_Status st;
  61.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  62.         cout << "Process "<<  rank << " sends " << msg << "to proc " << rank +1 << "\n";
  63.         MPI_Recv(&msg, 1, MPI_INT, size -1, 1, MPI_COMM_WORLD, &st);
  64.         cout << "Process "<<  rank << "got " << msg << "with proc " << "5" << "\n";
  65.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  66.         cout << "Process "<<  rank << " sends " << msg << "to proc " << rank +1 << "\n";
  67.     }
  68.     if(rank>0 && rank<(size-1)){
  69.         int msg =0;
  70.             MPI_Status st;
  71.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  72.         msg++;
  73.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  74.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  75.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  76.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  77.         msg++;
  78.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  79.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  80.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  81.     }
  82.     /*if(rank==1){
  83.         int msg =0;
  84.             MPI_Status st;
  85.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  86.         msg++;
  87.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  88.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  89.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  90.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  91.         msg++;
  92.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  93.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  94.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  95.     }
  96.        if(rank==2){
  97.         int msg =0;
  98.         MPI_Status st;
  99.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  100.         msg++;
  101.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  102.         MPI_Send(&msg, 1, MPI_INT, rank+1 , 1, MPI_COMM_WORLD);
  103.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  104.                 MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  105.         msg++;
  106.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  107.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  108.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  109.     }
  110.         if(rank==3){
  111.         int msg =0;
  112.         MPI_Status st;
  113.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  114.         msg++;
  115.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  116.         MPI_Send(&msg, 1, MPI_INT, rank+1 , 1, MPI_COMM_WORLD);
  117.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  118.                 MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  119.         msg++;
  120.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  121.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  122.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  123.     }
  124.      if(rank==4){
  125.         int msg =0;
  126.         MPI_Status st;
  127.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  128.         msg++;
  129.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  130.         MPI_Send(&msg, 1, MPI_INT, rank+1 , 1, MPI_COMM_WORLD);
  131.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  132.                 MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  133.         msg++;
  134.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  135.         MPI_Send(&msg, 1, MPI_INT, rank + 1 , 1, MPI_COMM_WORLD);
  136.         cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  137.     }*/
  138.      if(rank==size -1){
  139.         int msg =0;
  140.         MPI_Status st;
  141.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  142.         msg++;
  143.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  144.        // if(k<2){
  145.             MPI_Send(&msg, 1, MPI_INT, 0 , 1, MPI_COMM_WORLD);
  146.             cout << "Process "<<  rank << "sends " << msg << "to proc " << rank+1 << "\n";
  147.         //}
  148.         MPI_Recv(&msg, 1, MPI_INT,  rank - 1, 1, MPI_COMM_WORLD, &st);
  149.         msg++;
  150.         cout << "Process "<<  rank << " got " << msg << "with proc " << rank-1 << "\n";
  151.     }
  152.     /*if(rank > 0 && rank < size-1){
  153.         int msg = 0;
  154.         MPI_Status st;
  155.     }*/
  156. }
  157.  
  158. int main(int argc, char *argv[])
  159. {
  160.     int COUNT=100;
  161.     MPI_Init(&argc, &argv);
  162.     int rank, size;
  163.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  164.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  165.  
  166.     cout << "rank "<< rank << " size is " << size << "\n";
  167.     Circle(rank, size);
  168.     MPI_Finalize();
  169.     return 0;
  170. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top