Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <stdlib.h>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[]) {
  9.  
  10. int ProcNum, ProcRank, N = 0;
  11. MPI_Status Status;
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
  15. MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
  16.  
  17. //--------------------------
  18. N = (argc >= 2) ? N = stoi(argv[1]) : N = 4;
  19. //--------------------------
  20. if (N == 0) {
  21. MPI_Finalize();
  22. return 0;
  23. }
  24. if (ProcRank == 0) {
  25.  
  26. int x = 0;
  27. bool exit = false;
  28. char m[10];
  29. while (exit != true) {
  30. cout << "Choose 1 to push, 2 to pop, 3 to exit" << endl;
  31. cin >> x;
  32. if (x == 1) {
  33. //push
  34. cout << "Plese enter a push value: ";
  35. cin >> m;
  36. MPI_Send(&m, 10, MPI_CHAR, 1, 1, MPI_COMM_WORLD);
  37. }
  38. if (x == 2) {
  39. //pop
  40. MPI_Send(&m, 1, MPI_INT, 1, 2, MPI_COMM_WORLD);
  41. }
  42. if (x == 3) {
  43. //exit
  44. for (int i = 1; i < ProcNum; i++) {
  45. MPI_Send(&m, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
  46. }
  47. exit = true;
  48. }
  49. }
  50. } else {
  51. char m[10];
  52. bool exit = false;
  53. bool msg = false;
  54. char message[10];
  55.  
  56. while (exit != true) {
  57. MPI_Recv(&m, 10, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &Status);
  58. cout << "Okey man I`m " << ProcRank << " and I get message " << m << endl;
  59. if (Status.MPI_TAG == 4) {
  60. cout << "I`m off" << endl;
  61. exit = true;
  62. }
  63.  
  64. if (Status.MPI_TAG == 1) {
  65.  
  66. if ((msg == true) && (ProcRank == ProcNum - 1)) {
  67. cout << "ProcRank = " << ProcRank << " ProcNum = " << ProcNum << endl;
  68. cout << "QUEUE IS FULL!!!";
  69. break;
  70. }
  71. if (msg == true) {
  72. MPI_Send(&m, 10, MPI_CHAR, ProcRank + 1, 1, MPI_COMM_WORLD);
  73. }
  74.  
  75. if (msg == false) {
  76. strcpy(message, m);
  77. msg = true;
  78. cout << "IT IS MY PUSH MESSAGE " << message << endl;
  79. }
  80. }
  81. }
  82. }
  83. MPI_Finalize();
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement