Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<mpi.h>
- #include<math.h>
- using namespace std;
- void main(int argc, char *argv[])
- {
- MPI_Init(&argc, &argv);
- int startR;
- int finishR;
- int realN;
- int M;
- int tmp;
- double *rbuf, *A;
- int N, myRank, *r2buf, gsize, *myfinisharray;
- int *myStady;
- MPI_Status myStatus;
- MPI_Status myStatus2;
- MPI_Request myRequest;
- MPI_Request *myRequest2;
- MPI_Request *myRequest3;
- MPI_Request myRequest4;
- MPI_Comm_size(MPI_COMM_WORLD, &gsize);
- MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
- myRequest3 = new MPI_Request[gsize];
- myStady = new int[gsize];
- int i = 0;
- if (myRank == 0)
- {
- srand(INT_MAX);
- cout << "Size of array: " << endl;
- cin >> N;
- cout << "Low boundary: " << endl;
- cin >> startR;
- cout << "High boundary: " << endl;
- cin >> finishR;
- cout << "Magic number: " << endl;
- cin >> M;
- A = new double[N];
- for (int i = 0; i < N; i++)
- {
- A[i] = startR + rand() % (finishR - startR+1);
- }
- A[7] = M;
- }
- else
- {
- A = NULL;
- myfinisharray = NULL;
- }
- MPI_Bcast(&M, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
- MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
- int count = N / gsize;
- cout << count << endl;
- rbuf = new double[count];
- MPI_Scatter(A, count, MPI_DOUBLE, rbuf, count, MPI_DOUBLE, 0, MPI_COMM_WORLD);
- myRequest2 = new MPI_Request[gsize];
- int code = -1;
- MPI_Irecv(&tmp, 1, MPI_INT, MPI_ANY_SOURCE, 100, MPI_COMM_WORLD, &myRequest);
- for (int k = 0; k < gsize; k++)
- {
- MPI_Send_init(&code, 1, MPI_INT, k, 100, MPI_COMM_WORLD, &myRequest2[k]);
- }
- int isMagic = 0;
- MPI_Barrier(MPI_COMM_WORLD);
- for (i = 0; i < count; i++)
- {
- if (rbuf[i] == M)
- {
- MPI_Startall(gsize, myRequest2);
- }
- MPI_Test(&myRequest, &isMagic, &myStatus2);
- if (isMagic == 1)
- {
- break;
- }
- rbuf[i] = 2 * rbuf[i];//sin(rbuf[i] - M)* exp(rbuf[i]) - log(cos(rbuf[i]));
- }
- MPI_Barrier(MPI_COMM_WORLD);
- MPI_Test(&myRequest, &isMagic, &myStatus2);
- if (isMagic)
- {
- cout << "i = " << i << ", MyRank = " << myRank << endl;
- MPI_Isend(&i, 1, MPI_INT, 0, 200, MPI_COMM_WORLD, &myRequest4);
- if (myRank == 0)
- {
- for (int j = 0; j < gsize; j++)
- {
- MPI_Recv(&myStady[j], 1, MPI_INT, j, 200, MPI_COMM_WORLD, &myStatus);
- }
- for (int i = 0; i < gsize; i++)
- {
- cout << myStady[i] << " ";
- }
- cout << endl;
- }
- }
- if (isMagic == 0)
- {
- MPI_Barrier(MPI_COMM_WORLD);
- cout << "Something" << endl;
- MPI_Gather(rbuf, count, MPI_DOUBLE, A + myRank*count, count, MPI_DOUBLE, 0, MPI_COMM_WORLD);
- if (myRank == 0)
- {
- for (int i = 0; i < N; i++)
- {
- cout << A[i] << " ";
- }
- cout << endl;
- }
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement