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 } // namespace ttg::device
56 
57 namespace std {
58  inline
59  std::ostream& operator<<(std::ostream& os, ttg::device::Device device) {
61  if (device.is_device()) {
62  os << "(" << device.id() << ")";
63  }
64  return os;
65  }
66 } // namespace std
67 
68 namespace ttg::device {
69 
70  namespace detail {
71  template<typename Stream>
72  struct default_stream {
73  static constexpr const Stream value = 0;
74  };
75  template<typename Stream>
77  } // namespace detail
78 
79 } // namespace ttg
80 
81 #if defined(TTG_HAVE_CUDA)
82 #include <cuda_runtime.h>
83 namespace ttg::device {
85  using Stream = cudaStream_t;
86 } // namespace ttg::device
87 #elif defined(TTG_HAVE_HIP)
88 #include <hip/hip_runtime.h>
89 namespace ttg::device {
91  using Stream = hipStream_t;
92 } // namespace ttg::device
93 #elif defined(TTG_HAVE_LEVEL_ZERO)
94 #include <CL/sycl.hpp>
95 namespace ttg::device {
97  using Stream = std::add_reference_t<sycl::queue>;
98 } // namespace ttg::device
99 #else
100 namespace ttg::device {
101  struct Stream { };
102  namespace detail {
103  template<>
105  static constexpr const Stream value = {};
106  };
107  } // namespace detail
109 } // namespace ttg::device
110 #endif
111 
112 namespace ttg::device {
113 
114 #if !defined(TTG_HAVE_LEVEL_ZERO)
115  namespace detail {
116  inline thread_local ttg::device::Device current_device_ts = {};
117  inline thread_local Stream current_stream_ts = detail::default_stream_v<Stream>; // default stream
118 
119  inline void reset_current() {
120  current_device_ts = {};
121  current_stream_ts = detail::default_stream_v<Stream>;
122  }
123 
124  inline void set_current(int device, Stream stream) {
126  current_stream_ts = stream;
127  }
128  } // namespace detail
129 
130  inline
133  }
134 
135  inline
138  }
139 
140  inline int num_devices() {
141  return TTG_IMPL_NS::num_devices();
142  }
143 
144 #else // TTG_HAVE_LEVEL_ZERO
145  /* SYCL needs special treatment because it uses pointers/references */
146  namespace detail {
147  inline thread_local ttg::device::Device current_device_ts = {};
148  inline thread_local sycl::queue* current_stream_ts = nullptr; // default stream
149 
150 
151  inline void reset_current() {
152  current_device_ts = {};
153  current_stream_ts = nullptr;
154  }
155 
156  inline void set_current(int device, sycl::queue& stream) {
158  current_stream_ts = &stream;
159  }
160  } // namespace detail
161 
162  inline
163  Device current_device() {
165  }
166 
167  inline
168  sycl::queue& current_stream() {
170  }
171 #endif // TTG_HAVE_LEVEL_ZERO
172 
173 } // 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
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition: device.h:59
const char * execution_space_name(ExecutionSpace space) noexcept
Definition: execution.h:26
thread_local ttg::device::Device current_device_ts
Definition: device.h:116
thread_local Stream current_stream_ts
Definition: device.h:117
void set_current(int device, Stream stream)
Definition: device.h:124
void reset_current()
Definition: device.h:119
constexpr const Stream default_stream_v
Definition: device.h:76
Stream current_stream()
Definition: device.h:136
Device current_device()
Definition: device.h:131
constexpr ttg::ExecutionSpace available_execution_space
Definition: device.h:108
int num_devices()
Definition: device.h:140
int num_devices()
Definition: device.h:41
ExecutionSpace
denotes task execution space
Definition: execution.h:17
static constexpr const Stream value
Definition: device.h:73