device.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ttg/config.h"
4 #include "ttg/execution.h"
5 #include "ttg/impl_selector.h"
6 #include "ttg/fwd.h"
7 #include "ttg/util/meta.h"
8 
9 
10 
11 namespace ttg::device {
12 
14  class Device {
15  int m_id = 0;
17 
18  public:
19  Device() = default;
21  : m_id(id)
22  , m_space(space)
23  { }
24 
25  int id() const {
26  if (is_host()) {
27  throw std::runtime_error("No valid ID for Host execution space!");
28  }
29  if (is_invalid()) {
30  throw std::runtime_error("Invalid execution space!");
31  }
32  return m_id;
33  }
34 
35  operator int() const {
36  return id();
37  }
38 
40  return m_space;
41  }
42 
43  bool is_device() const {
44  return !is_host();
45  }
46 
47  bool is_host() const {
48  return !is_invalid() && (m_space == ttg::ExecutionSpace::Host);
49  }
50 
51  bool is_invalid() const {
52  return (m_space == ttg::ExecutionSpace::Invalid);
53  }
54 
55  static Device host() {
56  return {};
57  }
58  };
59 } // namespace ttg::device
60 
61 namespace std {
62  inline
63  std::ostream& operator<<(std::ostream& os, ttg::device::Device device) {
65  if (device.is_device()) {
66  os << "(" << device.id() << ")";
67  }
68  return os;
69  }
70 } // namespace std
71 
72 namespace ttg::device {
73 
74  namespace detail {
75  template<typename Stream>
76  struct default_stream {
77  static constexpr const Stream value = 0;
78  };
79  template<typename Stream>
81  } // namespace detail
82 
83 } // namespace ttg
84 
85 #if defined(TTG_HAVE_CUDA)
86 #include <cuda_runtime.h>
87 namespace ttg::device {
89  using Stream = cudaStream_t;
90 } // namespace ttg::device
91 #elif defined(TTG_HAVE_HIP)
92 #include <hip/hip_runtime.h>
93 namespace ttg::device {
95  using Stream = hipStream_t;
96 } // namespace ttg::device
97 #elif defined(TTG_HAVE_LEVEL_ZERO)
98 #include <CL/sycl.hpp>
99 namespace ttg::device {
101  using Stream = std::add_reference_t<sycl::queue>;
102 } // namespace ttg::device
103 #else
104 namespace ttg::device {
105  struct Stream { };
106  namespace detail {
107  template<>
109  static constexpr const Stream value = {};
110  };
111  } // namespace detail
113 } // namespace ttg::device
114 #endif
115 
116 namespace ttg::device {
117 
118 #if !defined(TTG_HAVE_LEVEL_ZERO)
119  namespace detail {
120  inline thread_local ttg::device::Device current_device_ts = {};
121  inline thread_local Stream current_stream_ts = detail::default_stream_v<Stream>; // default stream
122 
123  inline void reset_current() {
124  current_device_ts = {};
125  current_stream_ts = detail::default_stream_v<Stream>;
126  }
127 
128  inline void set_current(int device, Stream stream) {
130  current_stream_ts = stream;
131  }
132  } // namespace detail
133 
134  inline
137  }
138 
139  inline
142  }
143 
144  inline int num_devices() {
145  return TTG_IMPL_NS::num_devices();
146  }
147 
148 #else // TTG_HAVE_LEVEL_ZERO
149  /* SYCL needs special treatment because it uses pointers/references */
150  namespace detail {
151  inline thread_local ttg::device::Device current_device_ts = {};
152  inline thread_local sycl::queue* current_stream_ts = nullptr; // default stream
153 
154 
155  inline void reset_current() {
156  current_device_ts = {};
157  current_stream_ts = nullptr;
158  }
159 
160  inline void set_current(int device, sycl::queue& stream) {
162  current_stream_ts = &stream;
163  }
164  } // namespace detail
165 
166  inline
167  Device current_device() {
169  }
170 
171  inline
172  sycl::queue& current_stream() {
174  }
175 #endif // TTG_HAVE_LEVEL_ZERO
176 
177 } // namespace ttg
Represents a device in a specific execution space.
Definition: device.h:14
bool is_host() const
Definition: device.h:47
bool is_device() const
Definition: device.h:43
ttg::ExecutionSpace space() const
Definition: device.h:39
bool is_invalid() const
Definition: device.h:51
Device(int id, ttg::ExecutionSpace space)
Definition: device.h:20
int id() const
Definition: device.h:25
static Device host()
Definition: device.h:55
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition: device.h:63
const char * execution_space_name(ExecutionSpace space) noexcept
Definition: execution.h:26
thread_local ttg::device::Device current_device_ts
Definition: device.h:120
thread_local Stream current_stream_ts
Definition: device.h:121
void set_current(int device, Stream stream)
Definition: device.h:128
void reset_current()
Definition: device.h:123
constexpr const Stream default_stream_v
Definition: device.h:80
Stream current_stream()
Definition: device.h:140
Device current_device()
Definition: device.h:135
constexpr ttg::ExecutionSpace available_execution_space
Definition: device.h:112
int num_devices()
Definition: device.h:144
int num_devices()
Definition: device.h:41
ExecutionSpace
denotes task execution space
Definition: execution.h:17
static constexpr const Stream value
Definition: device.h:77