Advertisement
Heart_Under_Blade

LR_5

Nov 5th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<mpi.h>
  3. #include<fstream>
  4. #include<string>
  5. #include<windows.h>
  6.  
  7. using namespace std;
  8.  
  9. const int m = 1000; //предел массива от - m/2 до + m/2 в
  10.  
  11.  
  12. int create_num(int A) //рандомное число (переменная rank)
  13. {
  14.     int number;
  15.     srand(time(NULL) + A);
  16.     number = (rand() % m) - m / 2;
  17.  
  18.     return number;
  19. }
  20.  
  21. int file_read() // считываем количество потоков
  22. {
  23.     ifstream fileout;
  24.     fileout.open("run.bat");//открытие файла
  25.     int max = -1;
  26.  
  27.     if (fileout.is_open())//Если открытие файла прошло успешно
  28.     {
  29.         string line;
  30.         getline(fileout, line);
  31.         getline(fileout, line);
  32.  
  33.         line = line.substr(line.find("n ") + 2);
  34.         line = line.substr(0, line.find(" "));
  35.  
  36.         fileout.close(); //Закрываем файл
  37.         max = stoi(line);
  38.     }
  39.     else
  40.     {
  41.         cout << "file not open" << endl;
  42.     }
  43.  
  44.     return max;
  45. }
  46.  
  47. int main(int argc, char** argv)
  48. {
  49.     int rank, size, n = 0;
  50.     int max = file_read() ; // считываем количество вызовов
  51.     int* rbuf = new int[max];
  52.  
  53.     for (int i = 0; i < max; i++)
  54.         rbuf[i] = 0;
  55.  
  56.     MPI_Init(&argc, &argv);
  57.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  58.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  59.  
  60.     n = create_num(rank);
  61.  
  62.     MPI_Gather(&n, 1, MPI_INT, rbuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
  63.  
  64.     cout << "Rank: " << rank << " Number: " << n << endl;
  65.  
  66.     if (rank == 0)
  67.     {
  68.         if (max != -1) //проверка считалось ли значение из файла
  69.         {
  70.             int sum = 0;
  71.             for (int i = 0; i < max; i++)
  72.                  sum += rbuf[i];
  73.             cout << "summ = " << sum << endl;
  74.         }
  75.         else
  76.         {
  77.             cout << "value undefined";
  78.         }
  79.     }
  80.  
  81.     MPI_Finalize();
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement