【发布时间】:2020-04-13 15:37:49
【问题描述】:
简介
为了在 C++ 中并行化数值积分,我想在我的本地机器上使用客户端/服务器方法。为此,我使用message passing interface for C++。
我的代码
所以我首先尝试了一个 hello world 设置,我将从客户端向服务器发送一条消息。为此,我在同一目录中有两个文件。这是服务器的代码,server.cpp:
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
int size, rank, msg;
int main(int argc, char *argv[]){
MPI_Comm client;
MPI_Status status;
char portname[MPI_MAX_PORT_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Open_port(MPI_INFO_NULL, portname);
printf("portname: %s\n", portname);
MPI_Comm_accept(portname, MPI_INFO_NULL, 0, MPI_COMM_SELF, &client);
printf("client connected\n");
MPI_Recv(&msg, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status);
printf("msg: %d\n", msg);
MPI_Comm_free(&client);
MPI_Close_port(portname);
MPI_Finalize();
}
这是客户端的代码,client.cpp:
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int size, rank;
int main(int argc, char *argv[]){
MPI_Comm server;
int msg, tag, dest;
char portname[MPI_MAX_PORT_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if (argc >= 2){
printf("Trying connect to %s\n", argv[1]);
strcpy(portname, argv[1]);
MPI_Comm_connect(portname, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &server);
msg = 42; tag = 0; dest = 0;
MPI_Send(&msg, 1, MPI_INT, dest, tag, server);
MPI_Comm_disconnect(&server);
}
MPI_Finalize();
}
我如何尝试运行我的代码
我分别使用mpiCC -g -Wall -o client.out client.cpp 和mpiCC -g -Wall -o server.out server.cpp 编译了客户端和服务器。
现在我打开两个不同的终端,并在第一个终端中使用mpirun -np 1 ./server.out 来运行服务器。这给了我端口名,所以我可以在第二个终端中使用mpirun -np 1 ./client.out <PORTNAME> 来运行客户端。但是,我收到以下错误:
Trying connect to <PORTNAME>
--------------------------------------------------------------------------
The user has called an operation involving MPI_Connect and/or MPI_Accept
that spans multiple invocations of mpirun. This requires the support of
the ompi-server tool, which must be executing somewhere that can be
accessed by all participants.
Please ensure the tool is running, and provide each mpirun with the MCA
parameter "pmix_server_uri" pointing to it.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
Your application has invoked an MPI function that is not supported in
this environment.
MPI function: MPI_Comm_connect
Reason: Underlying runtime environment does not support accept/connect functionality
--------------------------------------------------------------------------
[ubuntu-workstation:00101] *** An error occurred in MPI_Comm_connect
[ubuntu-workstation:00101] *** reported by process [23056836,0]
[ubuntu-workstation:00101] *** on communicator MPI_COMM_WORLD
[ubuntu-workstation:00101] *** MPI_ERR_INTERN: internal error
[ubuntu-workstation:00101] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[ubuntu-workstation:00101] *** and potentially your MPI job)
我尝试了什么
我找到了this blog post,那里发生了一个非常相似的错误。
似乎我只需要启动这个不祥的“ompi-server 工具”,但是,作为 MPI 的新手,我不知道在哪里可以找到这个工具以及如何启动它......那么我该如何解决这个错误?
【问题讨论】:
-
那是哪个版本的 Open MPI?
-
@HristoIliev 这是
ompi_info输出的前两行:Package: Debian OpenMPI \n Open MPI: 3.1.3 -
我有几个关于旧版本 Open MPI 中的客户端/服务器机制的答案,但从那以后情况发生了一些变化。让我检查一下 v3.1 的情况。同时,为什么需要客户端/服务器功能?你不能使用简单的 MPI 作业和一堆等级来进行处理,然后简单地让等级 0 成为服务器吗?
-
嗨 Hristo,是的,你是对的,我可以做到。但是,我正在尝试通过遵循我发现的一些在线练习来学习一点 MPI。其中一个要求我们使用客户端/服务器来实现数值集成????
-
MPI 的客户端/服务器功能被广泛使用(它甚至在 MPI 规范中关于进程创建的章节中的“其他功能”下列出),这就是为什么您最终会遇到它的情况主要 MPI 实现之一的支持被严重破坏。主要原因是没有容错的规定就没有什么意义,而这在 MPI 中仍然缺乏。所以,我建议你进行一组不同的在线练习:)
标签: c++ parallel-processing mpi