Compadre  1.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 **arg, bool print_status) :
7  _num_threads(-1), _numa(-1), _device(-1), _ngpu(-1) {
8 
9  // determine if Kokkos is already initialized
10  // if it has been, get the parameters needed from it
11  bool preinitialized = Kokkos::is_initialized();
12 
13  if (!preinitialized) {
14  Kokkos::initialize(narg, arg);
15  _called_initialize = true;
16  }
17 
18  // get parameters
19  // set parameters
20  retrievePreviouslyInstantiatedKokkosInitArguments();
21 
22  if (print_status && preinitialized) {
23  printf("Kokkos already initialized.");
24  }
25 
26  if (print_status) {
27  // call status
28  this->status();
29  }
30 
31  // MPI
32  //char *str;
33  //if ((str = getenv("SLURM_LOCALID"))) {
34  // int local_rank = atoi(str);
35  // device = local_rank % ngpu;
36  // if (device >= skip_gpu) device++;
37  //}
38  //if ((str = getenv("MV2_COMM_WORLD_LOCAL_RANK"))) {
39  // int local_rank = atoi(str);
40  // device = local_rank % ngpu;
41  // if (device >= skip_gpu) device++;
42  //}
43  //if ((str = getenv("OMPI_COMM_WORLD_LOCAL_RANK"))) {
44  // int local_rank = atoi(str);
45  // device = local_rank % ngpu;
46  // if (device >= skip_gpu) device++;
47  //}
48 }
49 
50 KokkosParser::KokkosParser(int num_threads, int numa, int device, int ngpu, bool print_status) :
51  _num_threads(num_threads), _numa(numa), _device(device), _ngpu(ngpu) {
52 
53 //#ifdef KOKKOS_HAVE_CUDA
54 // // only written for handling one gpu
55 // compadre_assert_release((ngpu == 1) && "Only one GPU supported at this time.");
56 //#else
57 // ngpu = 0;
58 //#endif
59 //#ifdef KOKKOS_HAVE_CUDA
60 // compadre_assert_release((ngpu > 0) && "Kokkos has been compiled for CUDA but no GPUs are requested");
61 //#endif
62 
63  // returns 1 if we initialized
65 
66  if (print_status && _called_initialize==1) {
67  printf("KOKKOS attempted initialization using settings:\n");
68  this->status();
70  printf("KOKKOS recalculated and initiatized using settings:\n");
71  this->status();
72  } else if (_called_initialize==0) {
73  // could be improved by retrieving the parameters Kokkos was initialized with
74  // get parameters
75  // set parameters
77 
78  if (print_status) {
79  printf("Kokkos already initialized.");
80  this->status();
81  }
82  } else if (print_status) {
83  printf("Kokkos failed to initialize.");
84  }
85 
86 }
87 
88 Kokkos::InitArguments KokkosParser::createInitArguments() const {
89  Kokkos::InitArguments args;
90  args.num_threads = _num_threads;
91  args.num_numa = _numa;
92  args.device_id = _device;
93  return args;
94 }
95 
97 // NUMA parts are not tested, and only work for 1 numa region
98 #ifdef COMPADRE_USE_CUDA
99  //auto cuda_space = Kokkos::DefaultExecutionSpace;
100  _device = 0;//cuda_space.cuda_device();
101  _ngpu = 1;
102  _numa = 0;
103  _num_threads = 0;
104 #else
105  _device = 0;
106  _ngpu = 0;
107 #endif
108 
109 #ifdef KOKKOS_ENABLE_THREADS
110  //_num_threads = Kokkos::HostSpace::execution_space::get_current_max_threads();
111  _numa = 1;
112  _num_threads = Kokkos::HostSpace::execution_space::concurrency();//impl_get_current_max_threads();
113 #endif
114 #ifdef KOKKOS_ENABLE_OPENMP
115  //_num_threads = Kokkos::HostSpace::execution_space::get_current_max_threads();
116  _numa = 1;
117  _num_threads = Kokkos::HostSpace::execution_space::concurrency();//impl_get_current_max_threads();
118 #endif
119 }
120 
122  // return codes:
123  // 1 - success
124  // 0 - already initialized
125  // -1 - failed for some other reason
126 
127  // determine if Kokkos is already initialized
128  // if it has been, get the parameters needed from it
129  bool preinitialized = Kokkos::is_initialized();
130 
131  // if already initialized, return
132  if (preinitialized) {
133  return 0;
134  } else {
135  try {
136  auto our_args = this->createInitArguments();
137  Kokkos::initialize(our_args);
138  return 1;
139  } catch (const std::exception& e) {
140  std::cout << e.what() << std::endl;
141  throw e;
142  } catch (...) {
143  return -1;
144  }
145  }
146 }
147 
148 int KokkosParser::finalize(bool hard_finalize) {
149  if (hard_finalize || _called_initialize==1) {
150  try {
151  Kokkos::finalize();
152  _called_initialize = 0; // reset since we finalized
153  return 1;
154  } catch (...) {
155  return 0;
156  }
157  } else {
158  return 1;
159  }
160 }
161 
162 void KokkosParser::status() const {
163 #ifdef COMPADRE_USE_CUDA
164  printf("KOKKOS mode is enabled on GPU with nthreads: %d, numa: %d, device_id: %d\n", getNumberOfThreads(), getNuma(), getDeviceID());
165 #else
166  printf("KOKKOS mode is enabled on CPU with nthreads: %d, numa: %d\n", getNumberOfThreads(), getNuma());
167 #endif
168 }
Kokkos::InitArguments createInitArguments() const
void retrievePreviouslyInstantiatedKokkosInitArguments()
int finalize(bool hard_finalize=false)