Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <mpi.h>
- #include <time.h>
- using std::cout;
- using std::cin;
- using namespace std;
- int mpi_Gather(int* send_data, int send_count, MPI_Datatype send_datatype, int* recv_data, int recv_count, MPI_Datatype recv_datatype, int root, MPI_Comm communicator)
- {
- int myid, numprocs;
- int* tmp = new int[send_count];
- MPI_Status status;
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
- int index = 0;
- if (myid != root)
- {
- MPI_Send(send_data, send_count, MPI_INT, root, 99, MPI_COMM_WORLD);
- }
- else
- {
- for (int i = 0; i < send_count; i++)
- {
- recv_data[index] = send_data[i];
- index++;
- }
- for (int i = 1; i < numprocs; i++)
- {
- MPI_Recv(tmp, recv_count, MPI_INT, i, 99, MPI_COMM_WORLD, &status);
- for (int j = 0; j < send_count; j++)
- {
- recv_data[index] = *tmp;
- *(++tmp);
- index++;
- }
- }
- }
- return 0;
- }
- int main(int argc, char **argv) {
- int size, rank;
- int rcvData1;
- int rcvData2;
- int *global_array = NULL;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- srand(time(NULL));
- int a = (size -1)/2;
- int b = a+1;
- int tableSize = a;
- //initialization of global_array in the memory of process 0
- if (rank == 0) {
- global_array = new int[tableSize];
- for (int i = 0; i < tableSize; i++) {
- global_array[i] = rand() % 100 + 1;
- int liczba = global_array[i];
- if (i == 0) {
- continue;
- }
- MPI_Send(&liczba, 1, MPI_INT, i, 99, MPI_COMM_WORLD);
- }
- rcvData1 = global_array[0];
- rcvData2 = global_array[0];
- cout << "Proces A" << rank + 1 << " otrzymal: " << rcvData1 << " oraz: " << rcvData2 << endl;
- /*for (int i = 0; i < tableSize; i++) {
- cout << global_array[i] << endl;
- }*/
- //MPI_Send(&rcvData1, 1, MPI_INT, tableSize + rank, 99, MPI_COMM_WORLD);
- //MPI_Send(&rcvData2, 1, MPI_INT, tableSize + rank + 1, 99, MPI_COMM_WORLD);
- }
- if (rank < a && rank != 0)
- {
- MPI_Recv(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
- rcvData2 = rcvData1;
- cout << "Proces A" << rank + 1<< " otrzymal: " << rcvData1 << " oraz: " << rcvData2 << endl;
- //MPI_Send(&rcvData1, 1, MPI_INT, tableSize + rank, 99, MPI_COMM_WORLD);
- //MPI_Send(&rcvData1, 1, MPI_INT, tableSize + rank + 1, 99, MPI_COMM_WORLD);
- }
- //Koniec inicjalizacji
- //Początek algorytmu
- for (int i = 0; i < a * 2 ; i++) {
- if (rank < a) {
- if (rcvData1 < rcvData2) {
- MPI_Send(&rcvData1, 1, MPI_INT, tableSize + rank, 99, MPI_COMM_WORLD);
- MPI_Send(&rcvData2, 1, MPI_INT, tableSize + rank + 1, 99, MPI_COMM_WORLD);
- cout << "wysylam do procesu B" << rank << " oraz do B" << rank + 1 << endl;
- }
- else {
- MPI_Send(&rcvData1, 1, MPI_INT, tableSize + rank + 1, 99, MPI_COMM_WORLD);
- MPI_Send(&rcvData2, 1, MPI_INT, tableSize + rank, 99, MPI_COMM_WORLD);
- cout << "wysylam do procesu B" << rank << " oraz do B" << rank + 1 << endl;
- }
- MPI_Recv(&rcvData1, 1, MPI_INT, tableSize + rank, 99, MPI_COMM_WORLD, &status);
- MPI_Recv(&rcvData2, 1, MPI_INT, tableSize + rank + 1, 99, MPI_COMM_WORLD, &status);
- cout << "W iteracji Proces A" << rank +1 << " otrzymal: " << rcvData1 << " oraz: " << rcvData2 << endl;
- }
- if (rank >= a)
- {
- //pierwszy b
- if (rank == a)
- {
- MPI_Recv(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
- cout << "Proces B" << rank - a << " otrzymal: " << rcvData1 << endl;
- MPI_Send(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD);
- }//ostatni b
- else if (rank == a + b -1)
- {
- MPI_Recv(&rcvData1, 1, MPI_INT, a - 1, 99, MPI_COMM_WORLD, &status);
- cout << "Proces B" << rank - a << " otrzymal: " << rcvData1 << endl;
- MPI_Send(&rcvData1, 1, MPI_INT, a - 1, 99, MPI_COMM_WORLD);
- }
- else
- {
- MPI_Recv(&rcvData1, 1, MPI_INT, rank - a - 1, 99, MPI_COMM_WORLD, &status);
- MPI_Recv(&rcvData2, 1, MPI_INT, rank - a, 99, MPI_COMM_WORLD, &status);
- cout << "Proces B" << rank - a<< " otrzymal: " << rcvData1 << " oraz: " << rcvData2 << endl;
- if (rcvData1 < rcvData2) {
- MPI_Send(&rcvData1, 1, MPI_INT, rank - a - 1, 99, MPI_COMM_WORLD);
- MPI_Send(&rcvData2, 1, MPI_INT, rank - a, 99, MPI_COMM_WORLD);
- }
- else {
- MPI_Send(&rcvData1, 1, MPI_INT, rank - a, 99, MPI_COMM_WORLD);
- MPI_Send(&rcvData2, 1, MPI_INT, rank - a - 1, 99, MPI_COMM_WORLD);
- }
- }
- }
- }
- if (rank < a && rank != 0) {
- MPI_Send(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD);
- }
- if (rank == 0) {
- for (int i = 1; i < a; i++) {
- MPI_Recv(&global_array[i], 1, MPI_INT, i, 99, MPI_COMM_WORLD, &status);
- }
- global_array[0] = rcvData1;
- cout << "Posortowana tabela" << endl;
- for (int i = 0; i < tableSize; i++) {
- cout << global_array[i] << endl;
- }
- }
- //if (rank >= a)
- //{
- // //pierwszy b
- // if (rank == a)
- // {
- // //MPI_Recv(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD, &status);
- // //MPI_Send(&rcvData1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD);
- // }//ostatni b
- // else if (rank == a + b - 1)
- // {
- // //MPI_Recv(&rcvData1, 1, MPI_INT, a + b - 1, 99, MPI_COMM_WORLD, &status);
- // //MPI_Send(&rcvData1, 1, MPI_INT, a + b - 1, 99, MPI_COMM_WORLD);
- // }
- // else
- // {
- // //MPI_Recv(&rcvData2, 1, MPI_INT, rank - a, 99, MPI_COMM_WORLD, &status);
- // //MPI_Recv(&rcvData1, 1, MPI_INT, rank - a - 1, 99, MPI_COMM_WORLD, &status);
- // }
- //}
- //cout << "Koniec " << rank << endl;
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement