Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Member Functions | Protected Attributes
c10::impl::InlineDeviceGuard< T > Class Template Reference

A DeviceGuard is an RAII class that sets a device to some value on construction, and resets the device to its original value on destruction. More...

#include <InlineDeviceGuard.h>

Inheritance diagram for c10::impl::InlineDeviceGuard< T >:
c10::impl::InlineStreamGuard< T >

Public Member Functions

 InlineDeviceGuard (Device device)
 Set the current device to the passed Device.
 
template<typename U = T, typename = typename std::enable_if<!std::is_same<U, VirtualGuardImpl>::value>::type>
 InlineDeviceGuard (DeviceIndex device_index)
 Set the current device index to the passed DeviceIndex. More...
 
template<typename U = T, typename = typename std::enable_if<std::is_same<U, VirtualGuardImpl>::value>::type>
 InlineDeviceGuard (Device device, const DeviceGuardImplInterface *impl)
 Construct an InlineDeviceGuard using VirtualGuardImpl with an explicit DeviceGuardImplInterface pointer. More...
 
 InlineDeviceGuard (const InlineDeviceGuard< T > &)=delete
 Copy is disallowed.
 
InlineDeviceGuard< T > & operator= (const InlineDeviceGuard< T > &)=delete
 
 InlineDeviceGuard (InlineDeviceGuard< T > &&other)=delete
 Move is disallowed, as DeviceGuard does not have an uninitialized state, which is required for moves on types with nontrivial destructors. More...
 
InlineDeviceGuardoperator= (InlineDeviceGuard< T > &&other)=delete
 
template<typename U = T, typename std::enable_if<!std::is_same< U, VirtualGuardImpl >::value, int >::type = 0>
void set_device (at::Device device)
 Sets the device to the given one.
 
template<typename U = T>
std::enable_if<!std::is_same< U, VirtualGuardImpl >::value >::type reset_device (at::Device device)
 Resets the currently set device to its original device, and then sets the current device to the passed device. More...
 
template<typename U = T>
std::enable_if< std::is_same< U, VirtualGuardImpl >::value >::type reset_device (at::Device device, const impl::DeviceGuardImplInterface *impl=nullptr)
 Resets the currently set device to its original device, and then sets the current device to the passed device (for a possibly different device type). More...
 
void set_index (DeviceIndex index)
 Sets the device index to the given one. More...
 
Device original_device () const
 Returns the device that was set at the time the most recent reset_device(), or otherwise the device at construction time. More...
 
Device current_device () const
 Returns the most recent device that was set using this device guard, either from construction, or via set_device/reset_device/set_index. More...
 

Protected Attributes

T impl_
 

Detailed Description

template<typename T>
class c10::impl::InlineDeviceGuard< T >

A DeviceGuard is an RAII class that sets a device to some value on construction, and resets the device to its original value on destruction.

InlineDeviceGuard is a helper class for implementing DeviceGuards. It is templated over a DeviceGuardImpl (anything that implements DeviceGuardImplInterface). There are two primary ways to instantiate InlineDeviceGuard:

If you are in a hurry, you can use InlineDeviceGuard directly:

using CUDAGuard = impl::InlineDeviceGuard<CUDAGuardImpl>;

However, you can provide a better user experience if you explicitly write a wrapper class that itself contains the template instantiation:

class CUDAGuard { public: // ... the API ... private: impl::InlineDeviceGuard<CUDAGuardImpl> guard_; }

The wrapper class provides a good place to write documentation, and helps avoid weird template instantiation errors when a user incorrectly uses the class.

If you need to test this class, consider instantiating it with FakeGuardImpl.

Definition at line 56 of file InlineDeviceGuard.h.

Constructor & Destructor Documentation

template<typename T>
template<typename U = T, typename = typename std::enable_if<!std::is_same<U, VirtualGuardImpl>::value>::type>
c10::impl::InlineDeviceGuard< T >::InlineDeviceGuard ( DeviceIndex  device_index)
inlineexplicit

Set the current device index to the passed DeviceIndex.

(The device type is inferred from the template parameter T).

Definition at line 80 of file InlineDeviceGuard.h.

template<typename T>
template<typename U = T, typename = typename std::enable_if<std::is_same<U, VirtualGuardImpl>::value>::type>
c10::impl::InlineDeviceGuard< T >::InlineDeviceGuard ( Device  device,
const DeviceGuardImplInterface impl 
)
inlineexplicit

Construct an InlineDeviceGuard using VirtualGuardImpl with an explicit DeviceGuardImplInterface pointer.

Definition at line 86 of file InlineDeviceGuard.h.

template<typename T>
c10::impl::InlineDeviceGuard< T >::InlineDeviceGuard ( InlineDeviceGuard< T > &&  other)
delete

Move is disallowed, as DeviceGuard does not have an uninitialized state, which is required for moves on types with nontrivial destructors.

Member Function Documentation

template<typename T>
Device c10::impl::InlineDeviceGuard< T >::current_device ( ) const
inline

Returns the most recent device that was set using this device guard, either from construction, or via set_device/reset_device/set_index.

Definition at line 174 of file InlineDeviceGuard.h.

template<typename T>
Device c10::impl::InlineDeviceGuard< T >::original_device ( ) const
inline

Returns the device that was set at the time the most recent reset_device(), or otherwise the device at construction time.

Definition at line 168 of file InlineDeviceGuard.h.

template<typename T>
template<typename U = T>
std::enable_if<!std::is_same<U, VirtualGuardImpl>::value >::type c10::impl::InlineDeviceGuard< T >::reset_device ( at::Device  device)
inline

Resets the currently set device to its original device, and then sets the current device to the passed device.

This is effectively equivalent to set_device when a guard supports only a single device type.

Definition at line 121 of file InlineDeviceGuard.h.

template<typename T>
template<typename U = T>
std::enable_if<std::is_same<U, VirtualGuardImpl>::value >::type c10::impl::InlineDeviceGuard< T >::reset_device ( at::Device  device,
const impl::DeviceGuardImplInterface impl = nullptr 
)
inline

Resets the currently set device to its original device, and then sets the current device to the passed device (for a possibly different device type).

This method is named reset_device to highlight the fact that previous device settings from this guard are NOT preserved, even if the device has a different device type. For example:

// CUDA device is 0 DeviceGuard g(Device(kCUDA, 1)); g.reset_device(Device(kHIP, 2)); // CUDA device is 0 (!!)

NOTE: this implementation may skip some device setting if it can prove that it is unnecessary.

Optional argument is for testing only.

Definition at line 144 of file InlineDeviceGuard.h.

template<typename T>
void c10::impl::InlineDeviceGuard< T >::set_index ( DeviceIndex  index)
inline

Sets the device index to the given one.

The device type is inferred from the original device type.

Definition at line 162 of file InlineDeviceGuard.h.


The documentation for this class was generated from the following file: