2017-02-22 21:51:57 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2017-02-22 21:51:57 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/RuntimeDeviceTracker.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/ErrorBadValue.h>
|
|
|
|
|
2017-02-23 16:19:13 +00:00
|
|
|
#include <algorithm>
|
2017-11-15 00:42:48 +00:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2017-02-22 21:51:57 +00:00
|
|
|
#include <sstream>
|
2017-11-15 00:42:48 +00:00
|
|
|
#include <thread>
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
struct RuntimeDeviceTrackerInternals
|
|
|
|
{
|
2019-03-15 19:54:42 +00:00
|
|
|
bool RuntimeAllowed[VTKM_MAX_DEVICE_ADAPTER_ID];
|
2017-02-22 21:51:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2019-05-15 21:26:34 +00:00
|
|
|
RuntimeDeviceTracker::RuntimeDeviceTracker(detail::RuntimeDeviceTrackerInternals* details,
|
|
|
|
bool reset)
|
|
|
|
: Internals(details)
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2019-05-15 21:26:34 +00:00
|
|
|
if (reset)
|
|
|
|
{
|
|
|
|
this->Reset();
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
RuntimeDeviceTracker::~RuntimeDeviceTracker()
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void RuntimeDeviceTracker::CheckDevice(vtkm::cont::DeviceAdapterId deviceId) const
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-07-25 15:11:05 +00:00
|
|
|
if (!deviceId.IsValueValid())
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
|
|
|
std::stringstream message;
|
2018-08-29 20:47:51 +00:00
|
|
|
message << "Device '" << deviceId.GetName() << "' has invalid ID of "
|
|
|
|
<< (int)deviceId.GetValue();
|
2017-02-22 21:51:57 +00:00
|
|
|
throw vtkm::cont::ErrorBadValue(message.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2019-05-21 19:32:10 +00:00
|
|
|
bool RuntimeDeviceTracker::CanRunOn(vtkm::cont::DeviceAdapterId deviceId) const
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2019-05-21 19:32:10 +00:00
|
|
|
if (deviceId == vtkm::cont::DeviceAdapterTagAny{})
|
|
|
|
{ //If at least a single device is enabled, than any device is enabled
|
|
|
|
for (vtkm::Int8 i = 1; i < VTKM_MAX_DEVICE_ADAPTER_ID; ++i)
|
|
|
|
{
|
|
|
|
if (this->Internals->RuntimeAllowed[static_cast<std::size_t>(i)])
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->CheckDevice(deviceId);
|
|
|
|
return this->Internals->RuntimeAllowed[deviceId.GetValue()];
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void RuntimeDeviceTracker::SetDeviceState(vtkm::cont::DeviceAdapterId deviceId, bool state)
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-08-29 20:47:51 +00:00
|
|
|
this->CheckDevice(deviceId);
|
2019-01-02 14:34:50 +00:00
|
|
|
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info,
|
|
|
|
"Setting device '" << deviceId.GetName() << "' to " << state);
|
2019-03-15 19:54:42 +00:00
|
|
|
this->Internals->RuntimeAllowed[deviceId.GetValue()] = state;
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:32:10 +00:00
|
|
|
|
|
|
|
VTKM_CONT void RuntimeDeviceTracker::ResetDevice(vtkm::cont::DeviceAdapterId deviceId)
|
|
|
|
{
|
|
|
|
if (deviceId == vtkm::cont::DeviceAdapterTagAny{})
|
|
|
|
{
|
|
|
|
this->Reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vtkm::cont::RuntimeDeviceInformation runtimeDevice;
|
|
|
|
this->SetDeviceState(deviceId, runtimeDevice.Exists(deviceId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-29 18:45:10 +00:00
|
|
|
VTKM_CONT
|
|
|
|
void RuntimeDeviceTracker::Reset()
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2019-03-29 18:45:10 +00:00
|
|
|
std::fill_n(this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, false);
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2019-03-29 18:45:10 +00:00
|
|
|
// We use this instead of calling CheckDevice/SetDeviceState so that
|
|
|
|
// when we use logging we get better messages stating we are reseting
|
|
|
|
// the devices.
|
|
|
|
vtkm::cont::RuntimeDeviceInformation runtimeDevice;
|
2019-05-21 19:32:10 +00:00
|
|
|
for (vtkm::Int8 i = 1; i < VTKM_MAX_DEVICE_ADAPTER_ID; ++i)
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2019-03-29 18:45:10 +00:00
|
|
|
vtkm::cont::DeviceAdapterId device = vtkm::cont::make_DeviceAdapterId(i);
|
2019-03-15 19:54:42 +00:00
|
|
|
if (device.IsValueValid())
|
|
|
|
{
|
2019-03-29 18:45:10 +00:00
|
|
|
const bool state = runtimeDevice.Exists(device);
|
|
|
|
this->Internals->RuntimeAllowed[device.GetValue()] = state;
|
2019-03-15 19:54:42 +00:00
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info,
|
|
|
|
"Reset device '" << device.GetName() << "' to " << state);
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:32:10 +00:00
|
|
|
VTKM_CONT void RuntimeDeviceTracker::DisableDevice(vtkm::cont::DeviceAdapterId deviceId)
|
2017-02-23 16:19:13 +00:00
|
|
|
{
|
2019-05-21 19:32:10 +00:00
|
|
|
if (deviceId == vtkm::cont::DeviceAdapterTagAny{})
|
2017-02-23 16:19:13 +00:00
|
|
|
{
|
2019-05-21 19:32:10 +00:00
|
|
|
std::fill_n(this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->SetDeviceState(deviceId, false);
|
2017-02-23 16:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:17:29 +00:00
|
|
|
VTKM_CONT
|
2019-03-15 19:54:42 +00:00
|
|
|
void RuntimeDeviceTracker::ForceDevice(DeviceAdapterId deviceId)
|
2018-10-17 19:17:29 +00:00
|
|
|
{
|
2019-05-20 19:04:09 +00:00
|
|
|
if (deviceId == vtkm::cont::DeviceAdapterTagAny{})
|
|
|
|
{
|
|
|
|
this->Reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-21 19:32:10 +00:00
|
|
|
this->CheckDevice(deviceId);
|
2019-05-20 19:04:09 +00:00
|
|
|
vtkm::cont::RuntimeDeviceInformation runtimeDevice;
|
2019-05-21 19:32:10 +00:00
|
|
|
const bool runtimeExists = runtimeDevice.Exists(deviceId);
|
|
|
|
if (!runtimeExists)
|
|
|
|
{
|
|
|
|
std::stringstream message;
|
|
|
|
message << "Cannot force to device '" << deviceId.GetName()
|
|
|
|
<< "' because that device is not available on this system";
|
|
|
|
throw vtkm::cont::ErrorBadValue(message.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info,
|
|
|
|
"Forcing execution to occur on device '" << deviceId.GetName() << "'");
|
|
|
|
|
|
|
|
std::fill_n(this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, false);
|
|
|
|
|
|
|
|
this->Internals->RuntimeAllowed[deviceId.GetValue()] = runtimeExists;
|
2019-05-20 19:04:09 +00:00
|
|
|
}
|
2018-12-06 18:33:31 +00:00
|
|
|
}
|
2019-01-03 20:03:35 +00:00
|
|
|
|
2019-05-15 19:48:15 +00:00
|
|
|
VTKM_CONT
|
2019-05-21 19:32:10 +00:00
|
|
|
ScopedRuntimeDeviceTracker::ScopedRuntimeDeviceTracker(vtkm::cont::DeviceAdapterId device,
|
|
|
|
RuntimeDeviceTrackerMode mode)
|
2019-05-15 21:26:34 +00:00
|
|
|
: RuntimeDeviceTracker(GetRuntimeDeviceTracker().Internals, false)
|
2019-05-15 19:48:15 +00:00
|
|
|
, SavedState(new detail::RuntimeDeviceTrackerInternals())
|
|
|
|
{
|
|
|
|
std::copy_n(
|
|
|
|
this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, this->SavedState->RuntimeAllowed);
|
2019-05-21 19:32:10 +00:00
|
|
|
|
|
|
|
if (mode == RuntimeDeviceTrackerMode::Force)
|
|
|
|
{
|
|
|
|
this->ForceDevice(device);
|
|
|
|
}
|
|
|
|
else if (mode == RuntimeDeviceTrackerMode::Enable)
|
|
|
|
{
|
|
|
|
this->ResetDevice(device);
|
|
|
|
}
|
|
|
|
else if (mode == RuntimeDeviceTrackerMode::Disable)
|
|
|
|
{
|
|
|
|
this->DisableDevice(device);
|
|
|
|
}
|
2019-05-20 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
ScopedRuntimeDeviceTracker::ScopedRuntimeDeviceTracker(
|
|
|
|
vtkm::cont::DeviceAdapterId device,
|
2019-05-21 19:32:10 +00:00
|
|
|
RuntimeDeviceTrackerMode mode,
|
2019-05-20 19:04:09 +00:00
|
|
|
const vtkm::cont::RuntimeDeviceTracker& tracker)
|
|
|
|
: RuntimeDeviceTracker(tracker.Internals, false)
|
|
|
|
, SavedState(new detail::RuntimeDeviceTrackerInternals())
|
|
|
|
{
|
|
|
|
std::copy_n(
|
|
|
|
this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, this->SavedState->RuntimeAllowed);
|
2019-05-21 19:32:10 +00:00
|
|
|
if (mode == RuntimeDeviceTrackerMode::Force)
|
|
|
|
{
|
|
|
|
this->ForceDevice(device);
|
|
|
|
}
|
|
|
|
else if (mode == RuntimeDeviceTrackerMode::Enable)
|
|
|
|
{
|
|
|
|
this->ResetDevice(device);
|
|
|
|
}
|
|
|
|
else if (mode == RuntimeDeviceTrackerMode::Disable)
|
|
|
|
{
|
|
|
|
this->DisableDevice(device);
|
|
|
|
}
|
2019-05-15 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
ScopedRuntimeDeviceTracker::ScopedRuntimeDeviceTracker(
|
|
|
|
const vtkm::cont::RuntimeDeviceTracker& tracker)
|
2019-05-15 21:26:34 +00:00
|
|
|
: RuntimeDeviceTracker(tracker.Internals, false)
|
2019-05-15 19:48:15 +00:00
|
|
|
, SavedState(new detail::RuntimeDeviceTrackerInternals())
|
|
|
|
{
|
|
|
|
std::copy_n(
|
|
|
|
this->Internals->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, this->SavedState->RuntimeAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
ScopedRuntimeDeviceTracker::~ScopedRuntimeDeviceTracker()
|
|
|
|
{
|
|
|
|
std::copy_n(
|
|
|
|
this->SavedState->RuntimeAllowed, VTKM_MAX_DEVICE_ADAPTER_ID, this->Internals->RuntimeAllowed);
|
|
|
|
}
|
|
|
|
|
2019-01-03 20:03:35 +00:00
|
|
|
VTKM_CONT
|
2019-03-15 19:54:42 +00:00
|
|
|
vtkm::cont::RuntimeDeviceTracker& GetRuntimeDeviceTracker()
|
2019-01-03 20:03:35 +00:00
|
|
|
{
|
2019-03-15 19:54:42 +00:00
|
|
|
#if defined(VTKM_CLANG) && defined(__apple_build_version__) && (__apple_build_version__ < 8000000)
|
2019-01-03 20:03:35 +00:00
|
|
|
static std::mutex mtx;
|
2019-03-15 19:54:42 +00:00
|
|
|
static std::map<std::thread::id, vtkm::cont::RuntimeDeviceTracker*> globalTrackers;
|
2019-05-15 21:26:34 +00:00
|
|
|
static std::map<std::thread::id, vtkm::cont::detail::RuntimeDeviceTrackerInternals*>
|
|
|
|
globalTrackerInternals;
|
2019-01-03 20:03:35 +00:00
|
|
|
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())
|
|
|
|
{
|
2019-03-15 19:54:42 +00:00
|
|
|
return *iter->second;
|
2019-01-03 20:03:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-15 21:26:34 +00:00
|
|
|
auto* details = new vtkm::cont::detail::RuntimeDeviceTrackerInternals();
|
|
|
|
vtkm::cont::RuntimeDeviceTracker* tracker = new vtkm::cont::RuntimeDeviceTracker(details, true);
|
2019-01-03 20:03:35 +00:00
|
|
|
globalTrackers[this_id] = tracker;
|
2019-05-15 21:26:34 +00:00
|
|
|
globalTrackerInternals[this_id] = details;
|
2019-03-15 19:54:42 +00:00
|
|
|
return *tracker;
|
2019-01-03 20:03:35 +00:00
|
|
|
}
|
|
|
|
#else
|
2019-05-15 21:26:34 +00:00
|
|
|
static thread_local vtkm::cont::detail::RuntimeDeviceTrackerInternals details;
|
|
|
|
static thread_local vtkm::cont::RuntimeDeviceTracker runtimeDeviceTracker(&details, true);
|
2019-01-03 20:03:35 +00:00
|
|
|
return runtimeDeviceTracker;
|
|
|
|
#endif
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
} // namespace vtkm::cont
|