Caffe2 - C++ API
A deep learning, cross platform ML framework
DeviceGuard.h
1 #pragma once
2 
3 #include <c10/core/impl/InlineDeviceGuard.h>
4 
5 namespace c10 {
6 
19 class DeviceGuard {
20 public:
22  explicit DeviceGuard() = delete;
23 
25  explicit DeviceGuard(Device device) : guard_(device) {}
26 
28  explicit DeviceGuard(Device device, const impl::DeviceGuardImplInterface* impl) : guard_(device, impl) {}
29 
31  DeviceGuard(const DeviceGuard&) = delete;
32  DeviceGuard& operator=(const DeviceGuard&) = delete;
33 
36  DeviceGuard(DeviceGuard&& other) = delete;
37  DeviceGuard& operator=(DeviceGuard&& other) = delete;
38 
47  guard_.reset_device(device);
48  }
49 
52  guard_.reset_device(device, impl);
53  }
54 
57  void set_index(DeviceIndex index) {
58  guard_.set_index(index);
59  }
60 
63  return guard_.original_device();
64  }
65 
69  return guard_.current_device();
70  }
71 
72 private:
74 };
75 
120 public:
122  explicit OptionalDeviceGuard() : guard_() {}
123 
125  explicit OptionalDeviceGuard(Device device) : guard_(device) {}
126 
129  explicit OptionalDeviceGuard(optional<Device> device) : guard_(device) {}
130 
132  explicit OptionalDeviceGuard(Device device, const impl::DeviceGuardImplInterface* impl) : guard_(device, impl) {}
133 
135  OptionalDeviceGuard(const OptionalDeviceGuard&) = delete;
136  OptionalDeviceGuard& operator=(const OptionalDeviceGuard&) = delete;
137 
142  OptionalDeviceGuard(OptionalDeviceGuard&& other) = delete;
143  OptionalDeviceGuard& operator=(OptionalDeviceGuard&& other) = delete;
144 
148  guard_.reset_device(device);
149  }
150 
153  guard_.reset_device(device, impl);
154  }
155 
158  return guard_.original_device();
159  }
160 
164  return guard_.current_device();
165  }
166 
167 private:
169 };
170 
171 // Note [Whither the DeviceGuard boilerplate]
172 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
173 // Design note: in principle, we could avoid these wrappers using:
174 //
175 // using DeviceGuard = impl::InlineDeviceGuard<impl::VirtualGuardImpl>;
176 // using OptionalDeviceGuard = impl::InlineOptionalDeviceGuard<impl::VirtualGuardImpl>;
177 //
178 // But the error messages are worse, and our users can't just look at the
179 // header file to find out what's going on. Furthermore, for specializations
180 // like CUDAStreamGuard, it can be profitable to replace some interfaces with
181 // refined types (e.g., return CUDAStream instead of Stream). So, we eat
182 // the boilerplate and write out the API explicitly.
183 
184 } // namespace c10
DeviceGuard(Device device)
Set the current device to the passed Device.
Definition: DeviceGuard.h:25
void set_index(DeviceIndex index)
Sets the device index to the given one.
optional< Device > original_device() const
Returns the device that was set at the time the guard was constructed.
Definition: DeviceGuard.h:157
void set_index(DeviceIndex index)
Sets the device index to the given one.
Definition: DeviceGuard.h:57
Device original_device() const
Returns the device that was set at the time the most recent reset_device(), or otherwise the device a...
OptionalDeviceGuard(optional< Device > device)
Initialize the guard if a Device is passed; otherwise leave the guard uninitialized.
Definition: DeviceGuard.h:129
DeviceGuard()=delete
No default constructor; see Note [Omitted default constructor from RAII].
OptionalDeviceGuard(Device device)
Initialize the guard, setting the current device to the passed Device.
Definition: DeviceGuard.h:125
TensorOptions device(Device device)
Convenience function that returns a TensorOptions object with the device set to the given one...
void reset_device(at::Device device)
Sets the device to the given one.
Definition: DeviceGuard.h:46
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
int16_t DeviceIndex
An index representing a specific device; e.g., the 1 in GPU 1.
Definition: Device.h:18
Device original_device() const
Returns the device that was set at the time the guard was constructed.
Definition: DeviceGuard.h:62
A OptionalDeviceGuard is an RAII class that sets a device to some value on initialization, and resets the device to its original value on destruction.
Definition: DeviceGuard.h:119
optional< Device > current_device() const
Returns the most recent device that was set using this device guard, either from construction, or via set_device.
Definition: DeviceGuard.h:163
void reset_device(at::Device device, const impl::DeviceGuardImplInterface *impl)
For testing only.
Definition: DeviceGuard.h:152
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
RAII guard that sets a certain default device in its constructor, and changes it back to the device t...
Definition: DeviceGuard.h:19
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.
OptionalDeviceGuard()
Create an uninitialized guard. Set the guard later using set_device.
Definition: DeviceGuard.h:122
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 passe...
void reset_device(at::Device device, const impl::DeviceGuardImplInterface *impl)
This method is for testing only.
Definition: DeviceGuard.h:51
DeviceGuard(Device device, const impl::DeviceGuardImplInterface *impl)
This constructor is for testing only.
Definition: DeviceGuard.h:28
OptionalDeviceGuard(Device device, const impl::DeviceGuardImplInterface *impl)
Constructor for testing only.
Definition: DeviceGuard.h:132
Device current_device() const
Returns the most recent device that was set using this device guard, either from construction, or via set_device.
Definition: DeviceGuard.h:68
DeviceGuardImplInterface represents the virtual interface which provides functionality to provide an ...
void reset_device(at::Device device)
Sets the device to the given one.
Definition: DeviceGuard.h:147