消息在网络上的两台机器之间传递
Message passing between two machines on a network
问题描述:网络中的两个不同的Unix机器上有两个进程。从一台计算机到另一台机器的最简单方法是什么?我知道IPC有很多方法,但是最小的开销最简单的方法是什么?
在我的情况下,Boost MPI有用吗?我正在使用C 进行实施。
插座。这是一个插座教程。一旦有插座,您也可以使用Boost插座
使用套接字(我建议提升插座)或查看Zeromq。Zeromq实际上可能会更容易,因为它可以保证始终接收完整的消息。
这很简单吗?(使用纯MPI标准呼叫,没有任何第三方库,例如Boost.MPI
)
#include <string>
#include <iostream>
#include <mpi.h>
using namespace std;
int main (int argc, char **argv) {
// Initialise the MPI library
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size != 2) {
if (rank == 0)
cout << "ERROR: You must run this program with 2 processes" << endl;
MPI_Abort(MPI_COMM_WORLD, 0);
}
if (rank == 0) {
// Rank 0 sends the message
string hi = "Hi!";
MPI_Send((void*)hi.c_str(), hi.length()+1, MPI_CHAR,
1, 0, MPI_COMM_WORLD);
}
else {
// Rank 1 receives the message
// Probe for a message and get its actual size
MPI_Status status;
MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
int len;
MPI_Get_count(&status, MPI_CHAR, &len);
// Allocate buffer space for the message and receive it
char *message = new char[len];
MPI_Recv(message, len, MPI_CHAR,
0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << "Rank 1 received: " << message << endl;
delete [] message;
}
MPI_Finalize();
return 0;
}
用mpic++ -o mpihi.exe mpihi.cc
编译并执行:
$ mpiexec -np 2 ./mpihi.exe
Rank 1 received: Hi!
如果消息的长度预先修复,则可以简化代码。可以使用Boost.MPI
进一步简化它,但我从未使用过它,因此我无法为您提供示例。
MPI的美好事物是其保证的消息传递及其抽象互连的细节的方式。您可以通过为mpiexec
提供适当的选项来更改两个MPI进程的位置。如果两个过程都放置在相同的物理节点上,则将共享内存用于传达消息。如果放置在不同的节点上,将使用某些网络机制。
当然,这完全取决于您的需求。MPI库是具有大量支持基础架构的复杂代码部分,例如您需要通过专用启动器程序(在大多数情况下mpiexec
或mpirun
)运行代码,并且您不能使用MPI简单地连接两个随机过程(即,您确实确实有通过mpiexec
/mpirun
启动它们)。
相关文章:
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- C++ 两台不同机器之间通过wifi进行套接字通信
- 如何使用Messenger示例连接两台机器
- Visual Studio 中的套接字 c++ 应用程序无法通过两台计算机之间的以太网连接工作
- 两台计算机之间的行为不同
- 可能的编译器错误:在两台机器之间使用英特尔编译器的boost bessel函数会产生奇怪的结果
- 消息在网络上的两台机器之间传递
- WHy 选择挂起 => 两台 UDP 服务器(广播)
- Qt:如何将AA_UseHighDpiPixmaps用于两台显示器
- 两台具有相同 IP 地址的计算机 - 使用套接字的聊天应用程序
- BST搜索函数在一台机器上返回true,在另一台机器中返回false
- OpenCl代码在一台机器上工作,但我在另一台机器中得到了CL_INVALID_KERNEL_ARGS
- 在同一台机器上,是否保证两倍值的不精确性是一致的
- 一个源需要在多台机器上进行不同的编译
- 将消息 JavaFX gui 交换到同一台机器上的C++应用程序
- 使用以太网电缆在两台 Mac 之间自动传输文件
- 我相信 -O2 标志导致我不得不在每台机器上重新编译.为什么
- 从两台机器调用C++dll
- 在同一台机器上运行两个IncrediBuild
- C++程序在两台不同的机器上编译.共享库在一个上正常工作.另一方面,类方法返回NULL