我有一个主应用程序,它产生一个本身产生两个奴隶的工人。从应用程序将其输出写入标准输出。我的想法是将标准输出绑定到工作应用程序中的不同流,以便能够将从属的输出存储在一个变量中并将其发送给处理输出的主控。但是,从属设备的标准输出没有正确重定向,仍然出现在控制台上。工作应用程序中的缓冲区保持为空。我是否遗漏了某些东西,或者这在我做这件事的方式上是不可能的?如果是这样,任何有关如何以不同方式处理此问题的建议将不胜感激。我在 Gentoo 上使用 Open MPI 1.6.5,这是我的应用程序的源代码:
大师.cpp
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char appExe[] = "worker";
char *appArg[] = {NULL};
int maxProcs = 1;
int myRank;
MPI_Comm childComm;
int spawnError;
// Initialize
MPI_Init(&argc, &argv);
// Rank
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
// Spawn application
MPI_Comm_spawn(appExe, appArg, maxProcs, MPI_INFO_NULL, myRank, MPI_COMM_SELF, &childComm, &spawnError);
// Receive length of message from worker
int len;
MPI_Recv(&len, 1, MPI_INT, 0, MPI_ANY_TAG, childComm, MPI_STATUS_IGNORE);
// Receive actual message from worker
char *buf = new char[len];
MPI_Recv(buf, len, MPI_CHAR, 0, MPI_ANY_TAG, childComm, MPI_STATUS_IGNORE);
cout << "master: Got the following from worker: " << buf << endl;
// Finalize
MPI_Finalize();
return 0;
}
工人.cpp
#include "mpi.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
char appExe[] = "slave";
char *appArg[] = {NULL};
int maxProcs = 2;
int myRank, parentRank;
MPI_Comm childComm, parentComm;
int spawnError[maxProcs];
// Initialize
MPI_Init(&argc, &argv);
// Rank
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
// Get parent
MPI_Comm_get_parent(&parentComm);
// Bind stdout to new_buffer
stringstream new_buffer;
streambuf *old_buffer = cout.rdbuf(new_buffer.rdbuf());
// Spawn application
MPI_Comm_spawn(appExe, appArg, maxProcs, MPI_INFO_NULL, myRank, MPI_COMM_SELF, &childComm, spawnError);
// Enter barrier
MPI_Barrier(childComm);
// Reset stdout to old_buffer
cout.rdbuf(old_buffer);
// Make a string
string tmp = new_buffer.str();
// Make a character array from string
const char* cstr = tmp.c_str();
cout << "worker: Got the following from slaves: " << cstr << endl;
// Send length of message to master
int len = sizeof(cstr);
MPI_Send(&len, 1, MPI_INT, 0, 0, parentComm);
// Send actual message
MPI_Send(&cstr, len, MPI_CHAR, 0, 0, parentComm);
// Finalize
MPI_Finalize();
return 0;
}
从属.cpp
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
MPI_Comm parent;
// Initialize
MPI_Init(&argc, &argv);
// Get parent
MPI_Comm_get_parent(&parent);
// Say hello
cout << "slave: Hi there!" << endl;
// Enter barrier
if (parent != MPI_COMM_NULL)
MPI_Barrier(parent);
// Finalize
MPI_Finalize();
return 0;
}