diff --git a/docs/changelog/use-thread_local-in-GetGlobalRuntimeDeviceTracker.md b/docs/changelog/use-thread_local-in-GetGlobalRuntimeDeviceTracker.md new file mode 100644 index 000000000..6a19747e3 --- /dev/null +++ b/docs/changelog/use-thread_local-in-GetGlobalRuntimeDeviceTracker.md @@ -0,0 +1,5 @@ +# Use thread_local in GetGlobalRuntimeDeviceTracker function if possible + +It will reduce the cost of getting the thread runtime device tracker, +and will have a better runtime overhead if user constructs a lot of +short lived threads that use VTK-m. diff --git a/vtkm/cont/RuntimeDeviceTracker.cxx b/vtkm/cont/RuntimeDeviceTracker.cxx index d0b31714e..3ee091afd 100644 --- a/vtkm/cont/RuntimeDeviceTracker.cxx +++ b/vtkm/cont/RuntimeDeviceTracker.cxx @@ -158,6 +158,7 @@ void RuntimeDeviceTracker::ForceDeviceImpl(vtkm::cont::DeviceAdapterId deviceId, VTKM_CONT vtkm::cont::RuntimeDeviceTracker GetGlobalRuntimeDeviceTracker() { +#if defined(VTKM_CLANG) && (__apple_build_version__ < 8000000) static std::mutex mtx; static std::map globalTrackers; std::thread::id this_id = std::this_thread::get_id(); @@ -174,6 +175,9 @@ vtkm::cont::RuntimeDeviceTracker GetGlobalRuntimeDeviceTracker() globalTrackers[this_id] = tracker; return tracker; } +#else + return runtimeDeviceTracker; +#endif } } } // namespace vtkm::cont diff --git a/vtkm/cont/RuntimeDeviceTracker.h b/vtkm/cont/RuntimeDeviceTracker.h index d42430796..1bc2944a2 100644 --- a/vtkm/cont/RuntimeDeviceTracker.h +++ b/vtkm/cont/RuntimeDeviceTracker.h @@ -237,6 +237,10 @@ private: /// to check over and over again, VTK-m uses per thread runtime device tracker /// so that these choices are marked and shared. /// +/// Xcode's clang only supports thread_local from version 8 +#if !(defined(VTKM_CLANG) && (__apple_build_version__ < 8000000)) +thread_local static vtkm::cont::RuntimeDeviceTracker runtimeDeviceTracker; +#endif VTKM_CONT_EXPORT VTKM_CONT vtkm::cont::RuntimeDeviceTracker GetGlobalRuntimeDeviceTracker();