Newer
Older
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
/*
* ofp_context.hpp
*
* Created on: Nov 15, 2018
* Author: i-bird
*/
#ifndef OFP_CONTEXT_HXX_
#define OFP_CONTEXT_HXX_
#include <iostream>
#ifdef CUDA_ON_CPU
namespace gpu
{
enum gpu_context_opt
{
no_print_props,//!< no_print_props
print_props, //!< print_props
dummy //!< dummy
};
struct context_t {};
class ofp_context_t : public context_t
{
protected:
std::string _props;
openfpm::vector<aggregate<unsigned char>> tmem;
template<int no_arg = 0>
void init(int dev_num, gpu_context_opt opt)
{}
public:
/*! \brief gpu context constructor
*
* \param opt options for this gpu context
*
*/
ofp_context_t(gpu_context_opt opt = gpu_context_opt::no_print_props , int dev_num = 0, int stream_ = 0)
{}
~ofp_context_t()
{}
virtual const std::string& props() const
{
return _props;
}
virtual int ptx_version() const
{
return 0;
}
virtual int stream()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
return 0;
}
// Alloc GPU memory.
virtual void* alloc(size_t size, int space)
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
return NULL;
}
virtual void free(void* p, int space)
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
}
virtual void synchronize()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
}
virtual int event()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
return 0;
}
virtual void timer_begin()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
}
virtual double timer_end()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
return 0.0;
}
virtual int getDevice()
{
std::cout << __FILE__ << ":" << __LINE__ << " Not implemented" << std::endl;
return 0;
}
};
}
#else
#ifdef CUDA_GPU
#include "util/gpu_context.hpp"
namespace gpu
{
enum gpu_context_opt
{
no_print_props,//!< no_print_props
print_props, //!< print_props
dummy //!< dummy
};
////////////////////////////////////////////////////////////////////////////////
// ofp_context_t is a trivial implementation of context_t. Users can
// derive this type to provide a custom allocator.
class ofp_context_t : public context_t
{
protected:
cudaDeviceProp _props;
int _ptx_version;
cudaStream_t _stream;
cudaEvent_t _timer[2];
cudaEvent_t _event;
openfpm::vector_gpu<aggregate<unsigned char>> tmem;
openfpm::vector_gpu<aggregate<unsigned char>> tmem2;
openfpm::vector_gpu<aggregate<unsigned char>> tmem3;
// Making this a template argument means we won't generate an instance
// of empty_f for each translation unit.
template<int no_arg = 0>
void init(int dev_num, gpu_context_opt opt)
{
cudaFuncAttributes attr;
#ifdef __NVCC__
cudaError_t result = cudaFuncGetAttributes(&attr, (void *)empty_f<0>);
if(cudaSuccess != result) throw cuda_exception_t(result);
_ptx_version = attr.ptxVersion;
#else
_ptx_version = 60;
//std::cout << __FILE__ << ":" << __LINE__ << " Warning initialization of GPU context has been done from a standard Cpp file, rather than a CUDA or HIP file" << std::endl;
#endif
int num_dev;
cudaGetDeviceCount(&num_dev);
if (num_dev == 0) {return;}
if (opt != gpu_context_opt::dummy)
{
cudaSetDevice(dev_num % num_dev);
}
int ord;
cudaGetDevice(&ord);
cudaGetDeviceProperties(&_props, ord);
cudaEventCreate(&_timer[0]);
cudaEventCreate(&_timer[1]);
cudaEventCreate(&_event);
}
public:
/*! \brief gpu context constructor
*
* \param opt options for this gpu context
*
*/
ofp_context_t(gpu_context_opt opt = gpu_context_opt::no_print_props , int dev_num = 0, cudaStream_t stream_ = 0)
:context_t(), _stream(stream_)
{
init(dev_num,opt);
if(opt == gpu_context_opt::print_props)
{
printf("%s\n", device_prop_string(_props).c_str());
}
}
~ofp_context_t()
{
cudaEventDestroy(_timer[0]);
cudaEventDestroy(_timer[1]);
cudaEventDestroy(_event);
}
virtual const cudaDeviceProp& props() const { return _props; }
virtual int ptx_version() const { return _ptx_version; }
virtual cudaStream_t stream() { return _stream; }
// Alloc GPU memory.
virtual void* alloc(size_t size, memory_space_t space)
{
void* p = nullptr;
if(size)
{
cudaError_t result = (memory_space_device == space) ?cudaMalloc(&p, size) : cudaMallocHost(&p, size);
if(cudaSuccess != result) throw cuda_exception_t(result);
}
return p;
}
virtual void free(void* p, memory_space_t space)
{
if(p)
{
cudaError_t result = (memory_space_device == space) ? cudaFree(p) : cudaFreeHost(p);
if(cudaSuccess != result) throw cuda_exception_t(result);
}
}
virtual void synchronize()
{
cudaError_t result = _stream ?
cudaStreamSynchronize(_stream) :
cudaDeviceSynchronize();
if(cudaSuccess != result) throw cuda_exception_t(result);
}
virtual cudaEvent_t event()
{
return _event;
}
virtual void timer_begin()
{
cudaEventRecord(_timer[0], _stream);
}
virtual double timer_end()
{
cudaEventRecord(_timer[1], _stream);
cudaEventSynchronize(_timer[1]);
float ms;
cudaEventElapsedTime(&ms, _timer[0], _timer[1]);
return ms / 1.0e3;
}
virtual int getDevice()
{
int dev = 0;
cudaGetDevice(&dev);
return dev;
}
virtual int getNDevice()
{
int num_dev;
cudaGetDeviceCount(&num_dev);
return num_dev;
}
openfpm::vector_gpu<aggregate<unsigned char>> & getTemporalCUB()
{
return tmem;
}
openfpm::vector_gpu<aggregate<unsigned char>> & getTemporalCUB2()
{
return tmem2;
}
openfpm::vector_gpu<aggregate<unsigned char>> & getTemporalCUB3()
{
return tmem3;
}
};
}
#else
namespace gpu
{
enum gpu_context_opt
{
no_print_props,//!< no_print_props
print_props, //!< print_props
dummy //!< dummy
};
// Stub class for modern gpu
struct ofp_context_t
{
ofp_context_t(gpu_context_opt opt = gpu_context_opt::no_print_props , int dev_num = 0)
{}
};
}
#endif
#endif
#endif /* OFP_CONTEXT_HXX_ */