在C中使用MPI发送2D数组行的块

时间:2010-12-07 13:32:21

标签: c arrays mpi

我正在尝试使用C中的MPI进行矩阵乘法。(c <= a * b)

我在4个节点上运行以下代码。所有矩阵的大小都是8 * 8。

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

矩阵b [] []被广播,因此所有节点都获得相同的副本。对于矩阵a [] []而不是广播,我想只发送每个节点所需的行集。

但是当我运行以下代码并在MPI_Recv()工作节点之后打印矩阵a [] []时,打印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 个答案:

答案 0 :(得分: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