-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda_helper.h
479 lines (395 loc) · 15.7 KB
/
cuda_helper.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright 1993-2020 NVIDIA Corporation. All rights reserved.
*
* NOTICE TO USER:
*
* This source code is subject to NVIDIA ownership rights under U.S. and
* international Copyright laws. Users and possessors of this source code
* are hereby granted a nonexclusive, royalty-free license to use this code
* in individual and commercial software.
*
* NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
* CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
* IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
* IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOURCE CODE.
*
* U.S. Government End Users. This source code is a "commercial item" as
* that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of
* "commercial computer software" and "commercial computer software
* documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
* and is provided to the U.S. Government only as a commercial end item.
* Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
* 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
* source code with only those rights set forth herein.
*
* Any use of this source code in individual and commercial software must
* include, in the user documentation and internal comments to the code,
* the above Disclaimer and U.S. Government End Users Notice.
*/
#include <cassert>
#include <chrono>
#include <cstdio>
#include <memory>
#include <vector>
// *************** FOR ERROR CHECKING *******************
#ifndef CUDA_RT_CALL
#define CUDA_RT_CALL( call ) \
{ \
auto status = static_cast<cudaError_t>( call ); \
if ( status != cudaSuccess ) \
std::fprintf( stderr, \
"ERROR: CUDA RT call \"%s\" in line %d of file %s failed " \
"with " \
"%s (%d).\n", \
#call, \
__LINE__, \
__FILE__, \
cudaGetErrorString( status ), \
status ); \
}
#endif // CUDA_RT_CALL
// *************** FOR ERROR CHECKING *******************
// ***************** FOR NVTX MARKERS *******************
#ifdef USE_NVTX
#include "nvtx3/nvToolsExt.h"
const uint32_t colors[] = { 0xff00ff00, 0xff0000ff, 0xffffff00, 0xffff00ff, 0xff00ffff, 0xffff0000, 0xffffffff };
const int num_colors = sizeof( colors ) / sizeof( uint32_t );
#define PUSH_RANGE( name, cid ) \
{ \
int color_id = cid; \
color_id = color_id % num_colors; \
nvtxEventAttributes_t eventAttrib = { 0 }; \
eventAttrib.version = NVTX_VERSION; \
eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; \
eventAttrib.colorType = NVTX_COLOR_ARGB; \
eventAttrib.color = colors[color_id]; \
eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; \
eventAttrib.message.ascii = name; \
nvtxRangePushEx( &eventAttrib ); \
}
#define POP_RANGE( ) nvtxRangePop( );
#else
#define PUSH_RANGE( name, cid )
#define POP_RANGE( )
#endif
// ***************** FOR NVTX MARKERS *******************
// ****************** CLASS Add **********************
template<class O>
class Add {
public:
__host__ __device__ O operator( )( const O &a, const O &b ) const {
return a + b;
}
};
// ****************** CLASS Add **********************
// ****************** CLASS Sub **********************
template<class O>
class Sub {
public:
__host__ __device__ O operator( )( const O &a, const O &b ) const {
return a - b;
}
};
// ****************** CLASS Sub **********************
template<class O, typename T>
__global__ void VectorOperation( const int n, const T a, T *__restrict__ x, O op ) {
for ( int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x ) {
// printf("%d: %f %f %f\n", i, a, x[i], op( a, x[i] ));
x[i] = op( a, x[i] );
}
}
// ****************** CLASS MemCpy **********************
template<typename T>
class MemCpy {
public:
MemCpy( );
MemCpy( const size_t &N );
~MemCpy( ) noexcept;
virtual void run_chrono( ) = 0;
virtual void run_events( ) = 0;
virtual void run_nvtx( ) = 0;
const static size_t init_size { 1024 * 1024 * 256 };
protected:
// Host variables
const static size_t loops { 5 };
std::vector<T> key_A {};
std::vector<T> key_B {};
// Timing variables: Chrono
std::chrono::high_resolution_clock::time_point start {};
std::chrono::high_resolution_clock::time_point stop {};
std::chrono::duration<float, std::milli> elapsed_chrono_ms {};
float average_chrono_ms {};
// Timing variables: Events
cudaEvent_t start_event { nullptr };
cudaEvent_t stop_event { nullptr };
float elapsed_events_ms {};
float average_events_ms {};
double throughput {};
// CUDA streams
const static int num_streams { 2 };
cudaStream_t cuda_streams[num_streams];
// Helper functions
T *PagedAllocate( const size_t &N );
struct PagedMemoryDeleter {
void operator( )( T *ptr );
};
using UniquePagedPtr = std::unique_ptr<T, PagedMemoryDeleter>;
// Device variables
UniquePagedPtr d_a {};
UniquePagedPtr d_b {};
T a { 5.0f };
T b { 9.0f };
int device {};
int threads_per_block { 512 };
int blocks_per_grid {};
// Host functions
void get_chrono_results( const size_t &size );
void get_events_results( const size_t &size );
void reset( const size_t &N, T *a, T *b );
void verify( const size_t &N, T *a, T *b );
// Kernel Arguments
Add<T> *add_op {};
Sub<T> *sub_op {};
void * a_args[4];
void * b_args[4];
private:
int sm_count {};
void fill( const size_t &N, const T &x, T *input );
};
template<typename T>
MemCpy<T>::MemCpy( ) : add_op( new Add<T>( ) ), sub_op( new Sub<T>( ) ) {
CUDA_RT_CALL( cudaGetDevice( &device ) );
CUDA_RT_CALL( cudaDeviceGetAttribute( &sm_count, cudaDevAttrMultiProcessorCount, device ) );
CUDA_RT_CALL( cudaEventCreate( &start_event, cudaEventBlockingSync ) );
CUDA_RT_CALL( cudaEventCreate( &stop_event, cudaEventBlockingSync ) );
for ( int i = 0; i < num_streams; i++ ) {
CUDA_RT_CALL( cudaStreamCreate( &cuda_streams[i] ) );
}
blocks_per_grid = sm_count * 32;
}
template<typename T>
MemCpy<T>::MemCpy( const size_t &N ) :
add_op( new Add<T>( ) ),
sub_op( new Sub<T>( ) ),
d_a { PagedAllocate( N ) },
d_b { PagedAllocate( N ) } {
CUDA_RT_CALL( cudaGetDevice( &device ) );
CUDA_RT_CALL( cudaDeviceGetAttribute( &sm_count, cudaDevAttrMultiProcessorCount, device ) );
CUDA_RT_CALL( cudaEventCreate( &start_event, cudaEventBlockingSync ) );
CUDA_RT_CALL( cudaEventCreate( &stop_event, cudaEventBlockingSync ) );
for ( int i = 0; i < num_streams; i++ ) {
CUDA_RT_CALL( cudaStreamCreate( &cuda_streams[i] ) );
}
blocks_per_grid = sm_count * 32;
}
template<typename T>
MemCpy<T>::~MemCpy( ) noexcept {
CUDA_RT_CALL( cudaEventDestroy( start_event ) );
CUDA_RT_CALL( cudaEventDestroy( stop_event ) );
for ( int i = 0; i < num_streams; i++ ) {
CUDA_RT_CALL( cudaStreamDestroy( cuda_streams[i] ) );
}
delete[] add_op;
delete[] sub_op;
}
template<typename T>
T *MemCpy<T>::PagedAllocate( const size_t &N ) {
T * ptr { nullptr };
size_t bytes { N * sizeof( T ) };
CUDA_RT_CALL( cudaMalloc( reinterpret_cast<void **>( &ptr ), bytes ) );
return ( ptr );
}
template<typename T>
void MemCpy<T>::PagedMemoryDeleter::operator( )( T *ptr ) {
if ( ptr ) {
CUDA_RT_CALL( cudaFree( ptr ) );
}
};
template<typename T>
void MemCpy<T>::get_chrono_results( const size_t &size ) {
average_chrono_ms = elapsed_chrono_ms.count( ) / ( loops * 4 );
throughput = ( size * 1e-9 ) / ( average_chrono_ms * 0.001 );
std::printf( "Chrono: %0.6f ms @ %0.6f GB/s\n", average_chrono_ms, throughput );
}
template<typename T>
void MemCpy<T>::get_events_results( const size_t &size ) {
average_events_ms /= ( loops * 4 );
throughput = ( size * 1e-9 ) / ( average_events_ms * 0.001 );
std::printf( "Events: %0.6f ms @ %0.6f GB/s\n", average_events_ms, throughput );
}
template<typename T>
void MemCpy<T>::fill( const size_t &N, const T &x, T *input ) {
for ( int i = 0; i < N; i++ ) {
input[i] = x;
}
}
template<typename T>
void MemCpy<T>::reset( const size_t &N, T *x, T *y ) {
this->fill( N, a, x );
this->fill( N, b, y );
}
template<typename T>
void MemCpy<T>::verify( const size_t &N, T *x, T *y ) {
for ( int i = 0; i < N; i++ ) {
assert( x[i] == add_op->operator( )( b, a ) );
assert( y[i] == sub_op->operator( )( a, b ) );
}
}
// ****************** CLASS MemCpy **********************
// **************** CLASS MemCpyPaged *******************
template<typename T>
class MemCpyPaged : public MemCpy<T> {
public:
MemCpyPaged( ) noexcept = delete;
MemCpyPaged( const size_t &N );
~MemCpyPaged( ) noexcept;
void run_chrono( );
void run_events( );
void run_nvtx( );
size_t size {};
private:
// Host variables
std::vector<T> h_a_paged {};
std::vector<T> h_b_paged {};
size_t N {};
};
template<typename T>
MemCpyPaged<T>::MemCpyPaged( const size_t &N ) :
MemCpy<T>( N ),
N { N },
size { N * sizeof( T ) },
h_a_paged( N ),
h_b_paged( N ) {
this->a_args[0] = reinterpret_cast<void *>( &this->N );
this->a_args[1] = &this->b;
this->a_args[2] = &this->d_a;
this->a_args[3] = &this->add_op;
this->b_args[0] = reinterpret_cast<void *>( &this->N );
this->b_args[1] = &this->a;
this->b_args[2] = &this->d_b;
this->b_args[3] = &this->sub_op;
}
template<typename T>
MemCpyPaged<T>::~MemCpyPaged( ) noexcept {}
// **************** CLASS MemCpyPaged *******************
// **************** CLASS MemCpyPinned *******************
template<typename T>
class MemCpyPinned : public MemCpy<T> {
public:
MemCpyPinned( ) noexcept = delete;
MemCpyPinned( const size_t &N );
~MemCpyPinned( ) noexcept;
void run_chrono( );
void run_events( );
void run_nvtx( );
size_t size {};
private:
// Helper functions
T *PinnedAllocate( const size_t &N );
struct PinnedMemoryDeleter {
void operator( )( T *ptr );
};
using UniquePinnedPtr = std::unique_ptr<T, PinnedMemoryDeleter>;
// Host variables
UniquePinnedPtr h_a_pinned {};
UniquePinnedPtr h_b_pinned {};
size_t N {};
};
template<typename T>
MemCpyPinned<T>::MemCpyPinned( const size_t &N ) :
MemCpy<T>( N ),
N { N },
size { N * sizeof( T ) },
h_a_pinned { PinnedAllocate( N ) },
h_b_pinned { PinnedAllocate( N ) } {
this->a_args[0] = reinterpret_cast<void *>( &this->N );
this->a_args[1] = &this->b;
this->a_args[2] = &this->d_a;
this->a_args[3] = &this->add_op;
this->b_args[0] = reinterpret_cast<void *>( &this->N );
this->b_args[1] = &this->a;
this->b_args[2] = &this->d_b;
this->b_args[3] = &this->sub_op;
}
template<typename T>
MemCpyPinned<T>::~MemCpyPinned( ) noexcept {}
template<typename T>
T *MemCpyPinned<T>::PinnedAllocate( const size_t &N ) {
T * ptr { nullptr };
size_t bytes { N * sizeof( T ) };
CUDA_RT_CALL( cudaHostAlloc( reinterpret_cast<void **>( &ptr ), bytes, cudaHostAllocDefault ) );
return ( ptr );
}
template<typename T>
void MemCpyPinned<T>::PinnedMemoryDeleter::operator( )( T *ptr ) {
if ( ptr ) {
CUDA_RT_CALL( cudaFreeHost( ptr ) );
}
};
// **************** CLASS MemCpyPinned *******************
// **************** CLASS MemManaged *****************
template<typename T>
class MemManaged : public MemCpy<T> {
public:
MemManaged( ) noexcept = delete;
MemManaged( const size_t &N );
~MemManaged( ) noexcept;
void run_chrono( );
void run_events( );
void run_nvtx( );
size_t size {};
private:
// Helper functions
template<typename U>
U *UnifiedAllocate( const size_t &N );
template<typename U>
struct UnifiedMemoryDeleter {
void operator( )( U *ptr );
};
template<typename U>
using UniqueUnifiedPtr = std::unique_ptr<U, UnifiedMemoryDeleter<U>>;
// Host variables
UniqueUnifiedPtr<T> h_a_unified {};
UniqueUnifiedPtr<T> h_b_unified {};
size_t N {};
};
template<typename T>
MemManaged<T>::MemManaged( const size_t &N ) :
MemCpy<T>( ),
N { N },
size { N * sizeof( T ) },
h_a_unified { UnifiedAllocate<T>( N ) },
h_b_unified { UnifiedAllocate<T>( N ) } {
this->a_args[0] = reinterpret_cast<void *>( &this->N );
this->a_args[1] = &this->b;
this->a_args[2] = &this->h_a_unified;
this->a_args[3] = &this->add_op;
this->b_args[0] = reinterpret_cast<void *>( &this->N );
this->b_args[1] = &this->a;
this->b_args[2] = &this->h_b_unified;
this->b_args[3] = &this->sub_op;
}
template<typename T>
MemManaged<T>::~MemManaged( ) noexcept {}
template<typename T>
template<typename U>
U *MemManaged<T>::UnifiedAllocate( const size_t &N ) {
U * ptr { nullptr };
size_t bytes { N * sizeof( U ) };
CUDA_RT_CALL( cudaMallocManaged( reinterpret_cast<void **>( &ptr ), bytes ) );
return ( ptr );
}
template<typename T>
template<typename U>
void MemManaged<T>::UnifiedMemoryDeleter<U>::operator( )( U *ptr ) {
if ( ptr ) {
CUDA_RT_CALL( cudaFree( ptr ) );
}
};
// **************** CLASS MemManaged *****************