2010-12-07 2 views
2

Я пытаюсь сделать матричное умножение с использованием MPI в C. (с < = A * B)Отправка блоков строк 2D массива с использованием MPI в C

Я бегу следующий код на 4 узлах. Все матрицы имеют размер 8 * 8.

(num of rows in a matrix % num of nodes == 0) 

матрица b [] [] передается так, что все узлы получают одну и ту же копию. Для матрицы a [] [] вместо трансляции я хочу отправить только набор строк, необходимых каждому узлу.

Но когда я запустил следующий код и напечатал матрицу [] [] после того, как рабочие узлы MPI_Recv() напечатают 0s вместо значений, назначенных в главном узле.

Можете ли вы указать, что я делаю неправильно здесь?

#include <stdio.h> 
#include "mpi.h" 
#include "matrix.c" // matrix definitions and matrix operation functions are here 

int main(int argc, char *argv[]) 
{ 
    MPI_Status status; 
    int num, rank, size, tag, high,low,i; 
    int offset, tmphigh,rows; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 

    rows=MAX/size; // MAX is the length(=height) of the matrices 
    tag = 201; 
    low=rank*rows; 
    high=low+rows; 


    if (rank == 0) { 
    fillMatrix(b,MAX); 
    fillMatrix(a,MAX); 
    } 

    MPI_Bcast(&b[0][0],MAX*MAX,MPI_INT,0,MPI_COMM_WORLD); 

    if(rank==0){ 
    for(i=1;i<size;i++){ 
     offset=i*rows; 
     MPI_Send(&a[offset][0],rows*MAX,MPI_INT,i,tag,MPI_COMM_WORLD); 
    } 

    }else{ 
     MPI_Recv(&a[low][0],rows*MAX,MPI_INT,0,tag,MPI_COMM_WORLD,&status); 
    } 

    printMatrix(a,MAX); 

    MPI_Finalize(); 
    return 0; 
} 

вот как матрицы создаются

int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX]; 
int len; //(edited after Jeremy W. Sherman's comment) 
//this was the reason that caused this problem. changing this to int len=MAX; solved the problem 

void fillMatrix(int (*matrix)[len], int len){ 
    int i=0,j=0; 
    for(i=0;i<len;i++){ 
     for(j=0;j<len;j++){ 
      matrix[i][j]=j; 
     } 
    } 
    //printMatrix(matrix,len); 
} 

Спасибо.

+1

Действительно ли трансляция b работает так, как ожидалось? И вы могли бы показать нам, как создаются [] [] и b [] []? – 2010-12-07 13:40:11

+0

есть b хорошо работает. Я просто присваиваю несколько значений всем матрицам – Niroshan 2010-12-07 14:47:11

ответ

3

Проблема может быть printMatrix() и fillMatrix(). clang отказался компилировать определение fillMatrix():

so_mpi.c:22:31: error: use of undeclared identifier 'len' 
void fillMatrix(int (*matrix)[len], int len){ 
          ^

капают len от прототипа только создает другую проблему:

so_mpi.c:26:19: error: subscript of pointer to incomplete type 'int []' 
      matrix[i][j]=j; 
      ~~~~~~^ 

Что работа заключалась в следующем:

void fillMatrix(int *matrix, int len) { 
    int i, j; 

    for (i = 0; i < len; ++i) { 
    int *row = &matrix[i * len]; 
    for(j = 0; j < len; ++j) { 
     row[j] = j; 
    } 
    } 
} 

fillMatrix((int *)a, MAX); 
fillMatrix((int *)b, MAX); 

С учетом этого изменения, все работает нормально. Я использовал MAX = 5 и 5 узлов. Я префиксные заявления о регистрации с рангом узла и добавил еще несколько операторов регистрации. Вот результат:

$ mpirun -np 5 ./so_mpi 
node 1 of 5 
node 4 of 5 
node 0 of 5 
0: filling matrices 
0: matrix B: 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
0: matrix A: 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
0: broadcast of B complete 
0: sending 1 rows (5 elements) of A to node 1 
0: sending 1 rows (5 elements) of A to node 2 
0: sending 1 rows (5 elements) of A to node 3 
0: sending 1 rows (5 elements) of A to node 4 
0: matrix A: 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
    0 1 2 3 4 
1: broadcast of B complete 
1: received portion of matrix 
1: matrix A: 
    0 0 0 0 0 
    0 1 2 3 4 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
node 2 of 5 
2: broadcast of B complete 
2: received portion of matrix 
2: matrix A: 
    0 0 0 0 0 
    0 0 0 0 0 
    0 1 2 3 4 
    0 0 0 0 0 
    0 0 0 0 0 
node 3 of 5 
3: broadcast of B complete 
3: received portion of matrix 
3: matrix A: 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 1 2 3 4 
    0 0 0 0 0 
4: broadcast of B complete 
4: received portion of matrix 
4: matrix A: 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 0 0 0 0 
    0 1 2 3 4 
Смежные вопросы