Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mpi.h>
- #include <stdlib.h>
- #include <cstring>
- using namespace std;
- int main(int argc, char *argv[]) {
- int ProcNum, ProcRank, N = 0;
- MPI_Status Status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
- MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
- //--------------------------
- N = (argc >= 2) ? N = stoi(argv[1]) : N = 4;
- //--------------------------
- if (N == 0) {
- MPI_Finalize();
- return 0;
- }
- if (ProcRank == 0) {
- int x = 0;
- bool exit = false;
- char m[10];
- while (exit != true) {
- cout << "Choose 1 to push, 2 to pop, 3 to exit" << endl;
- cin >> x;
- if (x == 1) {
- //push
- cout << "Plese enter a push value: ";
- cin >> m;
- MPI_Send(&m, 10, MPI_CHAR, 1, 1, MPI_COMM_WORLD);
- }
- if (x == 2) {
- //pop
- MPI_Send(&m, 1, MPI_INT, 1, 2, MPI_COMM_WORLD);
- }
- if (x == 3) {
- //exit
- for (int i = 1; i < ProcNum; i++) {
- MPI_Send(&m, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
- }
- exit = true;
- }
- }
- } else {
- char m[10];
- bool exit = false;
- bool msg = false;
- char message[10];
- while (exit != true) {
- MPI_Recv(&m, 10, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &Status);
- cout << "Okey man I`m " << ProcRank << " and I get message " << m << endl;
- if (Status.MPI_TAG == 4) {
- cout << "I`m off" << endl;
- exit = true;
- }
- if (Status.MPI_TAG == 1) {
- if ((msg == true) && (ProcRank == ProcNum - 1)) {
- cout << "ProcRank = " << ProcRank << " ProcNum = " << ProcNum << endl;
- cout << "QUEUE IS FULL!!!";
- break;
- }
- if (msg == true) {
- MPI_Send(&m, 10, MPI_CHAR, ProcRank + 1, 1, MPI_COMM_WORLD);
- }
- if (msg == false) {
- strcpy(message, m);
- msg = true;
- cout << "IT IS MY PUSH MESSAGE " << message << endl;
- }
- }
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement