我正在尝试构建在https://www.mpi-forum.org/docs/mpi-3.1/mpi31-report/node248.htm#Node248中找到的MPI服务器-客户端示例。运行服务器和客户端似乎很好,除了它始终在MPI_Comm_disconnect调用后卡住。
服务器端
#include <iostream>
#include <mpi.h>
int main (int argc, char ** argv) {
MPI_Comm client;
MPI_Sttus status;
char port_name[MPI_MAX_PORT_NAME];
int size, again, i;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 1) {
std::cerr << "Server MPI processors must be 1!\n";
exit(EXIT_FAILURE);
}
MPI_Open_port(MPI_INFO_NULL, port_name);
std::cout << "SERVER: " << port_name << std::endl;
while (1) {
std::cout << "Waiting for a client ...\n";
MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &client);
again = 1;
while (again) {
MPI_Recv(&i, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status);
switch(status.MPI_TAG) {
case 0:
std::cout << "Shut down server!\n";
MPI_Comm_free(&client);
MPI_Close_port(port_name);
MPI_Finalize();
return EXIT_SUCCESS;
case 1:
std::cout << "Disconnect client!\n";
MPI_Comm_disconnect(&client);
std::cout << "check\n";
again = 0;
break;
case 2:
std::cout << "Received: " << i << std::endl;
break;
default:
std::cerr << "Abort with unknown tag!\n;
MPI_Abort(MPI_COMM_WORLD, 1);
break;
}
}
}
MPI_Finalize();
return EXIT_SUCCESS;
}
客户端
#include <iostream>
#include <mpi.h>
int main (int argc, char ** argv)
{
MPI_Comm server;
char port_name[MPI_MAX_PORT_NAME];
int i, tag;
if (argc < 2) {
std::cerr << "Server port name required!\n";
exit(EXIT_FAILURE);
}
MPI_Init(&argc, &argv);
strcpy(port_name, argv[1]);
std::cout << "Server at " << port_name << std::endl;
MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &server);
std::cout << "Start send ...\n";
for (i = 0; i < 5; i++) {
tag = 2;
MPI_Send(&i, 1, MPI_INT, 0, tag, server);
}
std::cout << "Send disconnect request!\n";
tag = 1;
MPI_Send(&i, 1, MPI_INT, 0, tag, server);
std::cout << "Disconnect server!\n";
MPI_Comm_disconnect(&server);
std::cout << "Finalize!\n";
MPI_Finalize();
return EXIT_SUCCESS;
}
我首先启动了服务器:
mpirun -n 1个服务器
然后,它将打印出以下消息
端口上可用的服务器:[这里是端口名称,并将其用作客户端代码的输入参数]
正在等待连接....
然后我启动了客户端:
mpirun -n 1个客户端[端口名称从上面复制]
然后,服务器在MPI_Comm_disconnect之后卡住。
已收到:0
已收到:1
已收到:2
已收到:3
已收到:4
断开客户端连接!
它永远不会打印出“检查!”。
在客户端,它在MPI_Comm_disconnect之后也卡住了。
开始发送...
发送断开连接请求!
断开服务器连接!
它永远不会打印出“完成!”。
什么会导致此问题?