From 9ae3098af8b28c1e554d9ab8cf8425a52e37ac7e Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Tue, 14 Nov 2017 19:42:48 -0500 Subject: [PATCH] Give each thread using vtk a separate runtime device tracker --- vtkm/cont/RuntimeDeviceTracker.cxx | 21 +++++++++++++++++++-- vtkm/cont/RuntimeDeviceTracker.h | 6 +++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/vtkm/cont/RuntimeDeviceTracker.cxx b/vtkm/cont/RuntimeDeviceTracker.cxx index 33ef3b43c..d0b31714e 100644 --- a/vtkm/cont/RuntimeDeviceTracker.cxx +++ b/vtkm/cont/RuntimeDeviceTracker.cxx @@ -29,7 +29,10 @@ #include #include +#include +#include #include +#include #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 globalTrackers; + std::thread::id this_id = std::this_thread::get_id(); + + std::unique_lock 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 cf0f4ad8d..ebdfe5e72 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