运行 C++ client/server 使用 MPI 进行并行处理
Running C++ client/server for parallel processing using MPI
简介
为了在 C++ 中并行化数值积分,我想在我的本地机器上使用 client/server 方法。为此,我使用 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 的新手,我不知道在哪里可以找到这个工具以及如何启动它......所以我该如何修复错误?
不幸的是,具有工作 accept/connect 功能的最新版本的 Open MPI 似乎是 Open MPI 1.6.5。从 v1.7 开始,支持被破坏,而 ORTE 2.x 所需的支持完全缺失,其实现优先级较低,如 issue 中所示。您应该以某种方式实现没有 client/server 功能的解决方案,降级到 Open MPI 1.6.5,或者简单地切换到 MPICH 或 Intel MPI。后者现在可以作为 oneAPI 的一部分免费使用,我刚刚测试了您的代码在修复 client.cpp
中的拼写错误后是否可以使用它(server
而不是 servidor
)。
简介
为了在 C++ 中并行化数值积分,我想在我的本地机器上使用 client/server 方法。为此,我使用 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 的新手,我不知道在哪里可以找到这个工具以及如何启动它......所以我该如何修复错误?
不幸的是,具有工作 accept/connect 功能的最新版本的 Open MPI 似乎是 Open MPI 1.6.5。从 v1.7 开始,支持被破坏,而 ORTE 2.x 所需的支持完全缺失,其实现优先级较低,如 issue 中所示。您应该以某种方式实现没有 client/server 功能的解决方案,降级到 Open MPI 1.6.5,或者简单地切换到 MPICH 或 Intel MPI。后者现在可以作为 oneAPI 的一部分免费使用,我刚刚测试了您的代码在修复 client.cpp
中的拼写错误后是否可以使用它(server
而不是 servidor
)。