Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include<mpi.h>
  3. #include<math.h>
  4.  
  5. using namespace std;
  6.  
  7. void main(int argc, char *argv[])
  8. {
  9.     MPI_Init(&argc, &argv);
  10.     int startR;
  11.     int finishR;
  12.     int realN;
  13.     int M;
  14.     int tmp;
  15.     double *rbuf, *A;
  16.     int N, myRank, *r2buf, gsize, *myfinisharray;
  17.     int *myStady;
  18.     MPI_Status myStatus;
  19.     MPI_Status myStatus2;
  20.     MPI_Request myRequest;
  21.     MPI_Request *myRequest2;
  22.     MPI_Request *myRequest3;
  23.     MPI_Request myRequest4;
  24.     MPI_Comm_size(MPI_COMM_WORLD, &gsize);
  25.     MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
  26.     myRequest3 = new MPI_Request[gsize];
  27.     myStady = new int[gsize];
  28.     int i = 0;
  29.     if (myRank == 0)
  30.     {
  31.         srand(INT_MAX);
  32.         cout << "Size of array: " << endl;
  33.         cin >> N;
  34.         cout << "Low boundary: " << endl;
  35.         cin >> startR;
  36.         cout << "High boundary: " << endl;
  37.         cin >> finishR;
  38.         cout << "Magic number: " << endl;
  39.         cin >> M;
  40.         A = new double[N];
  41.         for (int i = 0; i < N; i++)
  42.         {
  43.             A[i] = startR + rand() % (finishR - startR+1);
  44.         }
  45.         A[7] = M;
  46.     }
  47.     else
  48.     {
  49.         A = NULL;
  50.         myfinisharray = NULL;
  51.     }
  52.     MPI_Bcast(&M, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  53.     MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
  54.     int count = N / gsize;
  55.     cout << count << endl;
  56.     rbuf = new double[count];
  57.     MPI_Scatter(A, count, MPI_DOUBLE, rbuf, count, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  58.     myRequest2 = new MPI_Request[gsize];   
  59.     int code = -1;
  60.     MPI_Irecv(&tmp, 1, MPI_INT, MPI_ANY_SOURCE, 100, MPI_COMM_WORLD, &myRequest);
  61.     for (int k = 0; k < gsize; k++)
  62.     {
  63.         MPI_Send_init(&code, 1, MPI_INT, k, 100, MPI_COMM_WORLD, &myRequest2[k]);
  64.     }
  65.     int isMagic = 0;
  66.     MPI_Barrier(MPI_COMM_WORLD);
  67.     for (i = 0; i < count; i++)
  68.     {
  69.         if (rbuf[i] == M)
  70.         {
  71.             MPI_Startall(gsize, myRequest2);
  72.         }
  73.         MPI_Test(&myRequest, &isMagic, &myStatus2);
  74.         if (isMagic == 1)
  75.         {
  76.             break;
  77.         }
  78.        
  79.         rbuf[i] = 2 * rbuf[i];//sin(rbuf[i] - M)* exp(rbuf[i]) - log(cos(rbuf[i]));
  80.     }
  81.     MPI_Barrier(MPI_COMM_WORLD);
  82.     MPI_Test(&myRequest, &isMagic, &myStatus2);
  83.     if (isMagic)
  84.     {
  85.         cout << "i = " << i << ", MyRank = " << myRank << endl;
  86.         MPI_Isend(&i, 1, MPI_INT, 0, 200, MPI_COMM_WORLD, &myRequest4);
  87.         if (myRank == 0)
  88.         {
  89.             for (int j = 0; j < gsize; j++)
  90.             {
  91.                 MPI_Recv(&myStady[j], 1, MPI_INT, j, 200, MPI_COMM_WORLD, &myStatus);
  92.             }
  93.             for (int i = 0; i < gsize; i++)
  94.             {
  95.                 cout << myStady[i] << "  ";
  96.             }
  97.             cout << endl;
  98.         }
  99.     }
  100.    
  101.    
  102.     if (isMagic == 0)
  103.     {
  104.         MPI_Barrier(MPI_COMM_WORLD);
  105.         cout << "Something" << endl;
  106.         MPI_Gather(rbuf, count, MPI_DOUBLE, A + myRank*count, count, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  107.         if (myRank == 0)
  108.         {
  109.             for (int i = 0; i < N; i++)
  110.             {
  111.                 cout << A[i] << "  ";
  112.             }
  113.             cout << endl;
  114.         }
  115.     }
  116.     MPI_Finalize();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement