10 #ifndef vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
11 #define vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
19 #include <Kokkos_Core.hpp>
35 RuntimeDeviceConfigReturnCode GetArgFromList(
const std::vector<std::string>& argList,
36 const std::string& argName,
42 for (
auto argItr = argList.rbegin(); argItr != argList.rend(); argItr++)
44 if (argItr->rfind(argName, 0) == 0)
46 if (argItr->size() == argName.size())
48 value = std::stoi(*(--argItr), &pos, 10);
49 return RuntimeDeviceConfigReturnCode::SUCCESS;
53 value = std::stoi(argItr->substr(argName.size() + 1), &pos, 10);
54 return RuntimeDeviceConfigReturnCode::SUCCESS;
59 catch (
const std::invalid_argument&)
62 "Unable to get arg " + argName +
63 "from kokkos argList, invalid argument thrown... This shouldn't have happened");
64 return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
66 catch (
const std::out_of_range&)
69 "Unable to get arg " + argName +
70 "from kokkos argList, out of range thrown... This shouldn't have happened");
71 return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
73 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
79 class RuntimeDeviceConfiguration<
vtkm::cont::DeviceAdapterTagKokkos>
80 :
public vtkm::cont::internal::RuntimeDeviceConfigurationBase
88 VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(
const vtkm::Id& value)
override final
90 if (Kokkos::is_initialized())
94 "SetThreads was called but Kokkos was already initailized! Updates will not be applied.");
95 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
97 this->KokkosArguments.insert(this->KokkosArguments.begin(),
98 "--kokkos-num-threads=" + std::to_string(value));
99 return RuntimeDeviceConfigReturnCode::SUCCESS;
102 VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
103 const vtkm::Id& value)
override final
105 if (Kokkos::is_initialized())
108 "SetDeviceInstance was called but Kokkos was already initailized! Updates will "
110 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
112 this->KokkosArguments.insert(this->KokkosArguments.begin(),
113 "--kokkos-device-id=" + std::to_string(value));
114 return RuntimeDeviceConfigReturnCode::SUCCESS;
117 VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(
vtkm::Id& value)
const override final
119 return GetArgFromList(this->KokkosArguments,
"--kokkos-num-threads", value);
122 VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
123 vtkm::Id& value)
const override final
125 return GetArgFromList(this->KokkosArguments,
"--kokkos-device-id", value);
132 VTKM_CONT virtual void ParseExtraArguments(
int& argc,
char* argv[])
override final
134 if (argc > 0 && argv)
136 this->KokkosArguments.insert(this->KokkosArguments.end(), argv, argv + argc);
150 VTKM_CONT virtual void InitializeSubsystem() override final
152 if (!Kokkos::is_initialized())
154 std::vector<char*> argv;
155 for (
auto& arg : this->KokkosArguments)
157 argv.push_back(&arg[0]);
159 int size = argv.size();
160 Kokkos::initialize(size, argv.data());
161 std::atexit(Kokkos::finalize);
167 "Attempted to Re-initialize Kokkos! The Kokkos subsystem can only be initialized once");
172 std::vector<std::string> KokkosArguments;
179 #endif //vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h