Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Member Functions
c10::DeviceGuard Class Reference

RAII guard that sets a certain default device in its constructor, and changes it back to the device that was originally active upon destruction. More...

#include <DeviceGuard.h>

Public Member Functions

 DeviceGuard ()=delete
 No default constructor; see Note [Omitted default constructor from RAII].
 
 DeviceGuard (Device device)
 Set the current device to the passed Device.
 
 DeviceGuard (Device device, const impl::DeviceGuardImplInterface *impl)
 This constructor is for testing only.
 
 DeviceGuard (const DeviceGuard &)=delete
 Copy is disallowed.
 
DeviceGuardoperator= (const DeviceGuard &)=delete
 
 DeviceGuard (DeviceGuard &&other)=delete
 Move is disallowed, as DeviceGuard does not have an uninitialized state, which is required for moves on types with nontrivial destructors. More...
 
DeviceGuardoperator= (DeviceGuard &&other)=delete
 
void reset_device (at::Device device)
 Sets the device to the given one. More...
 
void reset_device (at::Device device, const impl::DeviceGuardImplInterface *impl)
 This method is for testing only.
 
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 guard was constructed.
 
Device current_device () const
 Returns the most recent device that was set using this device guard, either from construction, or via set_device. More...
 

Detailed Description

RAII guard that sets a certain default device in its constructor, and changes it back to the device that was originally active upon destruction.

The device is always reset to the one that was active at the time of construction of the guard. Even if you set_device after construction, the destructor will still reset the device to the one that was active at construction time.

This device guard does NOT have an uninitialized state; it is guaranteed to reset a device on exit. If you are in a situation where you might want to setup a guard (i.e., are looking for the moral equivalent of optional<DeviceGuard>), see OptionalDeviceGuard.

Definition at line 19 of file DeviceGuard.h.

Constructor & Destructor Documentation

c10::DeviceGuard::DeviceGuard ( DeviceGuard &&  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

Device c10::DeviceGuard::current_device ( ) const
inline

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

Definition at line 68 of file DeviceGuard.h.

void c10::DeviceGuard::reset_device ( at::Device  device)
inline

Sets the device to the given one.

The specified device must be consistent with the device type originally specified during guard construction.

TODO: The consistency check here is inconsistent with StreamGuard's behavior with set_stream, where a stream on a different device than the original one isn't an error; we just reset the stream and then switch devices.

Definition at line 46 of file DeviceGuard.h.

void c10::DeviceGuard::set_index ( DeviceIndex  index)
inline

Sets the device index to the given one.

The device type is inferred from the original device type the guard was constructed with.

Definition at line 57 of file DeviceGuard.h.


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