/
PavelPanchuk
/
lab3
Обзор
Документация
Войти
/
PavelPanchuk
/
lab3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
mpii.cpp
163 строки
4 KB
PavelPanchuk
create mpii.cpp
12 дек 2024, 12:44
12 дек 2024, 12:44
561d7c8
Код
Авторство
О чём код?
#include <iostream> #include <omp.h> #include <cmath> #include <mpi.h> #include <ctime> using namespace std; // Вывод системы уравнений void sysout(double** a, double* y, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << a[i][j] << "*x" << j; if (j < n - 1) cout << " + "; } cout << " = " << y[i] << endl; } return; } double* gauss(double** a, double* y, int n) { double* x, max; int k, index; const double eps = 0.00001; // точность x = new double[n]; k = 0; while (k < n) { // Поиск строки с максимальным a[i][k] max = abs(a[k][k]); index = k; #pragma omp parallel for for (int i = k + 1; i < n; i++) { if (abs(a[i][k]) > max) { max = abs(a[i][k]); index = i; } } // Перестановка строк if (max < eps) { // нет ненулевых диагональных элементов cout << "Решение получить невозможно из-за нулевого столбца "; cout << index << " матрицы A" << endl; return 0; } #pragma omp parallel for for (int j = 0; j < n; j++) { double temp = a[k][j]; a[k][j] = a[index][j]; a[index][j] = temp; } double temp = y[k]; y[k] = y[index]; y[index] = temp; // Нормализация уравнений #pragma omp for //может ломать for (int i = k; i < n; i++) { double temp = a[i][k]; if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить #pragma omp parallel for for (int j = k; j < n; j++){ a[i][j] = a[i][j] / temp; } y[i] = y[i] / temp; if (i == k) continue; // уравнение не вычитать само из себя #pragma omp parallel for for (int j = 0; j < n; j++) a[i][j] = a[i][j] - a[k][j]; y[i] = y[i] - y[k]; } k++; } // обратная подстановка //#pragma omp parallel for for (k = n - 1; k >= 0; k--) { x[k] = y[k]; #pragma omp parallel for for (int i = 0; i < k; i++) y[i] = y[i] - a[i][k] * x[k]; } return x; } int main(int argc, char** argv) { MPI_Init(&argc,&argv); int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if(rank==0) { double** a, * y, * x; int n; cout << "Введите количество уравнений: "; cin >> n; a = new double* [n]; y = new double[n]; for (int i = 0; i < n; i++) { a[i] = new double[n]; for (int j = 0; j < n; j++) { cout << "a[" << i << "][" << j << "]= "; cin >> a[i][j]; } } for (int i = 0; i < n; i++) { cout << "y[" << i << "]= "; cin >> y[i]; } /* Начать измерение времени*/ clock_t start, finish; double duration; start = clock(); sysout(a, y, n); x = gauss(a, y, n); #pragma omp parallel for for (int i = 0; i < n; i++){ cout << "x[" << i << "]=" << x[i] << endl; } /* итог измерение времени*/ finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; cout << "Время выполнения программы="<<duration<<endl; } MPI_Finalize(); return 0; }