ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#pragma once
3
4#include "ttg/config.h"
5#include "ttg/execution.h"
6#include "ttg/impl_selector.h"
7#include "ttg/fwd.h"
8#include "ttg/util/meta.h"
9
10
11
12namespace ttg::device {
13
14
15 /* fwd-decl */
16 inline int num_devices();
17
19 class Device {
20 int m_id = 0;
22
23 public:
24 Device() = default;
26 : m_id(id)
27 , m_space(space)
28 { }
29
30 Device(const Device&) = default;
31 Device& operator=(const Device&) = default;
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 /* next device, cycling through all devices */
65 Device cycle() const {
66 if (m_space == ttg::ExecutionSpace::Host) {
67 return {};
68 }
69 return {(m_id + 1) % num_devices(), m_space};
70 }
71
72 static Device host() {
73 return {};
74 }
75
76 bool operator==(const Device& other) const {
77 return m_id == other.m_id && m_space == other.m_space;
78 }
79 };
80} // namespace ttg::device
81
82namespace std {
83 inline
84 std::ostream& operator<<(std::ostream& os, ttg::device::Device device) {
86 if (device.is_device()) {
87 os << "(" << device.id() << ")";
88 }
89 return os;
90 }
91} // namespace std
92
93namespace ttg::device {
94
95 namespace detail {
96 template<typename Stream>
98 static constexpr const Stream value = 0;
99 };
100 template<typename Stream>
102 } // namespace detail
103
104} // namespace ttg
105
106#if defined(TTG_HAVE_CUDA)
107#include <cuda_runtime.h>
108namespace ttg::device {
110 using Stream = cudaStream_t;
111} // namespace ttg::device
112#elif defined(TTG_HAVE_HIP)
113#include <hip/hip_runtime.h>
114namespace ttg::device {
116 using Stream = hipStream_t;
117} // namespace ttg::device
118#elif defined(TTG_HAVE_LEVEL_ZERO)
119#include <CL/sycl.hpp>
120namespace ttg::device {
122 using Stream = std::add_reference_t<sycl::queue>;
123} // namespace ttg::device
124#else
125namespace ttg::device {
126 struct Stream { };
127 namespace detail {
128 template<>
130 static constexpr const Stream value = {};
131 };
132 } // namespace detail
134} // namespace ttg::device
135#endif
136
137namespace ttg::device {
138
139#if !defined(TTG_HAVE_LEVEL_ZERO)
140 namespace detail {
141 inline thread_local ttg::device::Device current_device_ts = {};
142 inline thread_local Stream current_stream_ts = detail::default_stream_v<Stream>; // default stream
143
144 inline void reset_current() {
146 current_stream_ts = detail::default_stream_v<Stream>;
147 }
148
149 inline void set_current(int device, Stream stream) {
151 current_stream_ts = stream;
152 }
153 } // namespace detail
154
155 inline
159
160 inline
164
165 inline int num_devices() {
166 return TTG_IMPL_NS::num_devices();
167 }
168
169#else // TTG_HAVE_LEVEL_ZERO
170 /* SYCL needs special treatment because it uses pointers/references */
171 namespace detail {
172 inline thread_local ttg::device::Device current_device_ts = {};
173 inline thread_local sycl::queue* current_stream_ts = nullptr; // default stream
174
175
176 inline void reset_current() {
178 current_stream_ts = nullptr;
179 }
180
181 inline void set_current(int device, sycl::queue& stream) {
183 current_stream_ts = &stream;
184 }
185 } // namespace detail
186
187 inline
188 Device current_device() {
190 }
191
192 inline
193 sycl::queue& current_stream() {
195 }
196#endif // TTG_HAVE_LEVEL_ZERO
197
198} // namespace ttg
Represents a device in a specific execution space.
Definition device.h:19
bool is_host() const
Definition device.h:56
bool is_device() const
Definition device.h:52
Device(const Device &)=default
bool operator==(const Device &other) const
Definition device.h:76
ttg::ExecutionSpace space() const
Definition device.h:48
bool is_invalid() const
Definition device.h:60
Device cycle() const
Definition device.h:65
Device & operator=(const Device &)=default
Device(int id, ttg::ExecutionSpace space)
Definition device.h:25
int id() const
Definition device.h:34
static Device host()
Definition device.h:72
STL namespace.
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition device.h:84
const char * execution_space_name(ExecutionSpace space) noexcept
Definition execution.h:27
thread_local ttg::device::Device current_device_ts
Definition device.h:141
thread_local Stream current_stream_ts
Definition device.h:142
void set_current(int device, Stream stream)
Definition device.h:149
void reset_current()
Definition device.h:144
constexpr const Stream default_stream_v
Definition device.h:101
Stream current_stream()
Definition device.h:161
Device current_device()
Definition device.h:156
constexpr ttg::ExecutionSpace available_execution_space
Definition device.h:133
int num_devices()
Definition device.h:165
ExecutionSpace
denotes task execution space
Definition execution.h:18
static constexpr const Stream value
Definition device.h:98