PyTorch
Loading...
Searching...
No Matches
CUDAGuard.h
Go to the documentation of this file.
1#pragma once
2
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>
8
9#include <cstddef>
10
11namespace c10 {
12namespace cuda {
13
14// This code is kind of boilerplatey. See Note [Whither the DeviceGuard
15// boilerplate]
16
22struct CUDAGuard {
24 explicit CUDAGuard() = delete;
25
27 explicit CUDAGuard(DeviceIndex device_index) : guard_(device_index) {}
28
31 explicit CUDAGuard(Device device) : guard_(device) {}
32
33 // Copy is not allowed
34 CUDAGuard(const CUDAGuard&) = delete;
35 CUDAGuard& operator=(const CUDAGuard&) = delete;
36
37 // Move is not allowed (there is no uninitialized state)
38 CUDAGuard(CUDAGuard&& other) = delete;
39 CUDAGuard& operator=(CUDAGuard&& other) = delete;
40
43 void set_device(Device device) {
44 guard_.set_device(device);
45 }
46
50 void reset_device(Device device) {
51 guard_.reset_device(device);
52 }
53
55 void set_index(DeviceIndex device_index) {
56 guard_.set_index(device_index);
57 }
58
61 return guard_.original_device();
62 }
63
67 return guard_.current_device();
68 }
69
70 private:
72 c10::impl::InlineDeviceGuard<impl::CUDAGuardImpl> guard_;
73};
74
79 explicit OptionalCUDAGuard() : guard_() {}
80
83 : guard_(device_opt) {}
84
87 explicit OptionalCUDAGuard(optional<DeviceIndex> device_index_opt)
88 : guard_(device_index_opt) {}
89
90 // Copy is not allowed
93
94 // See Note [Move construction for RAII guards is tricky]
96
97 // See Note [Move assignment for RAII guards is tricky]
99
103 void set_device(Device device) {
104 guard_.set_device(device);
105 }
106
110 void reset_device(Device device) {
111 guard_.reset_device(device);
112 }
113
116 void set_index(DeviceIndex device_index) {
117 guard_.set_index(device_index);
118 }
119
123 return guard_.original_device();
124 }
125
130 return guard_.current_device();
131 }
132
135 void reset() {
136 guard_.reset();
137 }
138
139 private:
140 c10::impl::InlineOptionalDeviceGuard<impl::CUDAGuardImpl> guard_;
141};
142
147 explicit CUDAStreamGuard() = delete;
148
152 explicit CUDAStreamGuard(Stream stream) : guard_(stream) {}
153
157
162
175 void reset_stream(Stream stream) {
176 guard_.reset_stream(stream);
177 }
178
182 return CUDAStream(CUDAStream::UNCHECKED, guard_.original_stream());
183 }
184
188 return CUDAStream(CUDAStream::UNCHECKED, guard_.current_stream());
189 }
190
194 return guard_.current_device();
195 }
196
200 return guard_.original_device();
201 }
202
203 private:
204 c10::impl::InlineStreamGuard<impl::CUDAGuardImpl> guard_;
205};
206
211 explicit OptionalCUDAStreamGuard() : guard_() {}
212
216 explicit OptionalCUDAStreamGuard(Stream stream) : guard_(stream) {}
217
222 : guard_(stream_opt) {}
223
227
228 // See Note [Move construction for RAII guards is tricky]
230
231 // See Note [Move assignment for RAII guards is tricky]
233
239 void reset_stream(Stream stream) {
240 guard_.reset_stream(stream);
241 }
242
246 auto r = guard_.original_stream();
247 if (r.has_value()) {
249 } else {
250 return nullopt;
251 }
252 }
253
258 auto r = guard_.current_stream();
259 if (r.has_value()) {
261 } else {
262 return nullopt;
263 }
264 }
265
268 void reset() {
269 guard_.reset();
270 }
271
272 private:
273 c10::impl::InlineOptionalStreamGuard<impl::CUDAGuardImpl> guard_;
274};
275
279 : guard_(unwrapStreams(streams)) {}
280
284
285 // See Note [Move construction for RAII guards is tricky]
287
288 // See Note [Move assignment for RAII guards is tricky]
290
291 private:
292 c10::impl::InlineMultiStreamGuard<impl::CUDAGuardImpl> guard_;
293
294 static std::vector<Stream> unwrapStreams(ArrayRef<CUDAStream> cudaStreams) {
295 std::vector<Stream> streams;
296 streams.reserve(cudaStreams.size());
297 for (const CUDAStream& cudaStream : cudaStreams) {
298 streams.push_back(cudaStream);
299 }
300 return streams;
301 }
302};
303
304} // namespace cuda
305} // namespace c10
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
Definition: ivalue.h:27
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