c - MPI_ERR_BUFFER 使用带有大消息的 MPI_Bsend()

标签 c mpi

我尝试使用 MPI_Bsend 发送大消息(整数)。但是如果消息大于 1006 个整数,我会得到错误“MPI_ERR_BUFFER:无效的缓冲区指针”。我尝试使用 MPI_Buffer_attach 附加缓冲区,但它并没有改变错误消息中的内容。

一个最小的工作示例是

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {

  int world_rank, world_size;
  MPI_Init(NULL, NULL);
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);

  if (world_size < 2) {
    fprintf(stderr, "World size must be greater than 1");
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  int msg_size = 1007;
  int msg[msg_size];
  int i;

  int buf_size = 2^16;
  int buf[buf_size];
  MPI_Buffer_attach(buf, buf_size);

  if (world_rank == 0) {
    for (i = 0; i < msg_size; ++i) {
      msg[i] = rand();
    }

    MPI_Bsend(&msg, msg_size, MPI_INT, 1, 0, MPI_COMM_WORLD);
  }

  else if (world_rank == 1) {
    MPI_Recv(&msg, msg_size, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  }
  MPI_Buffer_detach(&buf, &buf_size);
  MPI_Finalize();
}

谁能给我一个线索?对不起,如果这很明显,我是 MPI 的新手。

最佳答案

这里有很多问题!

首先,你的声明

int buf_size = 2^16;

将 buf_size 设置为 16 而不是 65536(^ 是按位异或而不是“提升到幂”)。

其次,缓冲区大小应以字节为单位,因此附加应为:

MPI_Buffer_attach(buf, buf_size*sizeof(int));

主要问题是您的发送和接收不匹配 - 等级 1 应该从等级 0 接收:

MPI_Recv(msg, msg_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

而目前它试图从自己接收。

大卫

关于c - MPI_ERR_BUFFER 使用带有大消息的 MPI_Bsend(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912559/

相关文章:

复杂格式说明符 C

c - 一个数的补码

c++ - 将带有迭代器的 C++ 程序转换为 Boost MPI 并行程序

c - MPI_Recv 和超时

c - 在 C 或 C++ 中,我可以对哪些文件可以包含我的头文件施加限制

c - C 中具有动态内存分配的结构

我可以多次运行同一个线程吗?

c++ - 用于向所有 mpi 进程发送和接收的时间高效设计模型 : MPI all 2 all communication

c - 进入 NIT 参数编号 9 时有非法值

c - 如何使用 MPI 中的标志打破所有进程中的循环?