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.
This commit is contained in:
Robert Maynard 2019-01-03 15:03:35 -05:00
parent f955103cad
commit 10895f0ea5
2 changed files with 45 additions and 32 deletions

@ -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<detail::RuntimeDeviceTrackerInternals>())
{
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<detail::RuntimeDeviceTrackerInternals>& internals)
: Internals(std::make_shared<detail::RuntimeDeviceTrackerInternals>())
{
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<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;
}
#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<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;
}
#else
return runtimeDeviceTracker;
#endif
}
}
} // namespace vtkm::cont

@ -217,6 +217,11 @@ public:
private:
std::shared_ptr<detail::RuntimeDeviceTrackerInternals> Internals;
// Deep Copy constructor.
VTKM_CONT_EXPORT
VTKM_CONT
RuntimeDeviceTracker(const std::shared_ptr<detail::RuntimeDeviceTrackerInternals>& 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();