Compadre  1.3.3
Compadre_KokkosParser.cpp
Go to the documentation of this file.
2 
3 using namespace Compadre;
4 
5 // for command line arguments, pass them directly in to Kokkos
6 KokkosParser::KokkosParser(int narg, char* args[], bool print_status) {
7  this->initialize(narg, args, print_status);
8 }
9 
10 KokkosParser::KokkosParser(std::vector<std::string> stdvec_args, bool print_status) {
11  std::vector<char*> char_args;
12  for (const auto& arg : stdvec_args) {
13  char_args.push_back((char*)arg.data());
14  }
15  char_args.push_back(nullptr);
16  int narg = (int)stdvec_args.size();
17  this->initialize(narg, char_args.data(), print_status);
18 }
19 
20 KokkosParser::KokkosParser(bool print_status) {
21  std::vector<std::string> stdvec_args;
22  stdvec_args.push_back("placeholder");
23  std::vector<char*> char_args;
24  for (const auto& arg : stdvec_args) {
25  char_args.push_back((char*)arg.data());
26  }
27  char_args.push_back(nullptr);
28  this->initialize(1, char_args.data(), print_status);
29 }
30 
31 int KokkosParser::initialize(int narg, char* argv[], bool print_status) {
32  // return codes:
33  // 1 - success
34  // 0 - already initialized
35  // -1 - failed for some other reason
36 
37  // determine if Kokkos is already initialized
38  // if it has been, get the parameters needed from it
39  bool preinitialized = Kokkos::is_initialized();
40 
41  // if already initialized, return
42  if (preinitialized) {
43  if (print_status) printf("Previously initialized.\n");
44  return 0;
45  } else {
46  compadre_assert_release((narg!=0 && argv!=NULL) && "Invalid input to initialize()\n");
47  try {
48  Kokkos::initialize(narg, argv);
49  bool success = Kokkos::is_initialized();
50  compadre_assert_release(success && "Kokkos did not initialize successfully.\n");
51  if (print_status) {
52  this->status();
53  }
54  return 1;
55  } catch (const std::exception& e) {
56  std::cout << e.what() << std::endl;
57  throw e;
58  } catch (...) {
59  return -1;
60  }
61  }
62 }
63 
64 int KokkosParser::finalize(bool hard_finalize) {
65  if (hard_finalize || _called_initialize==1) {
66  try {
67  Kokkos::finalize();
68  _called_initialize = 0; // reset since we finalized
69  return 1;
70  } catch (...) {
71  return 0;
72  }
73  } else {
74  return 1;
75  }
76 }
77 
78 void KokkosParser::status() const {
79  Kokkos::print_configuration(std::cout, true);
80 }
int initialize(int argc, char *[], bool print_status=false)
int finalize(bool hard_finalize=false)
#define compadre_assert_release(condition)
compadre_assert_release is used for assertions that should always be checked, but generally are not e...