CUDAGuard.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #pragma once
  2. #include <c10/core/DeviceType.h>
  3. #include <c10/core/impl/InlineDeviceGuard.h>
  4. #include <c10/core/impl/InlineStreamGuard.h>
  5. #include <c10/cuda/CUDAMacros.h>
  6. #include <c10/cuda/impl/CUDAGuardImpl.h>
  7. namespace c10::cuda {
  8. // This code is kind of boilerplatey. See Note [Whither the DeviceGuard
  9. // boilerplate]
  10. /// A variant of DeviceGuard that is specialized for CUDA. It accepts
  11. /// integer indices (interpreting them as CUDA devices) and is a little
  12. /// more efficient than DeviceGuard (it compiles to straight line
  13. /// cudaSetDevice/cudaGetDevice calls); however, it can only be used
  14. /// from code that links against CUDA directly.
  15. struct CUDAGuard {
  16. /// No default constructor; see Note [Omitted default constructor from RAII]
  17. explicit CUDAGuard() = delete;
  18. /// Set the current CUDA device to the passed device index.
  19. explicit CUDAGuard(DeviceIndex device_index) : guard_(device_index) {}
  20. /// Sets the current CUDA device to the passed device. Errors if the passed
  21. /// device is not a CUDA device.
  22. explicit CUDAGuard(Device device) : guard_(device) {}
  23. // Copy is not allowed
  24. CUDAGuard(const CUDAGuard&) = delete;
  25. CUDAGuard& operator=(const CUDAGuard&) = delete;
  26. // Move is not allowed (there is no uninitialized state)
  27. CUDAGuard(CUDAGuard&& other) = delete;
  28. CUDAGuard& operator=(CUDAGuard&& other) = delete;
  29. ~CUDAGuard() = default;
  30. /// Sets the CUDA device to the given device. Errors if the given device
  31. /// is not a CUDA device.
  32. void set_device(Device device) {
  33. guard_.set_device(device);
  34. }
  35. /// Sets the CUDA device to the given device. Errors if the given device
  36. /// is not a CUDA device. (This method is provided for uniformity with
  37. /// DeviceGuard).
  38. void reset_device(Device device) {
  39. guard_.reset_device(device);
  40. }
  41. /// Sets the CUDA device to the given device index.
  42. void set_index(DeviceIndex device_index) {
  43. guard_.set_index(device_index);
  44. }
  45. /// Returns the device that was set upon construction of the guard
  46. Device original_device() const {
  47. return guard_.original_device();
  48. }
  49. /// Returns the last device that was set via `set_device`, if any, otherwise
  50. /// the device passed during construction.
  51. Device current_device() const {
  52. return guard_.current_device();
  53. }
  54. private:
  55. /// The guard for the current device.
  56. c10::impl::InlineDeviceGuard<impl::CUDAGuardImpl> guard_;
  57. };
  58. /// A variant of OptionalDeviceGuard that is specialized for CUDA. See
  59. /// CUDAGuard for when you can use this.
  60. struct OptionalCUDAGuard {
  61. /// Create an uninitialized OptionalCUDAGuard.
  62. explicit OptionalCUDAGuard() = default;
  63. /// Set the current CUDA device to the passed Device, if it is not nullopt.
  64. explicit OptionalCUDAGuard(std::optional<Device> device_opt)
  65. : guard_(device_opt) {}
  66. /// Set the current CUDA device to the passed device index, if it is not
  67. /// nullopt
  68. explicit OptionalCUDAGuard(std::optional<DeviceIndex> device_index_opt)
  69. : guard_(device_index_opt) {}
  70. // Copy is not allowed
  71. OptionalCUDAGuard(const OptionalCUDAGuard&) = delete;
  72. OptionalCUDAGuard& operator=(const OptionalCUDAGuard&) = delete;
  73. // See Note [Move construction for RAII guards is tricky]
  74. OptionalCUDAGuard(OptionalCUDAGuard&& other) = delete;
  75. // See Note [Move assignment for RAII guards is tricky]
  76. OptionalCUDAGuard& operator=(OptionalCUDAGuard&& other) = delete;
  77. ~OptionalCUDAGuard() = default;
  78. /// Sets the CUDA device to the given device, initializing the guard if it
  79. /// is not already initialized. Errors if the given device is not a CUDA
  80. /// device.
  81. void set_device(Device device) {
  82. guard_.set_device(device);
  83. }
  84. /// Sets the CUDA device to the given device, initializing the guard if it is
  85. /// not already initialized. Errors if the given device is not a CUDA device.
  86. /// (This method is provided for uniformity with OptionalDeviceGuard).
  87. void reset_device(Device device) {
  88. guard_.reset_device(device);
  89. }
  90. /// Sets the CUDA device to the given device index, initializing the guard if
  91. /// it is not already initialized.
  92. void set_index(DeviceIndex device_index) {
  93. guard_.set_index(device_index);
  94. }
  95. /// Returns the device that was set immediately prior to initialization of the
  96. /// guard, or nullopt if the guard is uninitialized.
  97. std::optional<Device> original_device() const {
  98. return guard_.original_device();
  99. }
  100. /// Returns the most recent device that was set using this device guard,
  101. /// either from construction, or via set_device, if the guard is initialized,
  102. /// or nullopt if the guard is uninitialized.
  103. std::optional<Device> current_device() const {
  104. return guard_.current_device();
  105. }
  106. /// Restore the original CUDA device, resetting this guard to uninitialized
  107. /// state.
  108. void reset() {
  109. guard_.reset();
  110. }
  111. private:
  112. c10::impl::InlineOptionalDeviceGuard<impl::CUDAGuardImpl> guard_;
  113. };
  114. /// A variant of StreamGuard that is specialized for CUDA. See CUDAGuard
  115. /// for when you can use this.
  116. struct CUDAStreamGuard {
  117. /// No default constructor, see Note [Omitted default constructor from RAII]
  118. explicit CUDAStreamGuard() = delete;
  119. /// Set the current CUDA device to the device associated with the passed
  120. /// stream, and set the current CUDA stream on that device to the passed
  121. /// stream. Errors if the Stream is not a CUDA stream.
  122. explicit CUDAStreamGuard(Stream stream) : guard_(stream) {}
  123. ~CUDAStreamGuard() = default;
  124. /// Copy is disallowed
  125. CUDAStreamGuard(const CUDAStreamGuard&) = delete;
  126. CUDAStreamGuard& operator=(const CUDAStreamGuard&) = delete;
  127. /// Move is disallowed, as CUDAStreamGuard does not have an uninitialized
  128. /// state, which is required for moves on types with nontrivial destructors.
  129. CUDAStreamGuard(CUDAStreamGuard&& other) = delete;
  130. CUDAStreamGuard& operator=(CUDAStreamGuard&& other) = delete;
  131. /// Resets the currently set stream to the original stream and
  132. /// the currently set device to the original device. Then,
  133. /// set the current device to the device associated with the passed stream,
  134. /// and set the current stream on that device to the passed stream.
  135. /// Errors if the stream passed is not a CUDA stream.
  136. ///
  137. /// NOTE: this implementation may skip some stream/device setting if
  138. /// it can prove that it is unnecessary.
  139. ///
  140. /// WARNING: reset_stream does NOT preserve previously set streams on
  141. /// different devices. If you need to set streams on multiple devices
  142. /// on CUDA, use CUDAMultiStreamGuard instead.
  143. void reset_stream(Stream stream) {
  144. guard_.reset_stream(stream);
  145. }
  146. /// Returns the CUDA stream that was set at the time the guard was
  147. /// constructed.
  148. CUDAStream original_stream() const {
  149. return CUDAStream(CUDAStream::UNCHECKED, guard_.original_stream());
  150. }
  151. /// Returns the most recent CUDA stream that was set using this device guard,
  152. /// either from construction, or via set_stream.
  153. CUDAStream current_stream() const {
  154. return CUDAStream(CUDAStream::UNCHECKED, guard_.current_stream());
  155. }
  156. /// Returns the most recent CUDA device that was set using this device guard,
  157. /// either from construction, or via set_device/reset_device/set_index.
  158. Device current_device() const {
  159. return guard_.current_device();
  160. }
  161. /// Returns the CUDA device that was set at the most recent reset_stream(),
  162. /// or otherwise the device at construction time.
  163. Device original_device() const {
  164. return guard_.original_device();
  165. }
  166. private:
  167. c10::impl::InlineStreamGuard<impl::CUDAGuardImpl> guard_;
  168. };
  169. /// A variant of OptionalStreamGuard that is specialized for CUDA. See
  170. /// CUDAGuard for when you can use this.
  171. struct OptionalCUDAStreamGuard {
  172. /// Create an uninitialized guard.
  173. explicit OptionalCUDAStreamGuard() = default;
  174. /// Set the current CUDA device to the device associated with the passed
  175. /// stream, and set the current CUDA stream on that device to the passed
  176. /// stream. Errors if the Stream is not a CUDA stream.
  177. explicit OptionalCUDAStreamGuard(Stream stream) : guard_(stream) {}
  178. /// Set the current device to the device associated with the passed stream,
  179. /// and set the current stream on that device to the passed stream,
  180. /// if the passed stream is not nullopt.
  181. explicit OptionalCUDAStreamGuard(std::optional<Stream> stream_opt)
  182. : guard_(stream_opt) {}
  183. /// Copy is disallowed
  184. OptionalCUDAStreamGuard(const OptionalCUDAStreamGuard&) = delete;
  185. OptionalCUDAStreamGuard& operator=(const OptionalCUDAStreamGuard&) = delete;
  186. // See Note [Move construction for RAII guards is tricky]
  187. OptionalCUDAStreamGuard(OptionalCUDAStreamGuard&& other) = delete;
  188. // See Note [Move assignment for RAII guards is tricky]
  189. OptionalCUDAStreamGuard& operator=(OptionalCUDAStreamGuard&& other) = delete;
  190. ~OptionalCUDAStreamGuard() = default;
  191. /// Resets the currently set CUDA stream to the original stream and
  192. /// the currently set device to the original device. Then,
  193. /// set the current device to the device associated with the passed stream,
  194. /// and set the current stream on that device to the passed stream.
  195. /// Initializes the guard if it was not previously initialized.
  196. void reset_stream(Stream stream) {
  197. guard_.reset_stream(stream);
  198. }
  199. /// Returns the CUDA stream that was set at the time the guard was most
  200. /// recently initialized, or nullopt if the guard is uninitialized.
  201. std::optional<CUDAStream> original_stream() const {
  202. auto r = guard_.original_stream();
  203. if (r.has_value()) {
  204. return CUDAStream(CUDAStream::UNCHECKED, r.value());
  205. } else {
  206. return std::nullopt;
  207. }
  208. }
  209. /// Returns the most recent CUDA stream that was set using this stream guard,
  210. /// either from construction, or via reset_stream, if the guard is
  211. /// initialized, or nullopt if the guard is uninitialized.
  212. std::optional<CUDAStream> current_stream() const {
  213. auto r = guard_.current_stream();
  214. if (r.has_value()) {
  215. return CUDAStream(CUDAStream::UNCHECKED, r.value());
  216. } else {
  217. return std::nullopt;
  218. }
  219. }
  220. /// Restore the original CUDA device and stream, resetting this guard to
  221. /// uninitialized state.
  222. void reset() {
  223. guard_.reset();
  224. }
  225. private:
  226. c10::impl::InlineOptionalStreamGuard<impl::CUDAGuardImpl> guard_;
  227. };
  228. /// A variant of MultiStreamGuard that is specialized for CUDA.
  229. struct CUDAMultiStreamGuard {
  230. explicit CUDAMultiStreamGuard(ArrayRef<CUDAStream> streams)
  231. : guard_(unwrapStreams(streams)) {}
  232. /// Copy is disallowed
  233. CUDAMultiStreamGuard(const CUDAMultiStreamGuard&) = delete;
  234. CUDAMultiStreamGuard& operator=(const CUDAMultiStreamGuard&) = delete;
  235. // See Note [Move construction for RAII guards is tricky]
  236. CUDAMultiStreamGuard(CUDAMultiStreamGuard&& other) = delete;
  237. // See Note [Move assignment for RAII guards is tricky]
  238. CUDAMultiStreamGuard& operator=(CUDAMultiStreamGuard&& other) = delete;
  239. ~CUDAMultiStreamGuard() = default;
  240. private:
  241. c10::impl::InlineMultiStreamGuard<impl::CUDAGuardImpl> guard_;
  242. static std::vector<Stream> unwrapStreams(ArrayRef<CUDAStream> cudaStreams) {
  243. std::vector<Stream> streams;
  244. streams.reserve(cudaStreams.size());
  245. for (const CUDAStream& cudaStream : cudaStreams) {
  246. streams.push_back(cudaStream);
  247. }
  248. return streams;
  249. }
  250. };
  251. } // namespace c10::cuda