forked from frankyeh/TIPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcu.hpp
More file actions
354 lines (323 loc) · 13.6 KB
/
Copy pathcu.hpp
File metadata and controls
354 lines (323 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#ifndef CU_HPP
#define CU_HPP
#include "utility/basic_image.hpp"
#include "def.hpp"
#include <iterator>
#include <type_traits>
#include <stdexcept>
#include <cstring>
#include <algorithm>
#ifdef __CUDACC__
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
#include <cuda.h>
#include <cuda_runtime.h>
#endif
namespace tipl {
// -------------------------------------------------------------------------
// DECLARATIONS FOR CUDA API WRAPPERS
// These tell g++ they exist elsewhere, preventing syntax/linking errors.
// -------------------------------------------------------------------------
template<typename T> void cu_malloc(T** ptr,size_t count);
template<typename T> void cu_free(T* ptr);
template<typename T> void cu_malloc_host(T** ptr,size_t count);
template<typename T> void cu_free_host(T* ptr);
template<typename Dest,typename Src> void cu_copy_d2d(Dest* dest,const Src* src,size_t count);
template<typename Dest,typename Src> void cu_copy_h2d(Dest* dest,const Src* src,size_t count);
template<typename Dest,typename Src> void cu_copy_d2h(Dest* dest,const Src* src,size_t count);
template<typename Dest,typename Src> void cu_copy_h2h(Dest* dest,const Src* src,size_t count);
template<typename T> void cu_memset(T* dest,int val,size_t count);
template<typename T> void cu_fill(T* dest,size_t count,T val);
template<typename T> T cu_eval(const T* ptr);
template<typename vtype>
class device_vector{
public:
using value_type = vtype;
using iterator = value_type*;
using const_iterator = const value_type*;
using reference = value_type;
private:
value_type* buf = nullptr;
size_t buf_size = 0;
size_t s = 0;
public:
device_vector(size_t new_size,bool init = true) {resize(new_size,init);}
device_vector(device_vector&& rhs)noexcept {swap(rhs);}
device_vector(const device_vector& rhs) {copy_from(rhs);}
device_vector(void){}
template<typename T,typename std::enable_if<std::is_class_v<T>,bool>::type = true>
device_vector(const T& rhs) {copy_from(rhs);}
template<typename T,typename std::enable_if<std::is_class_v<T>,bool>::type = true>
device_vector(T from,T to)
{
copy_from(&*from,to-from);
}
template<typename T>
device_vector(const T* from,const T* to)
{
if constexpr(std::is_same_v<T,void>)
copy_from(from,(to-from)/sizeof(value_type));
else
copy_from(from,to-from);
}
~device_vector(void)
{
if(buf)
{
if constexpr(tipl::use_cuda)
cu_free(buf);
buf = nullptr;
buf_size = 0;
s = 0;
}
}
public:
device_vector& operator=(const device_vector& rhs)
{
return copy_from(rhs),*this;
}
template<typename T>
device_vector& operator=(const T& rhs)
{
return copy_from(rhs),*this;
}
device_vector& operator=(device_vector&& rhs) noexcept
{
return swap(rhs),*this;
}
void clear(void)
{
s = 0;
}
template<typename T>
void copy_from(const T* from,size_t size)
{
resize(size,false);
if(s)
{
if constexpr(std::is_same_v<T,void>)
cu_copy_d2d(buf,from,size);
else
cu_copy_h2d(buf,from,size);
}
}
template<typename T,typename std::enable_if<std::is_class<T>::value,bool>::type = true>
void copy_from(T from,size_t size)
{
copy_from(&*from,size);
}
template<typename T,typename std::enable_if<std::is_class<T>::value,bool>::type = true>
void copy_from(const T& rhs)
{
resize(rhs.size(),false);
if(s)
{
if constexpr(memory_location<T>::at == CUDA)
cu_copy_d2d(buf,rhs.data(),s);
else
cu_copy_h2d(buf,rhs.data(),s);
}
}
template<typename T,typename std::enable_if<std::is_class<T>::value,bool>::type = true>
void copy_to(T& rhs) const
{
if(s)
{
if constexpr(memory_location<T>::at == CUDA)
cu_copy_d2d(rhs.data(),buf,s);
else
cu_copy_d2h(rhs.data(),buf,s);
}
}
void resize(size_t new_s,bool init = true)
{
if(s == new_s)
return;
if(new_s > buf_size) // need reallocation
{
value_type* new_buf;
cu_malloc(&new_buf,new_s);
if(s) cu_copy_d2d(new_buf,buf,s);
if(buf) cu_free(buf);
buf = new_buf;
buf_size = new_s;
}
if(new_s > s && init)
{
size_t added_s = new_s-s;
if constexpr(std::is_integral<value_type>::value || std::is_pointer<value_type>::value)
{
cu_memset(buf+s,0,added_s);
}
else
{
if constexpr(std::is_class<value_type>::value)
cu_fill(buf+s,added_s,value_type());
else if constexpr(std::is_floating_point<value_type>::value)
cu_fill(buf+s,added_s,value_type(0));
}
}
s = new_s;
}
public:
void swap(device_vector& rhs)
{
std::swap(buf,rhs.buf);
std::swap(buf_size,rhs.buf_size);
std::swap(s,rhs.s);
}
__INLINE__ size_t size(void) const {return s;}
__INLINE__ bool empty(void) const {return s==0;}
public: // only in device memory
template<typename index_type>
value_type operator[](index_type index) const
{
value_type result;
cu_copy_d2h(&result,&buf[index],1);
return result;
}
public:
__INLINE__ iterator data(void) {return buf;}
__INLINE__ const_iterator data(void) const {return buf;}
__INLINE__ const void* begin(void) const {return buf;}
__INLINE__ const void* end(void) const {return buf+s;}
__INLINE__ void* begin(void) {return buf;}
__INLINE__ void* end(void) {return buf+s;}
};
template<int dim,typename vtype = float>
using device_image = image<dim,vtype,device_vector>;
template<typename vtype>
struct shared_device_vector{
public:
using value_type = vtype;
using iterator = value_type*;
using const_iterator = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
private:
union{
iterator buf;
const_iterator const_buf;
};
size_t s = 0;
public:
shared_device_vector(void){}
shared_device_vector(device_vector<value_type>& rhs):buf(rhs.data()),s(rhs.size()) {}
shared_device_vector(const device_vector<value_type>& rhs):const_buf(rhs.data()),s(rhs.size()) {}
shared_device_vector(shared_device_vector<value_type>& rhs):buf(rhs.buf),s(rhs.s) {}
shared_device_vector(const shared_device_vector<value_type>& rhs):const_buf(rhs.const_buf),s(rhs.s) {}
shared_device_vector(iterator buf_,size_t s_):buf(buf_),s(s_) {}
shared_device_vector(const_iterator buf_,size_t s_):const_buf(buf_),s(s_) {}
public:
shared_device_vector& operator=(const shared_device_vector<value_type>& rhs) const{const_buf = rhs.const_buf;s=rhs.s;return *this;}
shared_device_vector& operator=(shared_device_vector<value_type>& rhs) {buf = rhs.buf;s=rhs.s;return *this;}
public:
__INLINE__ size_t size(void) const {return s;}
__INLINE__ bool empty(void) const {return s==0;}
public:
__INLINE__ const_iterator begin(void) const {return const_buf;}
__INLINE__ const_iterator end(void) const {return const_buf+s;}
__INLINE__ const_iterator data(void) const {return const_buf;}
template<typename index_type>
__INLINE__ const_reference operator[](index_type index) const {return const_buf[index];}
public:
__INLINE__ iterator begin(void) {return buf;}
__INLINE__ iterator end(void) {return buf+s;}
__INLINE__ iterator data(void) {return buf;}
template<typename index_type>
__INLINE__ reference operator[](index_type index) {return buf[index];}
};
template<typename T>
__INLINE__ auto make_shared(device_vector<T>& I)
{
return shared_device_vector<T>(I);
}
template<typename T>
__INLINE__ const auto make_shared(const device_vector<T>& I)
{
return shared_device_vector<T>(I);
}
template<typename vtype>
struct memory_location<device_vector<vtype> > {static constexpr memory_location_type at = CUDA;};
template<typename vtype>
struct memory_location<shared_device_vector<vtype> > {static constexpr memory_location_type at = CUDA;};
template<int dim,typename vtype>
struct memory_location<device_image<dim,vtype> > {static constexpr memory_location_type at = CUDA;};
} //namespace tipl
// -------------------------------------------------------------------------
// CUDA EXPLICIT IMPLEMENTATIONS & INSTANTIATIONS
// -------------------------------------------------------------------------
#ifdef __CUDACC__
namespace tipl {
template<typename T>
__global__ void device_vector_fill(T* buf,size_t size,T v)
{
TIPL_FOR(index,size)
buf[index] = v;
}
// Wrapper Implementations
template<typename T> void cu_malloc(T** ptr,size_t count) {
if(cudaMalloc(ptr,sizeof(T) * count) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename T> void cu_free(T* ptr) {
if(ptr) cudaFree(ptr);
}
template<typename T> void cu_malloc_host(T** ptr,size_t count) {
if(cudaMallocHost(ptr,sizeof(T) * count) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename T> void cu_free_host(T* ptr) {
if(ptr) cudaFreeHost(ptr);
}
template<typename Dest,typename Src> void cu_copy_d2d(Dest* dest,const Src* src,size_t count) {
if(cudaMemcpy(dest,src,count * sizeof(Dest),cudaMemcpyDeviceToDevice) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename Dest,typename Src> void cu_copy_h2d(Dest* dest,const Src* src,size_t count) {
if(cudaMemcpy(dest,src,count * sizeof(Dest),cudaMemcpyHostToDevice) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename Dest,typename Src> void cu_copy_d2h(Dest* dest,const Src* src,size_t count) {
if(cudaMemcpy(dest,src,count * sizeof(Dest),cudaMemcpyDeviceToHost) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename Dest,typename Src> void cu_copy_h2h(Dest* dest,const Src* src,size_t count) {
if(cudaMemcpy(dest,src,count * sizeof(Dest),cudaMemcpyHostToHost) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename T> void cu_memset(T* dest,int val,size_t count) {
if(cudaMemset(dest,val,count * sizeof(T)) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
template<typename T> void cu_fill(T* dest,size_t count,T val) {
TIPL_RUN(device_vector_fill,count)(dest,count,val);
}
template<typename T> T cu_eval(const T* ptr) {
T v;
if(cudaMemcpy(&v,ptr,sizeof(T),cudaMemcpyDeviceToHost) != cudaSuccess) throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
return v;
}
} // namespace tipl
// Explicit template instantiations to solve ODR when linked to external object files
#define INSTANTIATE_CU_WRAPPERS(T) \
template void tipl::cu_malloc<T>(T**,size_t); \
template void tipl::cu_free<T>(T*); \
template void tipl::cu_malloc_host<T>(T**,size_t); \
template void tipl::cu_free_host<T>(T*); \
template void tipl::cu_copy_d2d<T,void>(T*,const void*,size_t); \
template void tipl::cu_copy_d2d<T,T>(T*,const T*,size_t); \
template void tipl::cu_copy_h2d<T,void>(T*,const void*,size_t); \
template void tipl::cu_copy_h2d<T,T>(T*,const T*,size_t); \
template void tipl::cu_copy_d2h<T,void>(T*,const void*,size_t); \
template void tipl::cu_copy_d2h<T,T>(T*,const T*,size_t); \
template void tipl::cu_copy_h2h<T,void>(T*,const void*,size_t); \
template void tipl::cu_copy_h2h<T,T>(T*,const T*,size_t); \
template void tipl::cu_memset<T>(T*,int,size_t); \
template void tipl::cu_fill<T>(T*,size_t,T); \
template T tipl::cu_eval<T>(const T*);
INSTANTIATE_CU_WRAPPERS(float)
INSTANTIATE_CU_WRAPPERS(double)
INSTANTIATE_CU_WRAPPERS(int)
INSTANTIATE_CU_WRAPPERS(unsigned int)
INSTANTIATE_CU_WRAPPERS(unsigned char)
INSTANTIATE_CU_WRAPPERS(short)
INSTANTIATE_CU_WRAPPERS(size_t)
#endif // __CUDACC__
#endif//CU_HPP