VTK-m  2.0
RuntimeDeviceConfigurationKokkos.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
11 #define vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
12 
14 #include <vtkm/cont/Logging.h>
17 
18 VTKM_THIRDPARTY_PRE_INCLUDE
19 #include <Kokkos_Core.hpp>
20 VTKM_THIRDPARTY_POST_INCLUDE
21 
22 #include <cstring>
23 #include <vector>
24 
25 namespace vtkm
26 {
27 namespace cont
28 {
29 namespace internal
30 {
31 
32 namespace
33 {
35 RuntimeDeviceConfigReturnCode GetArgFromList(const std::vector<std::string>& argList,
36  const std::string& argName,
37  vtkm::Id& value)
38 {
39  size_t pos;
40  try
41  {
42  for (auto argItr = argList.rbegin(); argItr != argList.rend(); argItr++)
43  {
44  if (argItr->rfind(argName, 0) == 0)
45  {
46  if (argItr->size() == argName.size())
47  {
48  value = std::stoi(*(--argItr), &pos, 10);
49  return RuntimeDeviceConfigReturnCode::SUCCESS;
50  }
51  else
52  {
53  value = std::stoi(argItr->substr(argName.size() + 1), &pos, 10);
54  return RuntimeDeviceConfigReturnCode::SUCCESS;
55  }
56  }
57  }
58  }
59  catch (const std::invalid_argument&)
60  {
62  "Unable to get arg " + argName +
63  "from kokkos argList, invalid argument thrown... This shouldn't have happened");
64  return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
65  }
66  catch (const std::out_of_range&)
67  {
69  "Unable to get arg " + argName +
70  "from kokkos argList, out of range thrown... This shouldn't have happened");
71  return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
72  }
73  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
74 }
75 
76 } // namespace anonymous
77 
78 template <>
79 class RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagKokkos>
80  : public vtkm::cont::internal::RuntimeDeviceConfigurationBase
81 {
82 public:
83  VTKM_CONT vtkm::cont::DeviceAdapterId GetDevice() const override final
84  {
85  return vtkm::cont::DeviceAdapterTagKokkos{};
86  }
87 
88  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(const vtkm::Id& value) override final
89  {
90  if (Kokkos::is_initialized())
91  {
92  VTKM_LOG_S(
94  "SetThreads was called but Kokkos was already initailized! Updates will not be applied.");
95  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
96  }
97  this->KokkosArguments.insert(this->KokkosArguments.begin(),
98  "--kokkos-threads=" + std::to_string(value));
99  return RuntimeDeviceConfigReturnCode::SUCCESS;
100  }
101 
102  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetNumaRegions(
103  const vtkm::Id& value) override final
104  {
105  if (Kokkos::is_initialized())
106  {
108  "SetNumaRegions was called but Kokkos was already initailized! Updates will not "
109  "be applied.");
110  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
111  }
112  this->KokkosArguments.insert(this->KokkosArguments.begin(),
113  "--kokkos-numa=" + std::to_string(value));
114  return RuntimeDeviceConfigReturnCode::SUCCESS;
115  }
116 
117  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
118  const vtkm::Id& value) override final
119  {
120  if (Kokkos::is_initialized())
121  {
123  "SetDeviceInstance was called but Kokkos was already initailized! Updates will "
124  "not be applied.");
125  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
126  }
127  this->KokkosArguments.insert(this->KokkosArguments.begin(),
128  "--kokkos-device-id=" + std::to_string(value));
129  return RuntimeDeviceConfigReturnCode::SUCCESS;
130  }
131 
132  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(vtkm::Id& value) const override final
133  {
134  return GetArgFromList(this->KokkosArguments, "--kokkos-threads", value);
135  }
136 
137  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetNumaRegions(
138  vtkm::Id& value) const override final
139  {
140  return GetArgFromList(this->KokkosArguments, "--kokkos-numa", value);
141  }
142 
143  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
144  vtkm::Id& value) const override final
145  {
146  return GetArgFromList(this->KokkosArguments, "--kokkos-device-id", value);
147  }
148 
149 protected:
153  VTKM_CONT virtual void ParseExtraArguments(int& argc, char* argv[]) override final
154  {
155  if (argc > 0 && argv)
156  {
157  this->KokkosArguments.insert(this->KokkosArguments.end(), argv, argv + argc);
158  }
159  }
160 
171  VTKM_CONT virtual void InitializeSubsystem() override final
172  {
173  if (!Kokkos::is_initialized())
174  {
175  std::vector<char*> argv;
176  for (auto& arg : this->KokkosArguments)
177  {
178  argv.push_back(&arg[0]);
179  }
180  int size = argv.size();
181  Kokkos::initialize(size, argv.data());
182  std::atexit(Kokkos::finalize);
183  }
184  else
185  {
186  VTKM_LOG_S(
188  "Attempted to Re-initialize Kokkos! The Kokkos subsystem can only be initialized once");
189  }
190  }
191 
192 private:
193  std::vector<std::string> KokkosArguments;
194 };
195 
196 } // namespace vtkm::cont::internal
197 } // namespace vtkm::cont
198 } // namespace vtkm
199 
200 #endif //vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
DeviceAdapterTagKokkos.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
RuntimeDeviceConfiguration.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::LogLevel::Error
@ Error
Important but non-fatal errors, such as device fail-over.
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:261
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
ErrorInternal.h
Logging.h
Logging utilities.