Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mpi.h>
- #include <string.h>
- #include <ctime>
- #include <time.h>
- int main(int argc, char* argv[])
- {
- int rank, brp, prenos, i;
- int sum = 0;
- int buff[10];
- int randbuff[320];
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &brp);
- if (rank == brp - 1)
- {
- srand(time(NULL));
- for (i = 0; i < brp * 10; i++)
- {
- randbuff[i] = rand() % 1000;
- }
- for (i = 0; i < 10; i++)
- sum = sum + randbuff[i];
- if (sum % 2 == 0)
- {
- prenos = 1;
- MPI_Send(&prenos, 1, MPI_INT, brp - 2, 0, MPI_COMM_WORLD);
- }
- else
- {
- prenos = 0;
- MPI_Send(&prenos, 1, MPI_INT, brp - 2, 0, MPI_COMM_WORLD);
- }
- }
- MPI_Scatter(randbuff, 10, MPI_INT, buff, 10, MPI_INT, brp - 1, MPI_COMM_WORLD);
- if (rank != 0 && rank != brp - 1)
- {
- MPI_Recv(&prenos, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
- prenos = prenos << 1;
- for (i = 0; i < 10; i++)
- sum = sum + buff[i];
- if (sum % 2 == 0)
- {
- prenos++;
- MPI_Send(&prenos, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD);
- }
- else
- {
- MPI_Send(&prenos, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD);
- }
- }
- if (rank == 0)
- {
- MPI_Recv(&prenos, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- for (int i = 1; i < brp; i++)
- {
- //std::cout << "Proces: " << i << " ima prenos: " << prenos <<"\n";
- if ((prenos >> 0) & 1)
- std::cout << "Proces: " << i << " ima parnu sumu bafera\n";
- prenos = prenos >> 1;
- }
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement