From 10895f0ea5ae2ed371cba1cd1250921418ad4a43 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Thu, 3 Jan 2019 15:03:35 -0500 Subject: [PATCH] RuntimeDeviceTracker DeepyCopy is better optimized. The DeepCopy method is used when a ScopedGlobalRuntimeDeviceTracker is constructed. This in turn causes the rebuilding of the device names and states which isn't a free operation. Now we copy the already computed information. This was noticeable when using ArrayHandleTransform since it uses ScopedGlobalRuntimeDeviceTracker when construction host side portals. --- vtkm/cont/RuntimeDeviceTracker.cxx | 69 +++++++++++++++++------------- vtkm/cont/RuntimeDeviceTracker.h | 8 ++-- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/vtkm/cont/RuntimeDeviceTracker.cxx b/vtkm/cont/RuntimeDeviceTracker.cxx index 8a454758d..244afba0a 100644 --- a/vtkm/cont/RuntimeDeviceTracker.cxx +++ b/vtkm/cont/RuntimeDeviceTracker.cxx @@ -69,6 +69,10 @@ struct VTKM_NEVER_EXPORT GetDeviceNameFunctor } }; +#if !(defined(VTKM_CLANG) && (__apple_build_version__ < 8000000)) +thread_local static vtkm::cont::RuntimeDeviceTracker runtimeDeviceTracker; +#endif + } // end anon namespace namespace vtkm @@ -101,7 +105,7 @@ struct RuntimeDeviceTrackerFunctor VTKM_CONT RuntimeDeviceTracker::RuntimeDeviceTracker() - : Internals(new detail::RuntimeDeviceTrackerInternals) + : Internals(std::make_shared()) { GetDeviceNameFunctor functor(this->Internals->DeviceNames); vtkm::ListForEach(functor, VTKM_DEFAULT_DEVICE_ADAPTER_LIST_TAG()); @@ -176,9 +180,7 @@ void RuntimeDeviceTracker::Reset() VTKM_CONT vtkm::cont::RuntimeDeviceTracker RuntimeDeviceTracker::DeepCopy() const { - vtkm::cont::RuntimeDeviceTracker dest; - dest.DeepCopy(*this); - return dest; + return vtkm::cont::RuntimeDeviceTracker(this->Internals); } VTKM_CONT @@ -188,6 +190,15 @@ void RuntimeDeviceTracker::DeepCopy(const vtkm::cont::RuntimeDeviceTracker& src) src.Internals->RuntimeValid, VTKM_MAX_DEVICE_ADAPTER_ID, this->Internals->RuntimeValid); } +VTKM_CONT +RuntimeDeviceTracker::RuntimeDeviceTracker( + const std::shared_ptr& internals) + : Internals(std::make_shared()) +{ + std::copy_n(internals->RuntimeValid, VTKM_MAX_DEVICE_ADAPTER_ID, this->Internals->RuntimeValid); + std::copy_n(internals->DeviceNames, VTKM_MAX_DEVICE_ADAPTER_ID, this->Internals->DeviceNames); +} + VTKM_CONT void RuntimeDeviceTracker::ForceDeviceImpl(vtkm::cont::DeviceAdapterId deviceId, bool runtimeExists) { @@ -208,31 +219,6 @@ void RuntimeDeviceTracker::ForceDeviceImpl(vtkm::cont::DeviceAdapterId deviceId, this->Internals->RuntimeValid[deviceId.GetValue()] = runtimeExists; } -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(); - - 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; - } -#else - return runtimeDeviceTracker; -#endif -} - VTKM_CONT void RuntimeDeviceTracker::ForceDevice(DeviceAdapterId id) { @@ -302,5 +288,30 @@ DeviceAdapterId RuntimeDeviceTracker::GetDeviceAdapterId(DeviceAdapterNameType n return vtkm::cont::DeviceAdapterTagUndefined{}; } + +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(); + + 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; + } +#else + return runtimeDeviceTracker; +#endif +} } } // namespace vtkm::cont diff --git a/vtkm/cont/RuntimeDeviceTracker.h b/vtkm/cont/RuntimeDeviceTracker.h index a17bfb181..dd97e4e61 100644 --- a/vtkm/cont/RuntimeDeviceTracker.h +++ b/vtkm/cont/RuntimeDeviceTracker.h @@ -217,6 +217,11 @@ public: private: std::shared_ptr Internals; + // Deep Copy constructor. + VTKM_CONT_EXPORT + VTKM_CONT + RuntimeDeviceTracker(const std::shared_ptr& internals); + VTKM_CONT_EXPORT VTKM_CONT void CheckDevice(vtkm::cont::DeviceAdapterId deviceId) const; @@ -243,9 +248,6 @@ private: /// 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();