18 #ifndef viskores_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
19 #define viskores_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h
27 #include <Kokkos_Core.hpp>
43 RuntimeDeviceConfigReturnCode GetArgFromList(
const std::vector<std::string>& argList,
44 const std::string& argName,
50 for (
auto argItr = argList.rbegin(); argItr != argList.rend(); argItr++)
52 if (argItr->rfind(argName, 0) == 0)
54 if (argItr->size() == argName.size())
56 value = std::stoi(*(--argItr), &pos, 10);
57 return RuntimeDeviceConfigReturnCode::SUCCESS;
61 value = std::stoi(argItr->substr(argName.size() + 1), &pos, 10);
62 return RuntimeDeviceConfigReturnCode::SUCCESS;
67 catch (
const std::invalid_argument&)
71 "Unable to get arg " + argName +
72 "from kokkos argList, invalid argument thrown... This shouldn't have happened");
73 return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
75 catch (
const std::out_of_range&)
78 "Unable to get arg " + argName +
79 "from kokkos argList, out of range thrown... This shouldn't have happened");
80 return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
82 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
88 class RuntimeDeviceConfiguration<
viskores::cont::DeviceAdapterTagKokkos>
89 :
public viskores::cont::internal::RuntimeDeviceConfigurationBase
97 VISKORES_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(
100 if (Kokkos::is_initialized())
104 "SetThreads was called but Kokkos was already initailized! Updates will not be applied.");
105 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
107 this->KokkosArguments.insert(this->KokkosArguments.begin(),
108 "--kokkos-num-threads=" + std::to_string(value));
109 return RuntimeDeviceConfigReturnCode::SUCCESS;
112 VISKORES_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
115 if (Kokkos::is_initialized())
119 "SetDeviceInstance was called but Kokkos was already initailized! Updates will "
121 return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
123 this->KokkosArguments.insert(this->KokkosArguments.begin(),
124 "--kokkos-device-id=" + std::to_string(value));
125 return RuntimeDeviceConfigReturnCode::SUCCESS;
128 VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(
131 return GetArgFromList(this->KokkosArguments,
"--kokkos-num-threads", value);
134 VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
137 return GetArgFromList(this->KokkosArguments,
"--kokkos-device-id", value);
144 VISKORES_CONT virtual void ParseExtraArguments(
int& argc,
char* argv[])
override final
146 if (argc > 0 && argv)
148 this->KokkosArguments.insert(this->KokkosArguments.end(), argv, argv + argc);
162 VISKORES_CONT virtual void InitializeSubsystem() override final
164 if (!Kokkos::is_initialized())
166 std::vector<char*> argv;
167 for (
auto& arg : this->KokkosArguments)
169 argv.push_back(&arg[0]);
171 int size = argv.size();
172 Kokkos::initialize(size, argv.data());
173 std::atexit(Kokkos::finalize);
179 "Attempted to Re-initialize Kokkos! The Kokkos subsystem can only be initialized once");
184 std::vector<std::string> KokkosArguments;
191 #endif //viskores_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h