-
Notifications
You must be signed in to change notification settings - Fork 100
/
CudaKernel.hpp
541 lines (418 loc) · 14.5 KB
/
CudaKernel.hpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*!
******************************************************************************
*
* \file
*
* \brief RAJA header file containing constructs used to run kernel
* traversals on GPU with CUDA.
*
******************************************************************************
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
#ifndef RAJA_policy_cuda_kernel_CudaKernel_HPP
#define RAJA_policy_cuda_kernel_CudaKernel_HPP
#include "RAJA/config.hpp"
#if defined(RAJA_ENABLE_CUDA)
#include <cassert>
#include <climits>
#include "camp/camp.hpp"
#include "RAJA/util/macros.hpp"
#include "RAJA/util/types.hpp"
#include "RAJA/pattern/kernel.hpp"
#include "RAJA/pattern/kernel/For.hpp"
#include "RAJA/pattern/kernel/Lambda.hpp"
#include "RAJA/policy/cuda/MemUtils_CUDA.hpp"
#include "RAJA/policy/cuda/policy.hpp"
#include "RAJA/internal/LegacyCompatibility.hpp"
#include "RAJA/policy/cuda/kernel/internal.hpp"
namespace RAJA
{
/*!
* CUDA kernel launch policy where the user may specify the number of physical
* thread blocks and threads per block.
* If num_blocks is 0 and num_threads is non-zero then num_blocks is chosen at
* runtime.
* Num_blocks is chosen to maximize the number of blocks running concurrently.
* If num_threads and num_blocks are both 0 then num_threads and num_blocks are
* chosen at runtime.
* Num_threads and num_blocks are determined by the CUDA occupancy calculator.
* If num_threads is 0 and num_blocks is non-zero then num_threads is chosen at
* runtime.
* Num_threads is 1024, which may not be appropriate for all kernels.
*/
template <bool async0, int num_blocks, int num_threads>
struct cuda_launch {};
/*!
* CUDA kernel launch policy where the user specifies the number of physical
* thread blocks and threads per block.
* If num_blocks is 0 then num_blocks is chosen at runtime.
* Num_blocks is chosen to maximize the number of blocks running concurrently.
*/
template <bool async0, int num_blocks, int num_threads>
using cuda_explicit_launch = cuda_launch<async0, num_blocks, num_threads>;
/*!
* CUDA kernel launch policy where the number of physical blocks and threads
* are determined by the CUDA occupancy calculator.
* If num_threads is 0 then num_threads is chosen at runtime.
*/
template <int num_threads0, bool async0>
using cuda_occ_calc_launch = cuda_launch<async0, 0, num_threads0>;
namespace statement
{
/*!
* A RAJA::kernel statement that launches a CUDA kernel.
*
*
*/
template <typename LaunchConfig, typename... EnclosedStmts>
struct CudaKernelExt
: public internal::Statement<cuda_exec<0>, EnclosedStmts...> {
};
/*!
* A RAJA::kernel statement that launches a CUDA kernel with the flexibility
* to fix the number of threads and/or blocks and let the CUDA occupancy
* calculator determine the unspecified values.
* The kernel launch is synchronous.
*/
template <int num_blocks, int num_threads, typename... EnclosedStmts>
using CudaKernelExp =
CudaKernelExt<cuda_launch<false, num_blocks, num_threads>, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel with the flexibility
* to fix the number of threads and/or blocks and let the CUDA occupancy
* calculator determine the unspecified values.
* The kernel launch is asynchronous.
*/
template <int num_blocks, int num_threads, typename... EnclosedStmts>
using CudaKernelExpAsync =
CudaKernelExt<cuda_launch<true, num_blocks, num_threads>, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel using the
* CUDA occupancy calculator to determine the optimal number of threads.
* The kernel launch is synchronous.
*/
template <typename... EnclosedStmts>
using CudaKernelOcc =
CudaKernelExt<cuda_occ_calc_launch<1024, false>, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel using the
* CUDA occupancy calculator to determine the optimal number of threads.
* The kernel launch is asynchronous.
*/
template <typename... EnclosedStmts>
using CudaKernelOccAsync =
CudaKernelExt<cuda_occ_calc_launch<1024, true>, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel with a fixed
* number of threads (specified by num_threads)
* The kernel launch is synchronous.
*/
template <int num_threads, typename... EnclosedStmts>
using CudaKernelFixed =
CudaKernelExt<cuda_explicit_launch<false, 0, num_threads>,
EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel with a fixed
* number of threads (specified by num_threads)
* The kernel launch is asynchronous.
*/
template <int num_threads, typename... EnclosedStmts>
using CudaKernelFixedAsync =
CudaKernelExt<cuda_explicit_launch<true, 0, num_threads>, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel with 1024 threads
* Thre kernel launch is synchronous.
*/
template <typename... EnclosedStmts>
using CudaKernel = CudaKernelFixed<1024, EnclosedStmts...>;
/*!
* A RAJA::kernel statement that launches a CUDA kernel with 1024 threads
* Thre kernel launch is asynchronous.
*/
template <typename... EnclosedStmts>
using CudaKernelAsync = CudaKernelFixedAsync<1024, EnclosedStmts...>;
} // namespace statement
namespace internal
{
/*!
* CUDA global function for launching CudaKernel policies
*/
template <typename Data, typename Exec>
__global__ void CudaKernelLauncher(Data data)
{
using data_t = camp::decay<Data>;
data_t private_data = data;
Exec::exec(private_data, true);
}
/*!
* CUDA global function for launching CudaKernel policies
* This is annotated to gaurantee that device code generated
* can be launched by a kernel with BlockSize number of threads.
*
* This launcher is used by the CudaKerelFixed policies.
*/
template <size_t BlockSize, typename Data, typename Exec>
__launch_bounds__(BlockSize, 1) __global__
void CudaKernelLauncherFixed(Data data)
{
using data_t = camp::decay<Data>;
data_t private_data = data;
// execute the the object
Exec::exec(private_data, true);
}
/*!
* Helper class that handles CUDA kernel launching, and computing
* maximum number of threads/blocks
*/
template<typename LaunchPolicy, typename StmtList, typename Data>
struct CudaLaunchHelper;
/*!
* Helper class specialization to determine the number of threads and blocks.
* The user may specify the number of threads and blocks or let one or both be
* determined at runtime using the CUDA occupancy calculator.
*/
template<bool async0, int num_blocks, int num_threads, typename StmtList, typename Data>
struct CudaLaunchHelper<cuda_launch<async0, num_blocks, num_threads>,StmtList,Data>
{
using Self = CudaLaunchHelper;
static constexpr bool async = async0;
using executor_t = internal::cuda_statement_list_executor_t<StmtList, Data>;
inline static void recommended_blocks_threads(int shmem_size,
int &recommended_blocks, int &recommended_threads)
{
auto func = internal::CudaKernelLauncher<Data, executor_t>;
if (num_blocks <= 0) {
if (num_threads <= 0) {
//
// determine blocks at runtime
// determine threads at runtime
//
internal::cuda_occupancy_max_blocks_threads<Self>(
func, shmem_size, recommended_blocks, recommended_threads);
} else {
//
// determine blocks at runtime
// threads determined at compile-time
//
recommended_threads = num_threads;
internal::cuda_occupancy_max_blocks<Self, num_threads>(
func, shmem_size, recommended_blocks);
}
} else {
if (num_threads <= 0) {
//
// determine threads at runtime, unsure what use 1024
// this value may be invalid for kernels with high register pressure
//
recommended_threads = 1024;
} else {
//
// threads determined at compile-time
//
recommended_threads = num_threads;
}
//
// blocks determined at compile-time
//
recommended_blocks = num_blocks;
}
}
inline static void max_threads(int RAJA_UNUSED_ARG(shmem_size), int &max_threads)
{
if (num_threads <= 0) {
//
// determine threads at runtime, unsure what use 1024
// this value may be invalid for kernels with high register pressure
//
max_threads = 1024;
} else {
//
// threads determined at compile-time
//
max_threads = num_threads;
}
}
inline static void max_blocks(int shmem_size,
int &max_blocks, int actual_threads)
{
auto func = internal::CudaKernelLauncher<Data, executor_t>;
if (num_blocks <= 0) {
//
// determine blocks at runtime
//
internal::cuda_occupancy_max_blocks<Self>(
func, shmem_size, max_blocks, actual_threads);
} else {
//
// blocks determined at compile-time
//
max_blocks = num_blocks;
}
}
static void launch(Data &&data,
internal::LaunchDims launch_dims,
size_t shmem,
cudaStream_t stream)
{
auto func = internal::CudaKernelLauncher<Data, executor_t>;
void *args[] = {(void*)&data};
RAJA::cuda::launch((const void*)func, launch_dims.blocks, launch_dims.threads, args, shmem, stream);
}
};
/*!
* Helper function that is used to compute either the number of blocks
* or threads that get launched.
* It takes the max threads (limit), the requested number (result),
* and a minimum limit (minimum).
*
* The algorithm is greedy (and probably could be improved), and favors
* maximizing the number of threads (or blocks) in x, y, then z.
*/
inline
cuda_dim_t fitCudaDims(unsigned int limit, cuda_dim_t result, cuda_dim_t minimum = cuda_dim_t()){
// clamp things to at least 1
result.x = result.x ? result.x : 1;
result.y = result.y ? result.y : 1;
result.z = result.z ? result.z : 1;
minimum.x = minimum.x ? minimum.x : 1;
minimum.y = minimum.y ? minimum.y : 1;
minimum.z = minimum.z ? minimum.z : 1;
// if we are under the limit, we're done
if(result.x * result.y * result.z <= limit) return result;
// Can we reduce z to fit?
if(result.x * result.y * minimum.z < limit){
// compute a new z
result.z = limit / (result.x*result.y);
return result;
}
// we don't fit, so reduce z to it's minimum and continue on to y
result.z = minimum.z;
// Can we reduce y to fit?
if(result.x * minimum.y * result.z < limit){
// compute a new y
result.y = limit / (result.x*result.z);
return result;
}
// we don't fit, so reduce y to it's minimum and continue on to x
result.y = minimum.y;
// Can we reduce y to fit?
if(minimum.x * result.y * result.z < limit){
// compute a new x
result.x = limit / (result.y*result.z);
return result;
}
// we don't fit, so we'll return the smallest possible thing
result.x = minimum.x;
return result;
}
/*!
* Specialization that launches CUDA kernels for RAJA::kernel from host code
*/
template <typename LaunchConfig, typename... EnclosedStmts>
struct StatementExecutor<
statement::CudaKernelExt<LaunchConfig, EnclosedStmts...>> {
using stmt_list_t = StatementList<EnclosedStmts...>;
using StatementType =
statement::CudaKernelExt<LaunchConfig, EnclosedStmts...>;
template <typename Data>
static inline void exec(Data &&data)
{
using data_t = camp::decay<Data>;
using executor_t = cuda_statement_list_executor_t<stmt_list_t, data_t>;
using launch_t = CudaLaunchHelper<LaunchConfig, stmt_list_t, data_t>;
//
// Compute the requested kernel dimensions
//
LaunchDims launch_dims = executor_t::calculateDimensions(data);
// Only launch kernel if we have something to iterate over
int num_blocks = launch_dims.num_blocks();
int num_threads = launch_dims.num_threads();
if (num_blocks > 0 || num_threads > 0) {
//
// Setup shared memory buffers
//
int shmem = 0;
cudaStream_t stream = 0;
//
// Compute the recommended physical kernel blocks and threads
//
int recommended_blocks, recommended_threads;
launch_t::recommended_blocks_threads(
shmem, recommended_blocks, recommended_threads);
//
// Compute the MAX physical kernel threads
//
int max_threads;
launch_t::max_threads(shmem, max_threads);
//
// Fit the requested threads
//
cuda_dim_t fit_threads{0,0,0};
if ( recommended_threads >= get_size(launch_dims.min_threads) ) {
fit_threads = fitCudaDims(
recommended_threads, launch_dims.threads, launch_dims.min_threads);
}
//
// Redo fit with max threads
//
if ( recommended_threads < max_threads &&
get_size(fit_threads) != recommended_threads ) {
fit_threads = fitCudaDims(
max_threads, launch_dims.threads, launch_dims.min_threads);
}
launch_dims.threads = fit_threads;
//
// Compute the MAX physical kernel blocks
//
int max_blocks;
launch_t::max_blocks(shmem, max_blocks, launch_dims.num_threads());
int use_blocks;
if ( launch_dims.num_threads() == recommended_threads ) {
//
// Fit the requested blocks
//
use_blocks = recommended_blocks;
} else {
//
// Fit the max blocks
//
use_blocks = max_blocks;
}
launch_dims.blocks = fitCudaDims(
use_blocks, launch_dims.blocks, launch_dims.min_blocks);
//
// make sure that we fit
//
if(launch_dims.num_blocks() > max_blocks){
RAJA_ABORT_OR_THROW("RAJA::kernel exceeds max num blocks");
}
if(launch_dims.num_threads() > max_threads){
RAJA_ABORT_OR_THROW("RAJA::kernel exceeds max num threads");
}
{
//
// Privatize the LoopData, using make_launch_body to setup reductions
//
auto cuda_data = RAJA::cuda::make_launch_body(
launch_dims.blocks, launch_dims.threads, shmem, stream, data);
//
// Launch the kernels
//
launch_t::launch(std::move(cuda_data), launch_dims, shmem, stream);
}
//
// Synchronize
//
if (!launch_t::async) { RAJA::cuda::synchronize(stream); }
}
}
};
} // namespace internal
} // namespace RAJA
#endif // closing endif for RAJA_ENABLE_CUDA guard
#endif // closing endif for header file include guard