我尝试使用 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/