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