Caffe2 - C++ API
A deep learning, cross platform ML framework
Stream.h
1 #pragma once
2 
3 #include <c10/core/Device.h>
4 
5 namespace c10 {
6 
15 using StreamId = int32_t;
16 
17 // NB: I decided not to call the above StreamIndex to avoid confusion with
18 // DeviceIndex. This way, you access device index with index(), and stream id
19 // with id()
20 
57 class Stream final {
58 private:
59  Device device_;
60  StreamId id_;
61 public:
62  enum Unsafe { UNSAFE };
63  enum Default { DEFAULT };
64 
71  explicit Stream(Unsafe, Device device, StreamId id)
72  : device_(device)
73  , id_(id) {}
74 
79  explicit Stream(Default, Device device)
80  : device_(device)
81  , id_(0) {}
82 
83  bool operator==(const Stream& other) const noexcept {
84  return this->device_ == other.device_ && this->id_ == other.id_;
85  }
86  bool operator!=(const Stream& other) const noexcept {
87  return !(*this == other);
88  }
89 
90  Device device() const noexcept { return device_; }
91  DeviceType device_type() const noexcept { return device_.type(); }
92  DeviceIndex device_index() const noexcept { return device_.index(); }
93  StreamId id() const noexcept { return id_; }
94 
95  // The purpose of this function is to more conveniently permit binding
96  // of Stream to and from Python. Without packing, I have to setup a whole
97  // class with two fields (device and stream id); with packing I can just
98  // store a single uint64_t.
99  //
100  // The particular way we pack streams into a uint64_t is considered an
101  // implementation detail and should not be relied upon.
102  uint64_t pack() const noexcept {
103  // Are you here because this static assert failed? Make sure you ensure
104  // that the bitmasking code below is updated accordingly!
105  static_assert(sizeof(DeviceType) == 2, "DeviceType is not 16-bit");
106  static_assert(sizeof(DeviceIndex) == 2, "DeviceIndex is not 16-bit");
107  static_assert(sizeof(StreamId) == 4, "DeviceIndex is not 32-bit");
108  // Concat these together into a 64-bit integer
109  // See Note [Hazard when concatenating signed integers]
110  uint64_t bits =
111  static_cast<uint64_t>(static_cast<uint16_t>(device_type())) << 48
112  | static_cast<uint64_t>(static_cast<uint16_t>(device_index())) << 32
113  | static_cast<uint64_t>(static_cast<uint32_t>(id()));
114  return bits;
115  }
116 
117  static Stream unpack(uint64_t bits) {
118  auto stream_id = static_cast<StreamId>(bits) & 0xFFFFFFFFull;
119  bits >>= 32;
120  auto device_index = static_cast<DeviceIndex>(bits) & 0xFFFFull;
121  bits >>= 16;
122  auto device_type = static_cast<DeviceType>(bits);
123  AT_CHECK(isValidDeviceType(device_type));
124  // Unfortunately, we can't check if the StreamId is valid here; it
125  // will be checked upon first use.
126  return Stream(UNSAFE, Device(device_type, device_index), stream_id);
127  }
128 
129  // I decided NOT to provide setters on this class, because really,
130  // why would you change the device of a stream? Just construct
131  // it correctly from the beginning dude.
132 };
133 
134 C10_API std::ostream& operator<<(std::ostream& stream, const Stream& s);
135 
136 } // namespace c10
137 
138 namespace std {
139  template <>
140  struct hash<c10::Stream> {
141  size_t operator()(c10::Stream s) const noexcept {
142  return std::hash<uint64_t>{}(s.pack());
143  }
144  };
145 } // namespace std
146 
147 namespace at {
148  using c10::StreamId;
149  using c10::Stream;
150 }
A stream is a software mechanism used to synchronize launched kernels without requiring explicit sync...
Definition: Stream.h:57
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
int16_t DeviceIndex
An index representing a specific device; e.g., the 1 in GPU 1.
Definition: Device.h:18
Stream(Unsafe, Device device, StreamId id)
Unsafely construct a stream from a Device and a StreamId.
Definition: Stream.h:71
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
int32_t StreamId
An index representing a specific stream.
Definition: Stream.h:15
Flush-To-Zero and Denormals-Are-Zero mode.
DeviceIndex index() const noexcept
Returns the optional index.
Definition: Device.h:70
DeviceType type() const noexcept
Returns the type of device this is.
Definition: Device.h:65
Stream(Default, Device device)
Construct the default stream of a Device.
Definition: Stream.h:79