c - 在 C 中使用 MPI 发送 2D 数组行 block

标签 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);
}

谢谢。

最佳答案

问题可能在于 printMatrix()fillMatrix()clang 拒绝编译您的 fillMatrix() 定义:

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

从原型(prototype)中删除 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 

关于c - 在 C 中使用 MPI 发送 2D 数组行 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377127/

相关文章:

python - pickle numpy数组的子类时保留自定义属性

64-bit - Cray mpich 是否有 64 位整数 MPI 变量?

parallel-processing - 如何在 MPI 中发送没有特定目的地的消息?

无法检索编辑控件中文本的长度

c - 程序分析(递归)

c - C 中有效指针操作的问题

c# - 从一个数组中删除其索引存在于另一个数组中的元素

arrays - 我想要将一组色 block 叠加到图像上并想要控制它们的颜色范围?

c - 如果收到所有内容则执行函数,MPI C

C fscanf 从文件错误中读取数据