Use thread_local in GetGlobalRuntimeDeviceTracker 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.
This commit is contained in:
Haocheng LIU 2018-08-01 13:30:22 -04:00
parent 53d50015f3
commit c95db1fc78
3 changed files with 13 additions and 0 deletions

@ -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.

@ -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<std::thread::id, vtkm::cont::RuntimeDeviceTracker> 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

@ -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();