10 #ifndef vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h 
   11 #define vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h 
   18 VTKM_THIRDPARTY_PRE_INCLUDE
 
   19 #include <Kokkos_Core.hpp> 
   20 VTKM_THIRDPARTY_POST_INCLUDE
 
   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
 
   85     return vtkm::cont::DeviceAdapterTagKokkos{};
 
   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-threads=" + std::to_string(value));
 
   99     return RuntimeDeviceConfigReturnCode::SUCCESS;
 
  102   VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetNumaRegions(
 
  103     const vtkm::Id& value) 
override final 
  105     if (Kokkos::is_initialized())
 
  108                  "SetNumaRegions was called but Kokkos was already initailized! Updates will not " 
  110       return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
 
  112     this->KokkosArguments.insert(this->KokkosArguments.begin(),
 
  113                                  "--kokkos-numa=" + std::to_string(value));
 
  114     return RuntimeDeviceConfigReturnCode::SUCCESS;
 
  117   VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
 
  118     const vtkm::Id& value) 
override final 
  120     if (Kokkos::is_initialized())
 
  123                  "SetDeviceInstance was called but Kokkos was already initailized! Updates will " 
  125       return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
 
  127     this->KokkosArguments.insert(this->KokkosArguments.begin(),
 
  128                                  "--kokkos-device-id=" + std::to_string(value));
 
  129     return RuntimeDeviceConfigReturnCode::SUCCESS;
 
  132   VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(
vtkm::Id& value) 
const override final 
  134     return GetArgFromList(this->KokkosArguments, 
"--kokkos-threads", value);
 
  137   VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetNumaRegions(
 
  138     vtkm::Id& value) 
const override final 
  140     return GetArgFromList(this->KokkosArguments, 
"--kokkos-numa", value);
 
  143   VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
 
  144     vtkm::Id& value) 
const override final 
  146     return GetArgFromList(this->KokkosArguments, 
"--kokkos-device-id", value);
 
  153   VTKM_CONT virtual void ParseExtraArguments(
int& argc, 
char* argv[]) 
override final 
  155     if (argc > 0 && argv)
 
  157       this->KokkosArguments.insert(this->KokkosArguments.end(), argv, argv + argc);
 
  171   VTKM_CONT virtual void InitializeSubsystem() override final
 
  173     if (!Kokkos::is_initialized())
 
  175       std::vector<char*> argv;
 
  176       for (
auto& arg : this->KokkosArguments)
 
  178         argv.push_back(&arg[0]);
 
  180       int size = argv.size();
 
  181       Kokkos::initialize(size, argv.data());
 
  182       std::atexit(Kokkos::finalize);
 
  188         "Attempted to Re-initialize Kokkos! The Kokkos subsystem can only be initialized once");
 
  193   std::vector<std::string> KokkosArguments;
 
  200 #endif //vtk_m_cont_kokkos_internal_RuntimeDeviceConfigurationKokkos_h