Доброго времени суток Есть задача, которую необходимо решить путём гибридного программирования При распараллеливании в функции MPI_Scatter возможно неправильно передаются строки, а скорее всего передаётся только первая строка, подскажите, где я допустил ошибку?
Код | #include "stdafx.h" #include <stdio.h> #include <conio.h> #include <string> #include <iostream> #include <omp.h> #include <mpi.h> #include <stdlib.h>
using namespace std;
void outMatrix(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; } cout << endl; }
int main(int argc, char* argv[]) { int n = 3; int i;
int *a = new int [n*n]; int *b = new int [n*n]; int *c = new int [n*n];
int *bufa = new int [n]; int *bufb = new int [n]; int *bufc = new int [n];
for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { a[i*n+j] = rand()%3; b[i*n+j] = rand()%3; } }
int size, rank; int sum;
MPI_Init (& argc ,& argv ); MPI_Comm_size ( MPI_COMM_WORLD ,& size ); MPI_Comm_rank ( MPI_COMM_WORLD ,& rank );
MPI_Bcast (&n, 1, MPI_INT , 0, MPI_COMM_WORLD ); MPI_Scatter(a, n, MPI_INT, bufa, n, MPI_INT, 0, MPI_COMM_WORLD); MPI_Scatter(b, n, MPI_INT, bufb, n, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) { cout << "A" << endl; outMatrix(a,n); cout << "B" << endl; outMatrix(b,n); }
omp_set_num_threads(n);
#pragma omp parallel for private(i) for(i=0;i<n;i++) { bufc[i]=bufa[i]+bufb[i]; cout << "bufa[" << i << "] = " << bufa[i] << endl; cout << "bufb[" << i << "] = " << bufb[i] << endl; } MPI_Gather(bufc, n, MPI_INT, c, n, MPI_INT, 0, MPI_COMM_WORLD); if (rank == 0) { cout << "Result " << endl; for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { cout << c[i*n+j] << " "; } cout << endl; } cout << endl; } MPI_Finalize (); getch(); }
|
В результате получается, что первые строки про суммировались, а остальные нет http://images.vfl.ru/ii/1398710978/2b58270e/4979233.png |