SOPT
Sparse OPTimisation
common_mpi_catch_main.cc
Go to the documentation of this file.
1 #define CATCH_CONFIG_RUNNER
2 
3 #include "sopt/config.h"
4 #include <catch2/catch_all.hpp>
5 #include <memory>
6 #include <mpi.h>
7 #include <random>
8 #include <regex>
9 #include "sopt/logging.h"
10 #include "sopt/mpi/session.h"
11 
12 std::unique_ptr<std::mt19937_64> mersenne(new std::mt19937_64(0));
13 
14 int main(int argc, const char **argv) {
15  Catch::Session session; // There must be exactly once instance
16 
17  auto const mpi_session = sopt::mpi::init(argc, argv);
18 
19  // The following mess transforms the input arguments so that output files have different names
20  // on different processors
21  std::vector<std::string> arguments(argv, argv + argc);
22  auto output_opt = std::find_if(arguments.begin(), arguments.end(), [](std::string const &arg) {
23  if (arg == "-o" or arg == "--out") return true;
24  auto const N = std::string("--out=").size();
25  return arg.size() > N and arg.substr(0, N) == "--out=";
26  });
27  if (output_opt != arguments.end()) {
28  if (*output_opt == "-o" or *output_opt == "--out") output_opt += 1;
29  if (output_opt != arguments.end()) {
30  int rank;
31  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32  if (rank > 0)
33  *output_opt =
34  std::regex_replace(*output_opt, std::regex("\\.xml"), std::to_string(rank) + ".xml");
35  }
36  }
37 
38  // transforms the modified arguments to a C-style array of pointers.
39  std::vector<char const *> cargs(arguments.size());
40  std::transform(arguments.begin(), arguments.end(), cargs.begin(),
41  [](std::string const &c) { return c.c_str(); });
42 
43  int const returnCode = session.applyCommandLine(argc, const_cast<char **>(cargs.data()));
44  if (returnCode != 0) // Indicates a command line error
45  return returnCode;
46  mersenne.reset(new std::mt19937_64(session.configData().rngSeed));
47 
48  auto const result = session.run();
49 
50  return result;
51 }
int main(int argc, const char **argv)
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
std::vector< char const * > cargs
Definition: mpi_session.cc:10
std::shared_ptr< details::initializer > init(int argc, const char **argv)
Definition: session.cc:27