Process creation#

The following two programs illustrate dynamic creation of new processes and establishing a communication channel in the form of inter-communicator.

#include <cstdlib>
#include <iostream>
// include MPL header file
#include <mpl/mpl.hpp>

int main() {
  using namespace std::string_literals;
  // get a reference to communicator "world"
  const mpl::communicator &comm_world{mpl::environment::comm_world()};
  // spawn 2 new processes
  mpl::info info;
  info.set("host", "localhost");
  auto inter_comm{comm_world.spawn(0, 2, {"./process_creation_client"s}, info)};
  // broadcast a message to the created processes
  double message;
  if (comm_world.rank() == 0) {
    // root rank
    message = 1.23;
    inter_comm.bcast(mpl::root, message);
  } else
    // non-root ranks
    inter_comm.bcast(mpl::proc_null, message);

  return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
// include MPL header file
#include <mpl/mpl.hpp>

int main() {
  using namespace std::string_literals;
  // get a reference to communicator "world"
  const mpl::communicator &comm_world{mpl::environment::comm_world()};
  // spawn 2 new processes
  mpl::info info;
  info.set("host", "localhost");
  auto inter_comm{
      comm_world.spawn_multiple(0,
                                {{"./process_creation_client"s, "arg1"s},
                                 {"./process_creation_client"s, "arg1"s, "arg2"s},
                                 {"./process_creation_client"s, "arg1"s, "arg2"s, "arg3"s}},
                                {info, info, info})};
  // broadcast a message to the created processes
  double message;
  if (comm_world.rank() == 0) {
    // root rank
    message = 1.23;
    inter_comm.bcast(mpl::root, message);
  } else
    // non-root ranks
    inter_comm.bcast(mpl::proc_null, message);

  return EXIT_SUCCESS;
}

The corresponding source of the spawned process is given as shown below:

#include <cstdlib>
#include <iostream>
// include MPL header file
#include <mpl/mpl.hpp>

int main(int argc, char *argv[]) {
  using namespace std::string_literals;

  // get a reference to communicator "world"
  [[maybe_unused]] const mpl::communicator &comm_world{mpl::environment::comm_world()};
  // get the parent inter-communicator
  auto &inter_comm{mpl::inter_communicator::parent()};
  std::cout << "Hello world! I am running on \"" << mpl::environment::processor_name()
            << "\". My rank is " << inter_comm.rank() << " out of " << inter_comm.size()
            << " processes.\n";
  std::cout << "commandline arguments: ";
  for (int i{0}; i < argc; ++i)
    std::cout << argv[i] << ' ';
  std::cout << std::endl;
  double message;
  inter_comm.bcast(0, message);
  std::cout << "got: " << message << '\n';
  return EXIT_SUCCESS;
}