TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
Classes | Public Member Functions | Static Public Member Functions | List of all members
cherrypi::ForkServer Class Reference

This class lets us fork when using MPI. More...

#include <forkserver.h>

Public Member Functions

 ForkServer ()
 
 ~ForkServer ()
 
std::tuple< int, int, int > execute (std::vector< std::string > const &command, std::vector< EnvVar > const &env)
 Execute command with environment. More...
 
template<typename F , typename... Args>
int fork (F &&f, Args &&...args)
 fork and call f with the specified arguments. More...
 
int waitpid (int pid)
 

Static Public Member Functions

static ForkServerinstance ()
 
static void startForkServer ()
 
static void endForkServer ()
 

Detailed Description

This class lets us fork when using MPI.

You must call ForkServer::startForkServer() before mpi/gloo is initialized, and before any threads are created. The best place to call it is in main, after parsing command line arguments, and before cherrypi::init(). Example usage:

ForkServer::startForkServer(); int pid = ForkServer::instance().fork([](std::string str) { VLOG(0) << "This is a new process with message: " << str; }, std::string("hello world")); ForkServer::instance().waitpid(pid);

Constructor & Destructor Documentation

cherrypi::ForkServer::ForkServer ( )
cherrypi::ForkServer::~ForkServer ( )

Member Function Documentation

void cherrypi::ForkServer::endForkServer ( )
static
std::tuple< int, int, int > cherrypi::ForkServer::execute ( std::vector< std::string > const &  command,
std::vector< EnvVar > const &  env 
)

Execute command with environment.

returns rfd, wfd, pid, where rfd and wfd is the read and write descriptor to stdout of the new process.

template<typename F , typename... Args>
int cherrypi::ForkServer::fork ( F &&  f,
Args &&...  args 
)
inline

fork and call f with the specified arguments.

f must be trivially copyable, args must be cereal serializable. You should not pass any pointers or references (either through argument or lambda capture), except to globals, since they will not be valid in the new process. There are no restrictions on what code can be executed in the function, but keep in mind that it runs in a new process with a single thread, as-if running from the point in the program where startForkServer was called. It is highly recommended to call waitpid at some point with the returned pid, because linux requires it in order to reap children and avoid a defunct process for every fork. returns pid

ForkServer & cherrypi::ForkServer::instance ( )
static
void cherrypi::ForkServer::startForkServer ( )
static
int cherrypi::ForkServer::waitpid ( int  pid)

The documentation for this class was generated from the following files: