4#include <c10/core/impl/InlineDeviceGuard.h>
5#include <c10/core/impl/InlineStreamGuard.h>
6#include <c10/cuda/CUDAMacros.h>
7#include <c10/cuda/impl/CUDAGuardImpl.h>
44 guard_.set_device(device);
51 guard_.reset_device(device);
56 guard_.set_index(device_index);
61 return guard_.original_device();
67 return guard_.current_device();
72 c10::impl::InlineDeviceGuard<impl::CUDAGuardImpl> guard_;
83 : guard_(device_opt) {}
88 : guard_(device_index_opt) {}
104 guard_.set_device(device);
111 guard_.reset_device(device);
117 guard_.set_index(device_index);
123 return guard_.original_device();
130 return guard_.current_device();
140 c10::impl::InlineOptionalDeviceGuard<impl::CUDAGuardImpl> guard_;
176 guard_.reset_stream(stream);
194 return guard_.current_device();
200 return guard_.original_device();
204 c10::impl::InlineStreamGuard<impl::CUDAGuardImpl> guard_;
222 : guard_(stream_opt) {}
240 guard_.reset_stream(stream);
246 auto r = guard_.original_stream();
258 auto r = guard_.current_stream();
273 c10::impl::InlineOptionalStreamGuard<impl::CUDAGuardImpl> guard_;
279 : guard_(unwrapStreams(streams)) {}
292 c10::impl::InlineMultiStreamGuard<impl::CUDAGuardImpl> guard_;
295 std::vector<Stream> streams;
296 streams.reserve(cudaStreams.
size());
297 for (
const CUDAStream& cudaStream : cudaStreams) {
298 streams.push_back(cudaStream);
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:40
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:169
Definition: CUDAStream.h:62
@ UNCHECKED
Definition: CUDAStream.h:64
Definition: Optional.h:549
constexpr nullopt_t nullopt
Definition: Optional.h:163
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
Definition: Optional.h:1223
int8_t DeviceIndex
An index representing a specific device; e.g., the 1 in GPU 1.
Definition: Device.h:18
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
A variant of DeviceGuard that is specialized for CUDA.
Definition: CUDAGuard.h:22
void set_device(Device device)
Sets the CUDA device to the given device.
Definition: CUDAGuard.h:43
Device original_device() const
Returns the device that was set upon construction of the guard.
Definition: CUDAGuard.h:60
CUDAGuard & operator=(CUDAGuard &&other)=delete
CUDAGuard()=delete
No default constructor; see Note [Omitted default constructor from RAII].
CUDAGuard(Device device)
Sets the current CUDA device to the passed device.
Definition: CUDAGuard.h:31
void reset_device(Device device)
Sets the CUDA device to the given device.
Definition: CUDAGuard.h:50
CUDAGuard(DeviceIndex device_index)
Set the current CUDA device to the passed device index.
Definition: CUDAGuard.h:27
CUDAGuard(const CUDAGuard &)=delete
Device current_device() const
Returns the last device that was set via set_device, if any, otherwise the device passed during const...
Definition: CUDAGuard.h:66
void set_index(DeviceIndex device_index)
Sets the CUDA device to the given device index.
Definition: CUDAGuard.h:55
CUDAGuard(CUDAGuard &&other)=delete
CUDAGuard & operator=(const CUDAGuard &)=delete
A variant of MultiStreamGuard that is specialized for CUDA.
Definition: CUDAGuard.h:277
CUDAMultiStreamGuard & operator=(const CUDAMultiStreamGuard &)=delete
CUDAMultiStreamGuard(CUDAMultiStreamGuard &&other)=delete
CUDAMultiStreamGuard & operator=(CUDAMultiStreamGuard &&other)=delete
CUDAMultiStreamGuard(ArrayRef< CUDAStream > streams)
Definition: CUDAGuard.h:278
CUDAMultiStreamGuard(const CUDAMultiStreamGuard &)=delete
Copy is disallowed.
A variant of StreamGuard that is specialized for CUDA.
Definition: CUDAGuard.h:145
Device original_device() const
Returns the CUDA device that was set at the most recent reset_stream(), or otherwise the device at co...
Definition: CUDAGuard.h:199
CUDAStreamGuard()=delete
No default constructor, see Note [Omitted default constructor from RAII].
void reset_stream(Stream stream)
Resets the currently set stream to the original stream and the currently set device to the original d...
Definition: CUDAGuard.h:175
CUDAStream original_stream() const
Returns the CUDA stream that was set at the time the guard was constructed.
Definition: CUDAGuard.h:181
CUDAStreamGuard(const CUDAStreamGuard &)=delete
Copy is disallowed.
CUDAStreamGuard & operator=(CUDAStreamGuard &&other)=delete
CUDAStreamGuard & operator=(const CUDAStreamGuard &)=delete
CUDAStream current_stream() const
Returns the most recent CUDA stream that was set using this device guard, either from construction,...
Definition: CUDAGuard.h:187
Device current_device() const
Returns the most recent CUDA device that was set using this device guard, either from construction,...
Definition: CUDAGuard.h:193
CUDAStreamGuard(Stream stream)
Set the current CUDA device to the device associated with the passed stream, and set the current CUDA...
Definition: CUDAGuard.h:152
CUDAStreamGuard(CUDAStreamGuard &&other)=delete
Move is disallowed, as CUDAStreamGuard does not have an uninitialized state, which is required for mo...
A variant of OptionalDeviceGuard that is specialized for CUDA.
Definition: CUDAGuard.h:77
void reset()
Restore the original CUDA device, resetting this guard to uninitialized state.
Definition: CUDAGuard.h:135
OptionalCUDAGuard(OptionalCUDAGuard &&other)=delete
OptionalCUDAGuard()
Create an uninitialized OptionalCUDAGuard.
Definition: CUDAGuard.h:79
void set_device(Device device)
Sets the CUDA device to the given device, initializing the guard if it is not already initialized.
Definition: CUDAGuard.h:103
OptionalCUDAGuard(const OptionalCUDAGuard &)=delete
void set_index(DeviceIndex device_index)
Sets the CUDA device to the given device index, initializing the guard if it is not already initializ...
Definition: CUDAGuard.h:116
optional< Device > current_device() const
Returns the most recent device that was set using this device guard, either from construction,...
Definition: CUDAGuard.h:129
OptionalCUDAGuard(optional< DeviceIndex > device_index_opt)
Set the current CUDA device to the passed device index, if it is not nullopt.
Definition: CUDAGuard.h:87
OptionalCUDAGuard(optional< Device > device_opt)
Set the current CUDA device to the passed Device, if it is not nullopt.
Definition: CUDAGuard.h:82
optional< Device > original_device() const
Returns the device that was set immediately prior to initialization of the guard, or nullopt if the g...
Definition: CUDAGuard.h:122
OptionalCUDAGuard & operator=(const OptionalCUDAGuard &)=delete
OptionalCUDAGuard & operator=(OptionalCUDAGuard &&other)=delete
void reset_device(Device device)
Sets the CUDA device to the given device, initializing the guard if it is not already initialized.
Definition: CUDAGuard.h:110
A variant of OptionalStreamGuard that is specialized for CUDA.
Definition: CUDAGuard.h:209
OptionalCUDAStreamGuard & operator=(OptionalCUDAStreamGuard &&other)=delete
OptionalCUDAStreamGuard(optional< Stream > stream_opt)
Set the current device to the device associated with the passed stream, and set the current stream on...
Definition: CUDAGuard.h:221
OptionalCUDAStreamGuard(const OptionalCUDAStreamGuard &)=delete
Copy is disallowed.
optional< CUDAStream > current_stream() const
Returns the most recent CUDA stream that was set using this stream guard, either from construction,...
Definition: CUDAGuard.h:257
OptionalCUDAStreamGuard(Stream stream)
Set the current CUDA device to the device associated with the passed stream, and set the current CUDA...
Definition: CUDAGuard.h:216
OptionalCUDAStreamGuard()
Create an uninitialized guard.
Definition: CUDAGuard.h:211
void reset_stream(Stream stream)
Resets the currently set CUDA stream to the original stream and the currently set device to the origi...
Definition: CUDAGuard.h:239
OptionalCUDAStreamGuard(OptionalCUDAStreamGuard &&other)=delete
void reset()
Restore the original CUDA device and stream, resetting this guard to uninitialized state.
Definition: CUDAGuard.h:268
optional< CUDAStream > original_stream() const
Returns the CUDA stream that was set at the time the guard was most recently initialized,...
Definition: CUDAGuard.h:245
OptionalCUDAStreamGuard & operator=(const OptionalCUDAStreamGuard &)=delete