Отправка блоков строк 2D-массива с использованием MPI в C - PullRequest
2 голосов
/ 07 декабря 2010

Я пытаюсь сделать матричное умножение, используя MPI в C. (c <= a * b) </p>

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

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

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

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

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

#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 Ответ

3 голосов
/ 07 декабря 2010

Проблема может лежать 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 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...