在openMPI中向进程发送数据(以字节为单位)的最佳方式

best way to send data(given in Bytes) to a process in openMPI?

本文关键字:为单位 字节 方式 最佳 数据 openMPI 进程      更新时间:2023-10-16

我正在这样做,假设我需要向根进程发送临时字节数:

var char*;
var=new char[temp];
MPI_Isend(&temp,1,MPI_INT,0,tag,MPI_COMM_WORLD,&request[0]);
MPI_Isend(var,temp,MPI_BYTE,0,tag,MPI_COMM_WORLD,&request[1]);

在根进程上,我正在编写以下代码,

MPI_Recv(&temp,1,MPI_INT,i,tag,MPI_COMM_WORLD,&status[0]);
var=new char[temp];
MPI_Recv(var,temp,MPI_BYTE,0,tag,MPI_COMM_WORLD,&status[1]);

我能够在根进程接收temp(要传输的数据字节数),并在下一部分看到截断消息的错误?

您不需要将元素的数量作为单独的消息发送。MPI提供了一种机制,允许程序窥探消息队列,并在接收消息之前获得消息大小

发件人:

char *var = new char[temp];
MPI_Send(var, temp, MPI_BYTE, 0, tag, MPI_COMM_WORLD);

接收器:

MPI_Status status;
int temp;
// Probe for matching message without receiving it
MPI_Probe(i, tag, MPI_COMM_WORLD, &status);
// Get the number of data elements in the message
MPI_Get_count(&status, MPI_BYTE, &temp);
char *var = new char[temp];
MPI_Recv(var, temp, MPI_BYTE, i, tag, MPI_COMM_WORLD, &status);

这是一种与不同大小的阵列进行通信的最佳方式。

您从错误的位置接收:

MPI_Recv(var,temp,MPI_BYTE,0,tag,MPI_COMM_WORLD,&status[1]);
                           ^

您应该(大概)从i进程接收。