Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<mpi.h>
- #include<fstream>
- #include<string>
- #include<windows.h>
- using namespace std;
- const int m = 1000; //предел массива от - m/2 до + m/2 в
- int create_num(int A) //рандомное число (переменная rank)
- {
- int number;
- srand(time(NULL) + A);
- number = (rand() % m) - m / 2;
- return number;
- }
- int file_read() // считываем количество потоков
- {
- ifstream fileout;
- fileout.open("run.bat");//открытие файла
- int max = -1;
- if (fileout.is_open())//Если открытие файла прошло успешно
- {
- string line;
- getline(fileout, line);
- getline(fileout, line);
- line = line.substr(line.find("n ") + 2);
- line = line.substr(0, line.find(" "));
- fileout.close(); //Закрываем файл
- max = stoi(line);
- }
- else
- {
- cout << "file not open" << endl;
- }
- return max;
- }
- int main(int argc, char** argv)
- {
- int rank, size, n = 0;
- int max = file_read() ; // считываем количество вызовов
- int* rbuf = new int[max];
- for (int i = 0; i < max; i++)
- rbuf[i] = 0;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- n = create_num(rank);
- MPI_Gather(&n, 1, MPI_INT, rbuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
- cout << "Rank: " << rank << " Number: " << n << endl;
- if (rank == 0)
- {
- if (max != -1) //проверка считалось ли значение из файла
- {
- int sum = 0;
- for (int i = 0; i < max; i++)
- sum += rbuf[i];
- cout << "summ = " << sum << endl;
- }
- else
- {
- cout << "value undefined";
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement