6#include <cuda_runtime_api.h>
8#include <c10/core/DeviceGuard.h>
9#include <c10/core/Stream.h>
10#include <c10/cuda/CUDAFunctions.h>
69 TORCH_CHECK(stream_.device_type() == DeviceType::CUDA);
78 return unwrap() == other.unwrap();
82 return unwrap() != other.unwrap();
86 operator cudaStream_t()
const {
92 operator Stream()
const {
98 return DeviceType::CUDA;
103 return stream_.device_index();
109 return Device(DeviceType::CUDA, device_index());
113 StreamId
id()
const {
118 DeviceGuard guard{stream_.device()};
119 cudaError_t err = C10_CUDA_ERROR_HANDLED(cudaStreamQuery(stream()));
121 if (err == cudaSuccess) {
123 }
else if (err != cudaErrorNotReady) {
127 (void)cudaGetLastError();
134 DeviceGuard guard{stream_.device()};
135 c10::cuda::stream_synchronize(stream());
139 DeviceGuard guard{stream_.device()};
141 C10_CUDA_CHECK(cudaStreamGetPriority(stream(), &priority));
162 struct c10::StreamData3 pack3() const {
163 return stream_.pack3();
171 return CUDAStream(Stream::unpack3(stream_id, device_index, device_type));
179 int least_priority, greatest_priority;
181 cudaDeviceGetStreamPriorityRange(&least_priority, &greatest_priority));
183 least_priority >= 0,
"Unexpected CUDA stream priority range");
185 greatest_priority <= -1,
"Unexpected CUDA stream priority range");
186 return std::make_tuple(0, -1);
255struct hash<
c10::cuda::CUDAStream> {
257 return std::hash<c10::Stream>{}(s.unwrap());
#define TORCH_INTERNAL_ASSERT(cond,...)
Definition: Exception.h:377
#define TORCH_CHECK(cond,...)
Definition: Exception.h:505
Definition: CUDAStream.h:62
Stream unwrap() const
Explicit conversion to Stream.
Definition: CUDAStream.h:149
cudaStream_t stream() const
Explicit conversion to cudaStream_t.
DeviceType device_type() const
Used to avoid baking in device type explicitly to Python-side API.
Definition: CUDAStream.h:97
bool query() const
Definition: CUDAStream.h:117
StreamId id() const
Return the stream ID corresponding to this particular stream.
Definition: CUDAStream.h:113
int priority() const
Definition: CUDAStream.h:138
CUDAStream(Unchecked, Stream stream)
Construct a CUDAStream from a Stream with no error checking.
Definition: CUDAStream.h:75
Device device() const
Get the full Device that this stream is associated with.
Definition: CUDAStream.h:108
Unchecked
Definition: CUDAStream.h:64
static std::tuple< int, int > priority_range()
Definition: CUDAStream.h:174
static CUDAStream unpack3(StreamId stream_id, DeviceIndex device_index, DeviceType device_type)
Definition: CUDAStream.h:167
void synchronize() const
Definition: CUDAStream.h:133
DeviceIndex device_index() const
Get the CUDA device index that this stream is associated with.
Definition: CUDAStream.h:102
CUDAStream(Stream stream)
Construct a CUDAStream from a Stream.
Definition: CUDAStream.h:68
bool operator!=(const CUDAStream &other) const noexcept
Definition: CUDAStream.h:81
bool operator==(const CUDAStream &other) const noexcept
Definition: CUDAStream.h:77
CUDAStream getDefaultCUDAStream(DeviceIndex device_index=-1)
Get the default CUDA stream, for the passed CUDA device, or for the current device if no device index...
CUDAStream getStreamFromExternal(cudaStream_t ext_stream, DeviceIndex device_index)
Get a CUDAStream from a externally allocated one.
CUDAStream getStreamFromPool(const bool isHighPriority=false, DeviceIndex device=-1)
Get a new stream from the CUDA stream pool.
void setCurrentCUDAStream(CUDAStream stream)
Set the current stream on the device of the passed in stream to be the passed in stream.
std::ostream & operator<<(std::ostream &stream, const CUDAStream &s)
CUDAStream getCurrentCUDAStream(DeviceIndex device_index=-1)
Get the current CUDA stream, for the passed CUDA device, or for the current device if no device index...
int8_t DeviceIndex
An index representing a specific device; e.g., the 1 in GPU 1.
Definition: Device.h:18
DeviceType
Definition: DeviceType.h:33
Represents a a compute device on which a tensor is located.
Definition: Device.h:30