xpu
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1
10#ifndef XPU_COMMON_H
11#define XPU_COMMON_H
12
13#include "detail/common.h"
14#include "defines.h"
15#include "detail/buffer_registry.h"
16
17namespace xpu {
18
20 cpu = detail::cpu,
21 cuda = detail::cuda,
22 hip = detail::hip,
23 sycl = detail::sycl,
24};
25
26struct dim {
27 int x = -1;
28 int y = -1;
29 int z = -1;
30
31 dim() = default;
32 constexpr dim(int _x) : x(_x) {}
33 constexpr dim(int _x, int _y) : x(_x), y(_y) {}
34 constexpr dim(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
35
36 constexpr int ndims() const { return 3 - (y < 0) - (z < 0); }
37
38 constexpr int linear() const {
39 return x * (y <= 0 ? 1 : y) * (z <= 0 ? 1 : z);
40 }
41
42 #if XPU_IS_HIP || XPU_IS_CUDA
43 ::dim3 as_cuda_grid() const { return ::dim3{(unsigned int)x, (unsigned int)y, (unsigned int)z}; }
44 #endif
45};
46
51struct grid {
52
55
56 inline void get_compute_grid(dim &block_dim, dim &grid_dim) const;
57
58private:
59 friend inline grid n_blocks(dim);
60 friend inline grid n_threads(dim);
61 grid(dim b, dim t);
62
63};
64
68inline grid n_blocks(dim nblocks);
69
75inline grid n_threads(dim nthreads);
76
78 buf_pinned = detail::buf_pinned,
79 buf_device = detail::buf_device,
80 buf_managed = detail::buf_managed,
81 buf_io = detail::buf_io,
82 buf_stack = detail::buf_stack,
83};
84
85template<typename T>
86class buffer {
87
88public:
89 using value_type = T;
90
94 buffer() = default;
95
127 buffer(size_t N, buffer_type type, T *data = nullptr);
128
132 #if XPU_IS_CUDA || XPU_IS_HIP
133 // Hack to allow putting buffers into constant memory
134 ~buffer() = default;
135 #else
137 #if !XPU_IS_DEVICE_CODE
138 remove_ref();
139 #endif
140 }
141 #endif
142
143 XPU_H XPU_D buffer(const buffer &other);
147
148 template<typename U>
150
151 template<typename U>
153
154 template<typename U>
156
157 template<typename U>
159
161 void reset(size_t N, buffer_type type, T *data = nullptr);
162
163 XPU_H XPU_D T *get() const { return m_data; }
164
165 XPU_H XPU_D T &operator*() const { return *m_data; }
166 XPU_H XPU_D T *operator->() const { return m_data; }
167
168 XPU_H XPU_D T &operator[](size_t i) const { return m_data[i]; }
169
170private:
171 // Don't initialize buffer in cuda & hip, so it can be used in constant and shared memory
172 T *m_data
173 #if !XPU_IS_CUDA && !XPU_IS_HIP
174 = nullptr
175 #endif
176 ;
177
178 XPU_H XPU_D void add_ref();
179 XPU_H XPU_D void remove_ref();
180};
181
182} // namespace xpu
183
184#include "impl/common.tpp"
185
186#endif
Definition common.h:86
XPU_H XPU_D buffer(buffer< U > &&other)
XPU_H XPU_D T * operator->() const
Definition common.h:166
XPU_H XPU_D buffer & operator=(buffer &&other)
XPU_H XPU_D T * get() const
Definition common.h:163
XPU_H XPU_D void reset()
XPU_H XPU_D buffer(const buffer< U > &other)
XPU_H XPU_D T & operator[](size_t i) const
Definition common.h:168
T value_type
Definition common.h:89
buffer(size_t N, buffer_type type, T *data=nullptr)
Create a buffer with the given size.
XPU_H XPU_D T & operator*() const
Definition common.h:165
void reset(size_t N, buffer_type type, T *data=nullptr)
buffer()=default
Create an emtpy buffer.
XPU_H XPU_D buffer & operator=(const buffer< U > &other)
XPU_H XPU_D buffer & operator=(const buffer &other)
XPU_H XPU_D buffer(const buffer &other)
XPU_H XPU_D buffer & operator=(buffer< U > &&other)
XPU_H XPU_D ~buffer()
Free the buffer.
Definition common.h:136
XPU_H XPU_D buffer(buffer &&other)
Defines for xpu.
#define XPU_H
Function specifier for host functions. (Replaces host)
Definition defines.h:33
#define XPU_D
Function specifier for device functions. (Replaces device)
Definition defines.h:21
xpu default namespace.
Definition common.h:17
buffer_type
Definition common.h:77
@ buf_device
Definition common.h:79
@ buf_stack
Definition common.h:82
@ buf_io
Definition common.h:81
@ buf_managed
Definition common.h:80
@ buf_pinned
Definition common.h:78
grid n_blocks(dim nblocks)
Construct a grid with the given number of blocks in each dimension.
grid n_threads(dim nthreads)
Construct a grid with the given number of threads in each dimension If the number of threads is not a...
driver_t
Definition common.h:19
@ hip
Definition common.h:22
@ cuda
Definition common.h:21
@ sycl
Definition common.h:23
@ cpu
Definition common.h:20
Definition common.h:26
constexpr dim(int _x)
Definition common.h:32
constexpr int ndims() const
Definition common.h:36
constexpr dim(int _x, int _y)
Definition common.h:33
int y
Definition common.h:28
constexpr int linear() const
Definition common.h:38
int z
Definition common.h:29
constexpr dim(int _x, int _y, int _z)
Definition common.h:34
dim()=default
int x
Definition common.h:27
3d execution grid describing the number of blocks and threads of a kernel Use 'n_blocks' or 'n_thread...
Definition common.h:51
void get_compute_grid(dim &block_dim, dim &grid_dim) const
friend grid n_blocks(dim)
Construct a grid with the given number of blocks in each dimension.
dim nblocks
Definition common.h:53
friend grid n_threads(dim)
Construct a grid with the given number of threads in each dimension If the number of threads is not a...
dim nthreads
Definition common.h:54