#include <iostream> #include <vector> #include <mpi.h> #include <iomanip> #define N 4…
- User
#include #include #include <mpi.h> #include
#define N 4
void initializeMatrixAndVector(std::vector<std::vector>& A, std::vector& b) { // Задаем конкретные значения матрицы A и вектора b A = { {2, 1, -1, 3}, {4, -6, 0, 3}, {-2, 7, 2, 1}, {1, 1, 1, 1} };
b = { 8, -2, 11, 4 };}
void printMatrix(const std::vector<std::vector>& A, const std::vector& b) { std::cout << "matrix A and vector b:" << std::endl; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { std::cout << std::setw(5) << A[i][j] << " "; } std::cout << "| " << std::setw(5) << b[i] << std::endl; } std::cout << std::endl; }
void gaussSolveMPI(std::vector<std::vector>& A, std::vector& b, std::vector& x, int rank, int size) { for (int k = 0; k < N; ++k) { if (rank == 0) { for (int j = k + 1; j < N; ++j) { A[k][j] /= A[k][k]; } b[k] /= A[k][k]; A[k][k] = 1.0; }
MPI_Bcast(&A[k][0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(&b[k], 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); for (int i = k + 1 + rank; i < N; i += size) { double factor = A[i][k]; for (int j = k + 1; j < N; ++j) { A[i][j] -= factor * A[k][j]; } b[i] -= factor * b[k]; A[i][k] = 0.0; } MPI_Barrier(MPI_COMM_WORLD); } for (int i = N - 1; i >= 0; --i) { x[i] = b[i]; for (int j = i + 1; j < N; ++j) { x[i] -= A[i][j] * x[j]; } x[i] /= A[i][i]; }}
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); std::vector<std::vector<double>> A(N, std::vector<double>(N)); std::vector<double> b(N), x(N); if (rank == 0) { initializeMatrixAndVector(A, b); printMatrix(A, b); } for (int i = 0; i < N; ++i) { MPI_Bcast(&A[i][0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD); } MPI_Bcast(&b[0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD); gaussSolveMPI(A, b, x, rank, size); if (rank == 0) { std::cout << "result vector x:" << std::endl; for (int i = 0; i < N; ++i) { std::cout << "x[" << i << "] = " << x[i] << std::endl; } } MPI_Finalize(); return 0;} Привет проверь правильность работы кода
- Assistant
- User
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 1 mpi_metodgausa.exe matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
result vector x: x[0] = 1.05085 x[1] = 1.71186 x[2] = -0.118644 x[3] = 1.35593
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 2 mpi_metodgausa.exe matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
result vector x: x[0] = -12.1667 x[1] = -1.44444 x[2] = 4.22222 x[3] = 12.6667
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 3 mpi_metodgausa.exe matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
result vector x: x[0] = -1.82558 x[1] = 0.953488 x[2] = 0.325581 x[3] = 3.67442
D:\Projects\shapov\mpi_metodgausa\x64\Debug>
- Assistant
- User
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 2 mpi_metodgausa.exe Matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
Result vector x: x[0] = -12.1667 x[1] = -1.44444 x[2] = 4.22222 x[3] = 12.6667
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 1 mpi_metodgausa.exe Matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
Result vector x: x[0] = 1.05085 x[1] = 1.71186 x[2] = -0.118644 x[3] = 1.35593
D:\Projects\shapov\mpi_metodgausa\x64\Debug>mpiexec -n 10 mpi_metodgausa.exe Matrix A and vector b: 2 1 -1 3 | 8 4 -6 0 3 | -2 -2 7 2 1 | 11 1 1 1 1 | 4
Result vector x: x[0] = -1.82558 x[1] = 0.953488 x[2] = 0.325581 x[3] = 3.67442
D:\Projects\shapov\mpi_metodgausa\x64\Debug>
- Assistant