diff --git a/vtkm/cont/RuntimeDeviceTracker.cxx b/vtkm/cont/RuntimeDeviceTracker.cxx index 33ef3b43cad609fc0d1e70da3970a20fc38e7420..d0b31714ec80d39fb2c953f615cc71652ffe84a6 100644 --- a/vtkm/cont/RuntimeDeviceTracker.cxx +++ b/vtkm/cont/RuntimeDeviceTracker.cxx @@ -29,7 +29,10 @@ #include <vtkm/cont/tbb/DeviceAdapterTBB.h> #include <algorithm> +#include <map> +#include <mutex> #include <sstream> +#include <thread> #define VTKM_MAX_DEVICE_ADAPTER_ID 8 @@ -155,8 +158,22 @@ void RuntimeDeviceTracker::ForceDeviceImpl(vtkm::cont::DeviceAdapterId deviceId, VTKM_CONT vtkm::cont::RuntimeDeviceTracker GetGlobalRuntimeDeviceTracker() { - static vtkm::cont::RuntimeDeviceTracker globalTracker; - return globalTracker; + static std::mutex mtx; + static std::map<std::thread::id, vtkm::cont::RuntimeDeviceTracker> globalTrackers; + std::thread::id this_id = std::this_thread::get_id(); + + std::unique_lock<std::mutex> lock(mtx); + auto iter = globalTrackers.find(this_id); + if (iter != globalTrackers.end()) + { + return iter->second; + } + else + { + vtkm::cont::RuntimeDeviceTracker tracker; + globalTrackers[this_id] = tracker; + return tracker; + } } } } // namespace vtkm::cont diff --git a/vtkm/cont/RuntimeDeviceTracker.h b/vtkm/cont/RuntimeDeviceTracker.h index cf0f4ad8d04415b2a56ff4723336ad5656c2692e..ebdfe5e72bd01247537a4d5806140deb919abe23 100644 --- a/vtkm/cont/RuntimeDeviceTracker.h +++ b/vtkm/cont/RuntimeDeviceTracker.h @@ -207,13 +207,13 @@ private: bool runtimeExists); }; -/// \brief Get the global \c RuntimeDeviceTracker. +/// \brief Get the \c RuntimeDeviceTracker for the current thread. /// /// Many features in VTK-m will attempt to run algorithms on the "best /// available device." This often is determined at runtime as failures in /// one device are recorded and that device is disabled. To prevent having -/// to check over and over again, VTK-m features generally use the global -/// device adapter so that these choices are marked and shared. +/// to check over and over again, VTK-m uses per thread runtime device tracker +/// so that these choices are marked and shared. /// VTKM_CONT_EXPORT VTKM_CONT