在 MPI 中发送和接收消息时何时使用标签?
When to use tags when sending and receiving messages in MPI?
我不确定何时必须为 MPI 发送、接收调用中的 tag 字段使用不同的数字。我看过this,但我看不懂
Sometimes there are cases when A might have to send many different
types of messages to B. Instead of B having to go through extra
measures to differentiate all these messages, MPI allows senders and
receivers to also specify message IDs with the message (known as
tags). When process B only requests a message with a certain tag
number, messages with different tags will be buffered by the network
until B is ready for them.
我是否必须使用标签,例如,当我从进程 A 多次调用 "isend"(使用不同的标签)而在进程 B 中只有 1 次调用 "ireceive" 时?
一般来说,我倾向于避开它们。不要求您使用标签。如果需要在解析消息之前获取消息大小,可以使用MPI_Probe
。这样你就可以发送不同的消息而不是指定标签。我通常使用标签,因为 MPI_Recv
要求您在获取数据之前知道消息大小。如果您有不同的大小和类型,标签可以通过让多个线程或进程侦听不同的子集来帮助您区分它们。标签 1 可以表示 X 类型的消息,标签 2 将是 Y 类型的消息。此外,它使您能够进行多次 "channels" 通信,而无需创建唯一的通信器和组。
#include <mpi.h>
#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
{
// Init MPI
MPI_Init( &argc, &argv);
// Get the rank and size
int rank, size;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
// If Master
if( rank == 0 ){
char* message_r1 = "Hello Rank 1";
char* message_r2 = "Hello Rank 2";
// Send a message over tag 0
MPI_Send( message_r1, 13, MPI_CHAR, 1, 0, MPI_COMM_WORLD );
// Send a message over tag 1
MPI_Send( message_r2, 13, MPI_CHAR, 2, 1, MPI_COMM_WORLD );
}
else{
// Buffer
char buffer[256];
MPI_Status status;
// Wait for your own message
MPI_Recv( buffer, 13, MPI_CHAR, 0, rank-1, MPI_COMM_WORLD, &status );
cout << "Rank: " << rank << ", Message: " << buffer << std::endl;
}
// Finalize MPI
MPI_Finalize();
}
消息标签是可选的。您可以为它们使用任意整数值,并使用您喜欢且对您有用的任何语义。
正如您所建议的,标签可用于区分包含不同类型(MPI_INTEGER
、MPI_REAL
、MPI_BYTE
等)的消息。您还可以使用标签添加一些关于数据实际代表什么的信息(如果您有一个 n
xn
矩阵,发送该矩阵一行的消息将包含 n
值,以及发送该矩阵的一列的消息;但是,您可能希望以不同方式处理行和列数据)。
请注意,接收操作必须匹配它要接收的消息的标签。但是,这并不意味着您必须指定相同的标签,您还可以使用通配符 MPI_ANY_TAG
作为消息标签;然后接收操作将匹配任意消息标签。您可以在 MPI_Probe
.
的帮助下找出发件人使用的标签
标签可用于 分布式计算 算法,其中可以有多种类型的消息。考虑领导者选举问题,其中一个进程(选举候选人)发送类型为 requestVote 的消息,其他进程以类型为 voteGrant 的消息响应。
有许多这样的算法可以区分消息的类型,标签可用于对此类消息进行分类。
我不确定何时必须为 MPI 发送、接收调用中的 tag 字段使用不同的数字。我看过this,但我看不懂
Sometimes there are cases when A might have to send many different types of messages to B. Instead of B having to go through extra measures to differentiate all these messages, MPI allows senders and receivers to also specify message IDs with the message (known as tags). When process B only requests a message with a certain tag number, messages with different tags will be buffered by the network until B is ready for them.
我是否必须使用标签,例如,当我从进程 A 多次调用 "isend"(使用不同的标签)而在进程 B 中只有 1 次调用 "ireceive" 时?
一般来说,我倾向于避开它们。不要求您使用标签。如果需要在解析消息之前获取消息大小,可以使用MPI_Probe
。这样你就可以发送不同的消息而不是指定标签。我通常使用标签,因为 MPI_Recv
要求您在获取数据之前知道消息大小。如果您有不同的大小和类型,标签可以通过让多个线程或进程侦听不同的子集来帮助您区分它们。标签 1 可以表示 X 类型的消息,标签 2 将是 Y 类型的消息。此外,它使您能够进行多次 "channels" 通信,而无需创建唯一的通信器和组。
#include <mpi.h>
#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
{
// Init MPI
MPI_Init( &argc, &argv);
// Get the rank and size
int rank, size;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
// If Master
if( rank == 0 ){
char* message_r1 = "Hello Rank 1";
char* message_r2 = "Hello Rank 2";
// Send a message over tag 0
MPI_Send( message_r1, 13, MPI_CHAR, 1, 0, MPI_COMM_WORLD );
// Send a message over tag 1
MPI_Send( message_r2, 13, MPI_CHAR, 2, 1, MPI_COMM_WORLD );
}
else{
// Buffer
char buffer[256];
MPI_Status status;
// Wait for your own message
MPI_Recv( buffer, 13, MPI_CHAR, 0, rank-1, MPI_COMM_WORLD, &status );
cout << "Rank: " << rank << ", Message: " << buffer << std::endl;
}
// Finalize MPI
MPI_Finalize();
}
消息标签是可选的。您可以为它们使用任意整数值,并使用您喜欢且对您有用的任何语义。
正如您所建议的,标签可用于区分包含不同类型(MPI_INTEGER
、MPI_REAL
、MPI_BYTE
等)的消息。您还可以使用标签添加一些关于数据实际代表什么的信息(如果您有一个 n
xn
矩阵,发送该矩阵一行的消息将包含 n
值,以及发送该矩阵的一列的消息;但是,您可能希望以不同方式处理行和列数据)。
请注意,接收操作必须匹配它要接收的消息的标签。但是,这并不意味着您必须指定相同的标签,您还可以使用通配符 MPI_ANY_TAG
作为消息标签;然后接收操作将匹配任意消息标签。您可以在 MPI_Probe
.
标签可用于 分布式计算 算法,其中可以有多种类型的消息。考虑领导者选举问题,其中一个进程(选举候选人)发送类型为 requestVote 的消息,其他进程以类型为 voteGrant 的消息响应。
有许多这样的算法可以区分消息的类型,标签可用于对此类消息进行分类。