CUDAGuard.h 11 KB

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