#include <mpi.h> #include <iostream> using namespace std; void multblock(int* a, int* b,…
- User
#include <mpi.h> #include
using namespace std;
void multblock(int* a, int* b, int* c, int b_row, int b_col, int b_size, int n) { for (int i = b_row * b_size; i < (b_row + 1) * b_size; ++i) { for (int j = b_col * b_size; j < (b_col + 1) * b_size; ++j) { for (int k = 0; k < n; ++k) { c[i * n + j] += a[i * n + k] * b[k * n + j]; } } } } void print_matr(int* matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout « matrix[i * n + j] « " "; } cout « endl; } }
int main(int argc, char** argv) { int rank, size; int n = 4; int* mat_1 = new int[n * n]; int* mat_2 = new int[n * n]; int* result = new int[n * n] {0}; int* r_result = new int[n * n] {0};
MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { mat_1[i * n + j] = 2; mat_2[i * n + j] = 2; } } } MPI_Bcast(mat_1, n * n, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(mat_2, n * n, MPI_INT, 0, MPI_COMM_WORLD); int b_size = n / size; for (int b_row = rank * b_size; b_row < (rank + 1) * b_size; ++b_row) { for (int b_col = 0; b_col < n / b_size; ++b_col) { multblock(mat_1, mat_2, result, b_row / b_size, b_col, b_size, n); std::cout « "Process " « rank « " is working on block (" « b_row « ", " « b_col « ")" « std::endl; } } MPI_Reduce(result, r_result, n * n, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0) { print_matr(r_result, n); } delete[] mat_1; delete[] mat_2; delete[] result; delete[] r_result; MPI_Finalize(); return 0; } У меня почему при запуске на двух процессорах каждый элемент равняется 32
- Assistant
- User
У меня только на 4 процессорах правильный результат
- Assistant
- User
Я и говорю у меня на 1, 2 процессорах неправильный результат
- Assistant
- User
#include <mpi.h> #include
using namespace std;
void multblock(int* a, int* b, int* c, int b_row, int b_col, int b_size, int n) { for (int i = b_row * b_size; i < (b_row + 1) * b_size; ++i) { for (int j = b_col * b_size; j < (b_col + 1) * b_size; ++j) { for (int k = 0; k < n; ++k) { c[i * n + j] += a[i * n + k] * b[k * n + j]; } } } } void print_matr(int* matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout « matrix[i * n + j] « " "; } cout « endl; } }
int main(int argc, char** argv) { int rank, size; int n = 4; int* mat_1 = new int[n * n]; int* mat_2 = new int[n * n]; int* result = new int[n * n] {0}; int* r_result = new int[n * n] {0};
MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { mat_1[i * n + j] = 2; mat_2[i * n + j] = 2; } } } MPI_Bcast(mat_1, n * n, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(mat_2, n * n, MPI_INT, 0, MPI_COMM_WORLD); int b_size = n / size; for (int b_row = rank * b_size; b_row < (rank + 1) * b_size; ++b_row) { for (int b_col = 0; b_col <size; ++b_col) { multblock(mat_1, mat_2, result, b_row, b_col, b_size, n); cout « "Process " « rank « " is working on block (" « b_row « ", " « b_col « ")" «endl; } } MPI_Reduce(result, r_result, n * n, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0) { print_matr(r_result, n); } delete[] mat_1; delete[] mat_2; delete[] result; delete[] r_result; MPI_Finalize(); return 0; } Изобрази пожалуйста с какими блоками работает каждый процессор
- Assistant
- User
D:\Projects\shapov\mpi_composition_matrix\x64\Debug>mpiexec -n 4 mpi_composition_matrix.exe Process 0 is working on block (0, 0) Process 0 is working on block (0, 1) Process 1 is working on block (1, 0) Process 0 is working on block (0, 2) Process 1 is working on block (1, 1) Process 0 is working on block (0, 3) Process 1 is working on block (1, 2) Process 1 is working on block (1, 3) Process 2 is working on block (2, 0) Process 2 is working on block (2, 1) Process 2 is working on block (2, 2) Process 2 is working on block (2, 3) Process 3 is working on block (3, 0) Process 3 is working on block (3, 1) Process 3 is working on block (3, 2) Process 3 is working on block (3, 3) 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
D:\Projects\shapov\mpi_composition_matrix\x64\Debug>mpiexec -n 2 mpi_composition_matrix.exe Process 0 is working on block (0, 0) Process 0 is working on block (0, 1) Process 0 is working on block (1, 0) Process 1 is working on block (2, 0) Process 0 is working on block (1, 1) Process 1 is working on block (2, 1) Process 1 is working on block (3, 0) Process 1 is working on block (3, 1) 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
job aborted: [ranks] message
[0] terminated
[1] process exited without calling finalize
--— error analysis —---
- Assistant
- User
Проблема в том что он не досчитывает при 2 или 1 процессе
- Assistant