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 
8 
9 
10 namespace ttg::device {
11 
12 #if defined(TTG_HAVE_CUDA)
14 #elif defined(TTG_HAVE_HIP)
16 #elif defined(TTG_HAVE_LEVEL_ZERO)
18 #else
20 #endif
21 
23  class Device {
24  int m_id = 0;
26 
27  public:
28  Device() = default;
30  : m_id(id)
31  , m_space(space)
32  { }
33 
34  int id() const {
35  if (is_host()) {
36  throw std::runtime_error("No valid ID for Host execution space!");
37  }
38  if (is_invalid()) {
39  throw std::runtime_error("Invalid execution space!");
40  }
41  return m_id;
42  }
43 
44  operator int() const {
45  return id();
46  }
47 
49  return m_space;
50  }
51 
52  bool is_device() const {
53  return !is_host();
54  }
55 
56  bool is_host() const {
57  return !is_invalid() && (m_space == ttg::ExecutionSpace::Host);
58  }
59 
60  bool is_invalid() const {
61  return (m_space == ttg::ExecutionSpace::Invalid);
62  }
63  };
64 } // namespace ttg::device
65 
66 namespace std {
67  inline
68  std::ostream& operator<<(std::ostream& os, ttg::device::Device device) {
70  if (device.is_device()) {
71  os << "(" << device.id() << ")";
72  }
73  return os;
74  }
75 } // namespace std
76 
77 #if defined(TTG_HAVE_CUDA)
78 #include <cuda_runtime.h>
79 
80 namespace ttg::device {
81  namespace detail {
82  inline thread_local ttg::device::Device current_device_ts = {};
83  inline thread_local cudaStream_t current_stream_ts = 0; // default stream
84 
85  inline void reset_current() {
86  current_device_ts = {};
87  current_stream_ts = 0;
88  }
89 
90  inline void set_current(int device, cudaStream_t stream) {
91  current_device_ts = ttg::device::Device(device, ttg::ExecutionSpace::CUDA);
92  current_stream_ts = stream;
93  }
94  } // namespace detail
95 
96  inline
97  Device current_device() {
98  return detail::current_device_ts;
99  }
100 
101  inline
102  cudaStream_t current_stream() {
103  return detail::current_stream_ts;
104  }
105 } // namespace ttg
106 
107 #elif defined(TTG_HAVE_HIP)
108 
109 #include <hip/hip_runtime.h>
110 
111 namespace ttg::device {
112  namespace detail {
113  inline thread_local ttg::device::Device current_device_ts = {};
114  inline thread_local hipStream_t current_stream_ts = 0; // default stream
115 
116  inline void reset_current() {
117  current_device_ts = {};
118  current_stream_ts = 0;
119  }
120 
121  inline void set_current(int device, hipStream_t stream) {
122  current_device_ts = ttg::device::Device(device, ttg::ExecutionSpace::HIP);
123  current_stream_ts = stream;
124  }
125  } // namespace detail
126 
127  inline
128  Device current_device() {
129  return detail::current_device_ts;
130  }
131 
132  inline
133  hipStream_t current_stream() {
134  return detail::current_stream_ts;
135  }
136 } // namespace ttg
137 
138 #elif defined(TTG_HAVE_LEVEL_ZERO)
139 
140 #include <CL/sycl.hpp>
141 
142 namespace ttg::device {
143  namespace detail {
144  inline thread_local ttg::device::Device current_device_ts = {};
145  inline thread_local sycl::queue* current_stream_ts = nullptr; // default stream
146 
147 
148  inline void reset_current() {
149  current_device_ts = {};
150  current_stream_ts = nullptr;
151  }
152 
153  inline void set_current(int device, sycl::queue& stream) {
154  current_device_ts = ttg::device::Device(device, ttg::ExecutionSpace::HIP);
155  current_stream_ts = &stream;
156  }
157  } // namespace detail
158 
159  inline
160  Device current_device() {
161  return detail::current_device_ts;
162  }
163 
164  inline
165  sycl::queue& current_stream() {
166  return *detail::current_stream_ts;
167  }
168 } // namespace ttg
169 
170 #else
171 
172 namespace ttg::device {
174  return {};
175  }
176 
177  template<ttg::ExecutionSpace Space = ttg::ExecutionSpace::Invalid>
178  inline const void* current_stream() {
179  static_assert(Space != ttg::ExecutionSpace::Invalid,
180  "TTG was built without any known device support so we cannot provide a current stream!");
181  return nullptr;
182  }
183 } // namespace ttg
184 #endif // defined(TTG_HAVE_HIP)
185 
186 namespace ttg::device {
187  inline int num_devices() {
188  return TTG_IMPL_NS::num_devices();
189  }
190 }
Represents a device in a specific execution space.
Definition: device.h:23
bool is_host() const
Definition: device.h:56
bool is_device() const
Definition: device.h:52
ttg::ExecutionSpace space() const
Definition: device.h:48
bool is_invalid() const
Definition: device.h:60
Device(int id, ttg::ExecutionSpace space)
Definition: device.h:29
int id() const
Definition: device.h:34
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition: device.h:68
const char * execution_space_name(ExecutionSpace space) noexcept
Definition: execution.h:26
Device current_device()
Definition: device.h:173
constexpr ttg::ExecutionSpace available_execution_space
Definition: device.h:19
const void * current_stream()
Definition: device.h:178
int num_devices()
Definition: device.h:187
int num_devices()
Definition: device.h:41
ExecutionSpace
denotes task execution space
Definition: execution.h:17