2016-01-19 14:59:31 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2016-01-19 14:59:31 +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.
|
|
|
|
//============================================================================
|
2017-02-22 20:33:52 +00:00
|
|
|
#ifndef vtk_m_cont_RuntimeDeviceTracker_h
|
|
|
|
#define vtk_m_cont_RuntimeDeviceTracker_h
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2018-05-14 20:21:30 +00:00
|
|
|
#include <vtkm/cont/vtkm_cont_export.h>
|
|
|
|
|
2019-04-03 20:28:31 +00:00
|
|
|
#include <vtkm/cont/DeviceAdapterTag.h>
|
2017-01-09 21:15:32 +00:00
|
|
|
#include <vtkm/cont/ErrorBadAllocation.h>
|
2017-12-22 15:01:09 +00:00
|
|
|
#include <vtkm/cont/ErrorBadDevice.h>
|
2016-01-19 14:59:31 +00:00
|
|
|
#include <vtkm/cont/RuntimeDeviceInformation.h>
|
|
|
|
|
2018-05-14 20:21:30 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
struct RuntimeDeviceTrackerInternals;
|
|
|
|
}
|
2019-05-15 19:48:15 +00:00
|
|
|
struct ScopedRuntimeDeviceTracker;
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2019-05-15 19:48:15 +00:00
|
|
|
/// RuntimeDeviceTracker is the central location for determining
|
|
|
|
/// which device adapter will be active for algorithm execution.
|
|
|
|
/// Many features in VTK-m will attempt to run algorithms on the "best
|
|
|
|
/// available device." This generally is determined at runtime as some
|
|
|
|
/// backends require specific hardware, or failures in one device are
|
|
|
|
/// recorded and that device is disabled.
|
|
|
|
///
|
|
|
|
/// While vtkm::cont::RunimeDeviceInformation reports on the existence
|
|
|
|
/// of a device being supported, this tracks on a per-thread basis
|
|
|
|
/// when worklets fail, why the fail, and will update the list
|
2016-01-19 14:59:31 +00:00
|
|
|
/// of valid runtime devices based on that information.
|
|
|
|
///
|
|
|
|
///
|
2019-03-15 19:54:42 +00:00
|
|
|
class VTKM_CONT_EXPORT RuntimeDeviceTracker
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2019-03-15 19:54:42 +00:00
|
|
|
friend VTKM_CONT_EXPORT vtkm::cont::RuntimeDeviceTracker& GetRuntimeDeviceTracker();
|
2018-10-17 19:17:29 +00:00
|
|
|
|
2016-01-19 14:59:31 +00:00
|
|
|
public:
|
2017-02-22 21:51:57 +00:00
|
|
|
VTKM_CONT
|
|
|
|
~RuntimeDeviceTracker();
|
2016-01-19 14:59:31 +00:00
|
|
|
|
|
|
|
/// Returns true if the given device adapter is supported on the current
|
|
|
|
/// machine.
|
|
|
|
///
|
2019-05-21 19:32:10 +00:00
|
|
|
VTKM_CONT bool CanRunOn(DeviceAdapterId deviceId) const;
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-11-07 19:17:28 +00:00
|
|
|
/// Report a failure to allocate memory on a device, this will flag the
|
2019-05-21 19:32:10 +00:00
|
|
|
/// device as being unusable for all future invocations.
|
2017-11-07 19:17:28 +00:00
|
|
|
///
|
2018-07-25 14:33:55 +00:00
|
|
|
VTKM_CONT void ReportAllocationFailure(vtkm::cont::DeviceAdapterId deviceId,
|
2017-11-07 19:17:28 +00:00
|
|
|
const vtkm::cont::ErrorBadAllocation&)
|
|
|
|
{
|
2018-08-29 20:47:51 +00:00
|
|
|
this->SetDeviceState(deviceId, false);
|
2017-11-07 19:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:01:09 +00:00
|
|
|
|
2019-03-15 19:54:42 +00:00
|
|
|
/// Report a ErrorBadDevice failure and flag the device as unusable.
|
2018-07-25 14:33:55 +00:00
|
|
|
VTKM_CONT void ReportBadDeviceFailure(vtkm::cont::DeviceAdapterId deviceId,
|
2017-12-22 15:01:09 +00:00
|
|
|
const vtkm::cont::ErrorBadDevice&)
|
|
|
|
{
|
2018-08-29 20:47:51 +00:00
|
|
|
this->SetDeviceState(deviceId, false);
|
2017-12-22 15:01:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
/// Reset the tracker for the given device. This will discard any updates
|
2019-05-21 19:32:10 +00:00
|
|
|
/// caused by reported failures. Passing DeviceAdapterTagAny to this will
|
|
|
|
/// reset all devices ( same as \c Reset ).
|
2016-01-19 14:59:31 +00:00
|
|
|
///
|
2019-05-21 19:32:10 +00:00
|
|
|
VTKM_CONT void ResetDevice(vtkm::cont::DeviceAdapterId deviceId);
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
/// Reset the tracker to its default state for default devices.
|
|
|
|
/// Will discard any updates caused by reported failures.
|
|
|
|
///
|
|
|
|
VTKM_CONT
|
|
|
|
void Reset();
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-02-23 16:19:13 +00:00
|
|
|
/// \brief Disable the given device
|
|
|
|
///
|
|
|
|
/// The main intention of \c RuntimeDeviceTracker is to keep track of what
|
|
|
|
/// devices are working for VTK-m. However, it can also be used to turn
|
|
|
|
/// devices on and off. Use this method to disable (turn off) a given device.
|
|
|
|
/// Use \c ResetDevice to turn the device back on (if it is supported).
|
|
|
|
///
|
2019-05-21 19:32:10 +00:00
|
|
|
/// Passing DeviceAdapterTagAny to this will disable all devices
|
|
|
|
///
|
|
|
|
VTKM_CONT void DisableDevice(DeviceAdapterId deviceId);
|
2017-02-23 16:19:13 +00:00
|
|
|
|
|
|
|
/// \brief Disable all devices except the specified one.
|
|
|
|
///
|
|
|
|
/// The main intention of \c RuntimeDeviceTracker is to keep track of what
|
|
|
|
/// devices are working for VTK-m. However, it can also be used to turn
|
|
|
|
/// devices on and off. Use this method to disable all devices except one
|
2019-05-20 19:04:09 +00:00
|
|
|
/// to effectively force VTK-m to use that device. Either pass the
|
|
|
|
/// DeviceAdapterTagAny to this function or call \c Reset to restore
|
|
|
|
/// all devices to their default state.
|
2017-02-23 16:19:13 +00:00
|
|
|
///
|
|
|
|
/// This method will throw a \c ErrorBadValue if the given device does not
|
|
|
|
/// exist on the system.
|
|
|
|
///
|
2019-05-21 19:32:10 +00:00
|
|
|
VTKM_CONT void ForceDevice(DeviceAdapterId deviceId);
|
2018-10-17 19:17:29 +00:00
|
|
|
|
2019-08-26 18:55:26 +00:00
|
|
|
VTKM_CONT void PrintSummary(std::ostream& out) const;
|
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
private:
|
2019-05-15 19:48:15 +00:00
|
|
|
friend struct ScopedRuntimeDeviceTracker;
|
|
|
|
|
2019-05-15 21:26:34 +00:00
|
|
|
detail::RuntimeDeviceTrackerInternals* Internals;
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2019-03-15 19:54:42 +00:00
|
|
|
VTKM_CONT
|
2019-05-15 21:26:34 +00:00
|
|
|
RuntimeDeviceTracker(detail::RuntimeDeviceTrackerInternals* details, bool reset);
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
RuntimeDeviceTracker(const RuntimeDeviceTracker&) = delete;
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
RuntimeDeviceTracker& operator=(const RuntimeDeviceTracker&) = delete;
|
2019-03-15 19:54:42 +00:00
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void CheckDevice(vtkm::cont::DeviceAdapterId deviceId) const;
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void SetDeviceState(vtkm::cont::DeviceAdapterId deviceId, bool state);
|
2019-09-30 20:07:07 +00:00
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
void LogEnabledDevices() const;
|
2019-05-21 19:32:10 +00:00
|
|
|
};
|
2017-02-23 16:19:13 +00:00
|
|
|
|
2019-05-21 19:32:10 +00:00
|
|
|
|
|
|
|
enum struct RuntimeDeviceTrackerMode
|
|
|
|
{
|
|
|
|
Force,
|
|
|
|
Enable,
|
|
|
|
Disable
|
2016-01-19 14:59:31 +00:00
|
|
|
};
|
|
|
|
|
2019-05-15 19:48:15 +00:00
|
|
|
/// A class that can be used to determine or modify which device adapter
|
|
|
|
/// VTK-m algorithms should be run on. This class captures the state
|
|
|
|
/// of the per-thread device adapter and will revert any changes applied
|
|
|
|
/// during its lifetime on destruction.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
struct VTKM_CONT_EXPORT ScopedRuntimeDeviceTracker : public vtkm::cont::RuntimeDeviceTracker
|
|
|
|
{
|
2019-05-21 19:32:10 +00:00
|
|
|
/// Construct a ScopedRuntimeDeviceTracker where the state of the active devices
|
|
|
|
/// for the current thread are determined by the parameters to the constructor.
|
|
|
|
///
|
|
|
|
/// 'Force'
|
|
|
|
/// - Force-Enable the provided single device adapter
|
|
|
|
/// - Force-Enable all device adapters when using vtkm::cont::DeviceAdaterTagAny
|
|
|
|
/// 'Enable'
|
|
|
|
/// - Enable the provided single device adapter if it was previously disabled
|
|
|
|
/// - Enable all device adapters that are currently disabled when using
|
|
|
|
/// vtkm::cont::DeviceAdaterTagAny
|
|
|
|
/// 'Disable'
|
|
|
|
/// - Disable the provided single device adapter
|
|
|
|
/// - Disable all device adapters when using vtkm::cont::DeviceAdaterTagAny
|
2019-05-20 19:04:09 +00:00
|
|
|
///
|
2019-05-15 19:48:15 +00:00
|
|
|
/// Constructor is not thread safe
|
2019-05-21 19:32:10 +00:00
|
|
|
VTKM_CONT ScopedRuntimeDeviceTracker(
|
|
|
|
vtkm::cont::DeviceAdapterId device,
|
|
|
|
RuntimeDeviceTrackerMode mode = RuntimeDeviceTrackerMode::Force);
|
2019-05-15 19:48:15 +00:00
|
|
|
|
2019-05-20 19:04:09 +00:00
|
|
|
/// Construct a ScopedRuntimeDeviceTracker associated with the thread
|
2019-05-21 19:32:10 +00:00
|
|
|
/// associated with the provided tracker. The active devices
|
|
|
|
/// for the current thread are determined by the parameters to the constructor.
|
|
|
|
///
|
|
|
|
/// 'Force'
|
|
|
|
/// - Force-Enable the provided single device adapter
|
|
|
|
/// - Force-Enable all device adapters when using vtkm::cont::DeviceAdaterTagAny
|
|
|
|
/// 'Enable'
|
|
|
|
/// - Enable the provided single device adapter if it was previously disabled
|
|
|
|
/// - Enable all device adapters that are currently disabled when using
|
|
|
|
/// vtkm::cont::DeviceAdaterTagAny
|
|
|
|
/// 'Disable'
|
|
|
|
/// - Disable the provided single device adapter
|
|
|
|
/// - Disable all device adapters when using vtkm::cont::DeviceAdaterTagAny
|
2019-05-20 19:04:09 +00:00
|
|
|
///
|
|
|
|
/// Any modifications to the ScopedRuntimeDeviceTracker will effect what
|
|
|
|
/// ever thread the \c tracker is associated with, which might not be
|
|
|
|
/// the thread which ScopedRuntimeDeviceTracker was constructed on.
|
|
|
|
///
|
|
|
|
/// Constructor is not thread safe
|
|
|
|
VTKM_CONT 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);
|
|
|
|
|
|
|
|
/// Construct a ScopedRuntimeDeviceTracker associated with the thread
|
|
|
|
/// associated with the provided tracker.
|
|
|
|
///
|
|
|
|
/// Any modifications to the ScopedRuntimeDeviceTracker will effect what
|
|
|
|
/// ever thread the \c tracker is associated with, which might not be
|
|
|
|
/// the thread which ScopedRuntimeDeviceTracker was constructed on.
|
|
|
|
///
|
2019-05-15 19:48:15 +00:00
|
|
|
/// Constructor is not thread safe
|
|
|
|
VTKM_CONT ScopedRuntimeDeviceTracker(const vtkm::cont::RuntimeDeviceTracker& tracker);
|
|
|
|
|
|
|
|
/// Destructor is not thread safe
|
|
|
|
VTKM_CONT ~ScopedRuntimeDeviceTracker();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<detail::RuntimeDeviceTrackerInternals> SavedState;
|
|
|
|
};
|
|
|
|
|
2017-11-15 00:42:48 +00:00
|
|
|
/// \brief Get the \c RuntimeDeviceTracker for the current thread.
|
2017-02-23 16:19:13 +00:00
|
|
|
///
|
|
|
|
/// Many features in VTK-m will attempt to run algorithms on the "best
|
|
|
|
/// available device." This often is determined at runtime as failures in
|
|
|
|
/// one device are recorded and that device is disabled. To prevent having
|
2017-11-15 00:42:48 +00:00
|
|
|
/// to check over and over again, VTK-m uses per thread runtime device tracker
|
|
|
|
/// so that these choices are marked and shared.
|
2017-02-23 16:19:13 +00:00
|
|
|
///
|
|
|
|
VTKM_CONT_EXPORT
|
|
|
|
VTKM_CONT
|
2019-03-15 19:54:42 +00:00
|
|
|
vtkm::cont::RuntimeDeviceTracker& GetRuntimeDeviceTracker();
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
} // namespace vtkm::cont
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-02-22 20:33:52 +00:00
|
|
|
#endif //vtk_m_filter_RuntimeDeviceTracker_h
|