-
Notifications
You must be signed in to change notification settings - Fork 407
/
Kokkos_Cuda_Parallel_MDRange.hpp
444 lines (395 loc) · 17.8 KB
/
Kokkos_Cuda_Parallel_MDRange.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
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_CUDA_PARALLEL_MD_RANGE_HPP
#define KOKKOS_CUDA_PARALLEL_MD_RANGE_HPP
#include <Kokkos_Macros.hpp>
#if defined(KOKKOS_ENABLE_CUDA)
#include <algorithm>
#include <string>
#include <Kokkos_Parallel.hpp>
#include <Cuda/Kokkos_Cuda_KernelLaunch.hpp>
#include <Cuda/Kokkos_Cuda_ReduceScan.hpp>
#include <Cuda/Kokkos_Cuda_BlockSize_Deduction.hpp>
#include <impl/Kokkos_Tools.hpp>
#include <typeinfo>
#include <KokkosExp_MDRangePolicy.hpp>
#include <impl/KokkosExp_IterateTileGPU.hpp>
namespace Kokkos {
namespace Impl {
template <typename ParallelType, typename Policy, typename LaunchBounds>
int max_tile_size_product_helper(const Policy& pol, const LaunchBounds&) {
cudaFuncAttributes attr =
CudaParallelLaunch<ParallelType,
LaunchBounds>::get_cuda_func_attributes();
auto const& prop = pol.space().cuda_device_prop();
// Limits due to registers/SM, MDRange doesn't have
// shared memory constraints
int const optimal_block_size =
cuda_get_opt_block_size_no_shmem(prop, attr, LaunchBounds{});
// Compute how many blocks of this size we can launch, based on warp
// constraints
int const max_warps_per_sm_registers =
Kokkos::Impl::cuda_max_warps_per_sm_registers(prop, attr);
int const max_num_threads_from_warps =
max_warps_per_sm_registers * prop.warpSize;
int const max_num_blocks = max_num_threads_from_warps / optimal_block_size;
// Compute the total number of threads
int const max_threads_per_sm = optimal_block_size * max_num_blocks;
return std::min(
max_threads_per_sm,
static_cast<int>(Kokkos::Impl::CudaTraits::MaxHierarchicalParallelism));
}
template <class FunctorType, class... Traits>
class ParallelFor<FunctorType, Kokkos::MDRangePolicy<Traits...>, Kokkos::Cuda> {
public:
using Policy = Kokkos::MDRangePolicy<Traits...>;
using functor_type = FunctorType;
private:
using RP = Policy;
using array_index_type = typename Policy::array_index_type;
using index_type = typename Policy::index_type;
using LaunchBounds = typename Policy::launch_bounds;
const FunctorType m_functor;
const Policy m_rp;
public:
template <typename Policy, typename Functor>
static int max_tile_size_product(const Policy& pol, const Functor&) {
return max_tile_size_product_helper<ParallelFor>(pol, LaunchBounds{});
}
Policy const& get_policy() const { return m_rp; }
inline __device__ void operator()() const {
Kokkos::Impl::DeviceIterateTile<Policy::rank, Policy, FunctorType,
typename Policy::work_tag>(m_rp, m_functor)
.exec_range();
}
inline void execute() const {
if (m_rp.m_num_tiles == 0) return;
const auto maxblocks = m_rp.space().cuda_device_prop().maxGridSize;
if (RP::rank == 2) {
const dim3 block(m_rp.m_tile[0], m_rp.m_tile[1], 1);
KOKKOS_ASSERT(block.x > 0);
KOKKOS_ASSERT(block.y > 0);
const dim3 grid(
std::min<array_index_type>(
(m_rp.m_upper[0] - m_rp.m_lower[0] + block.x - 1) / block.x,
maxblocks[0]),
std::min<array_index_type>(
(m_rp.m_upper[1] - m_rp.m_lower[1] + block.y - 1) / block.y,
maxblocks[1]),
1);
CudaParallelLaunch<ParallelFor, LaunchBounds>(
*this, grid, block, 0, m_rp.space().impl_internal_space_instance());
} else if (RP::rank == 3) {
const dim3 block(m_rp.m_tile[0], m_rp.m_tile[1], m_rp.m_tile[2]);
KOKKOS_ASSERT(block.x > 0);
KOKKOS_ASSERT(block.y > 0);
KOKKOS_ASSERT(block.z > 0);
const dim3 grid(
std::min<array_index_type>(
(m_rp.m_upper[0] - m_rp.m_lower[0] + block.x - 1) / block.x,
maxblocks[0]),
std::min<array_index_type>(
(m_rp.m_upper[1] - m_rp.m_lower[1] + block.y - 1) / block.y,
maxblocks[1]),
std::min<array_index_type>(
(m_rp.m_upper[2] - m_rp.m_lower[2] + block.z - 1) / block.z,
maxblocks[2]));
CudaParallelLaunch<ParallelFor, LaunchBounds>(
*this, grid, block, 0, m_rp.space().impl_internal_space_instance());
} else if (RP::rank == 4) {
// id0,id1 encoded within threadIdx.x; id2 to threadIdx.y; id3 to
// threadIdx.z
const dim3 block(m_rp.m_tile[0] * m_rp.m_tile[1], m_rp.m_tile[2],
m_rp.m_tile[3]);
KOKKOS_ASSERT(block.y > 0);
KOKKOS_ASSERT(block.z > 0);
const dim3 grid(
std::min<array_index_type>(m_rp.m_tile_end[0] * m_rp.m_tile_end[1],
maxblocks[0]),
std::min<array_index_type>(
(m_rp.m_upper[2] - m_rp.m_lower[2] + block.y - 1) / block.y,
maxblocks[1]),
std::min<array_index_type>(
(m_rp.m_upper[3] - m_rp.m_lower[3] + block.z - 1) / block.z,
maxblocks[2]));
CudaParallelLaunch<ParallelFor, LaunchBounds>(
*this, grid, block, 0, m_rp.space().impl_internal_space_instance());
} else if (RP::rank == 5) {
// id0,id1 encoded within threadIdx.x; id2,id3 to threadIdx.y; id4 to
// threadIdx.z
const dim3 block(m_rp.m_tile[0] * m_rp.m_tile[1],
m_rp.m_tile[2] * m_rp.m_tile[3], m_rp.m_tile[4]);
KOKKOS_ASSERT(block.z > 0);
const dim3 grid(
std::min<array_index_type>(m_rp.m_tile_end[0] * m_rp.m_tile_end[1],
maxblocks[0]),
std::min<array_index_type>(m_rp.m_tile_end[2] * m_rp.m_tile_end[3],
maxblocks[1]),
std::min<array_index_type>(
(m_rp.m_upper[4] - m_rp.m_lower[4] + block.z - 1) / block.z,
maxblocks[2]));
CudaParallelLaunch<ParallelFor, LaunchBounds>(
*this, grid, block, 0, m_rp.space().impl_internal_space_instance());
} else if (RP::rank == 6) {
// id0,id1 encoded within threadIdx.x; id2,id3 to threadIdx.y; id4,id5 to
// threadIdx.z
const dim3 block(m_rp.m_tile[0] * m_rp.m_tile[1],
m_rp.m_tile[2] * m_rp.m_tile[3],
m_rp.m_tile[4] * m_rp.m_tile[5]);
const dim3 grid(
std::min<array_index_type>(m_rp.m_tile_end[0] * m_rp.m_tile_end[1],
maxblocks[0]),
std::min<array_index_type>(m_rp.m_tile_end[2] * m_rp.m_tile_end[3],
maxblocks[1]),
std::min<array_index_type>(m_rp.m_tile_end[4] * m_rp.m_tile_end[5],
maxblocks[2]));
CudaParallelLaunch<ParallelFor, LaunchBounds>(
*this, grid, block, 0, m_rp.space().impl_internal_space_instance());
} else {
Kokkos::abort("Kokkos::MDRange Error: Exceeded rank bounds with Cuda\n");
}
} // end execute
// inline
ParallelFor(const FunctorType& arg_functor, Policy arg_policy)
: m_functor(arg_functor), m_rp(arg_policy) {}
};
template <class CombinedFunctorReducerType, class... Traits>
class ParallelReduce<CombinedFunctorReducerType,
Kokkos::MDRangePolicy<Traits...>, Kokkos::Cuda> {
public:
using Policy = Kokkos::MDRangePolicy<Traits...>;
using FunctorType = typename CombinedFunctorReducerType::functor_type;
using ReducerType = typename CombinedFunctorReducerType::reducer_type;
private:
using array_index_type = typename Policy::array_index_type;
using index_type = typename Policy::index_type;
using WorkTag = typename Policy::work_tag;
using Member = typename Policy::member_type;
using LaunchBounds = typename Policy::launch_bounds;
public:
using pointer_type = typename ReducerType::pointer_type;
using value_type = typename ReducerType::value_type;
using reference_type = typename ReducerType::reference_type;
using functor_type = FunctorType;
using size_type = Cuda::size_type;
using reducer_type = ReducerType;
// Conditionally set word_size_type to int16_t or int8_t if value_type is
// smaller than int32_t (Kokkos::Cuda::size_type)
// word_size_type is used to determine the word count, shared memory buffer
// size, and global memory buffer size before the reduction is performed.
// Within the reduction, the word count is recomputed based on word_size_type
// and when calculating indexes into the shared/global memory buffers for
// performing the reduction, word_size_type is used again.
// For scalars > 4 bytes in size, indexing into shared/global memory relies
// on the block and grid dimensions to ensure that we index at the correct
// offset rather than at every 4 byte word; such that, when the join is
// performed, we have the correct data that was copied over in chunks of 4
// bytes.
static_assert(sizeof(size_type) == 4);
using word_size_type = std::conditional_t<
sizeof(value_type) < 4,
std::conditional_t<sizeof(value_type) == 2, int16_t, int8_t>, size_type>;
// Algorithmic constraints: blockSize is a power of two AND blockDim.y ==
// blockDim.z == 1
const CombinedFunctorReducerType m_functor_reducer;
const Policy m_policy; // used for workrange and nwork
const pointer_type m_result_ptr;
const bool m_result_ptr_device_accessible;
word_size_type* m_scratch_space;
size_type* m_scratch_flags;
word_size_type* m_unified_space;
using DeviceIteratePattern = typename Kokkos::Impl::Reduce::DeviceIterateTile<
Policy::rank, Policy, FunctorType, typename Policy::work_tag,
reference_type>;
// Shall we use the shfl based reduction or not (only use it for static sized
// types of more than 128bit
static constexpr bool UseShflReduction = false;
//((sizeof(value_type)>2*sizeof(double)) && ReducerType::static_value_size())
// Some crutch to do function overloading
public:
template <typename Policy, typename Functor>
static int max_tile_size_product(const Policy& pol, const Functor&) {
return max_tile_size_product_helper<ParallelReduce>(pol, LaunchBounds{});
}
Policy const& get_policy() const { return m_policy; }
inline __device__ void exec_range(reference_type update) const {
Kokkos::Impl::Reduce::DeviceIterateTile<Policy::rank, Policy, FunctorType,
typename Policy::work_tag,
reference_type>(
m_policy, m_functor_reducer.get_functor(), update)
.exec_range();
}
inline __device__ void operator()() const {
const integral_nonzero_constant<word_size_type,
ReducerType::static_value_size() /
sizeof(word_size_type)>
word_count(m_functor_reducer.get_reducer().value_size() /
sizeof(word_size_type));
{
reference_type value =
m_functor_reducer.get_reducer().init(reinterpret_cast<pointer_type>(
kokkos_impl_cuda_shared_memory<word_size_type>() +
threadIdx.y * word_count.value));
// Number of blocks is bounded so that the reduction can be limited to two
// passes. Each thread block is given an approximately equal amount of
// work to perform. Accumulate the values for this block. The accumulation
// ordering does not match the final pass, but is arithmetically
// equivalent.
this->exec_range(value);
}
// Reduce with final value at blockDim.y - 1 location.
// Problem: non power-of-two blockDim
if (cuda_single_inter_block_reduce_scan<false>(
m_functor_reducer.get_reducer(), blockIdx.x, gridDim.x,
kokkos_impl_cuda_shared_memory<word_size_type>(), m_scratch_space,
m_scratch_flags)) {
// This is the final block with the final result at the final threads'
// location
word_size_type* const shared =
kokkos_impl_cuda_shared_memory<word_size_type>() +
(blockDim.y - 1) * word_count.value;
word_size_type* const global =
m_result_ptr_device_accessible
? reinterpret_cast<word_size_type*>(m_result_ptr)
: (m_unified_space ? m_unified_space : m_scratch_space);
if (threadIdx.y == 0) {
m_functor_reducer.get_reducer().final(
reinterpret_cast<value_type*>(shared));
}
if (CudaTraits::WarpSize < word_count.value) {
__syncthreads();
} else {
// In the above call to final(), shared might have been updated by a
// single thread within a warp without synchronization. Synchronize
// threads within warp to avoid potential race condition.
__syncwarp(0xffffffff);
}
for (unsigned i = threadIdx.y; i < word_count.value; i += blockDim.y) {
global[i] = shared[i];
}
}
}
// Determine block size constrained by shared memory:
inline unsigned local_block_size(const FunctorType& f) {
unsigned n = CudaTraits::WarpSize * 8;
int const maxShmemPerBlock =
m_policy.space().cuda_device_prop().sharedMemPerBlock;
int shmem_size =
cuda_single_inter_block_reduce_scan_shmem<false, WorkTag, value_type>(
f, n);
using closure_type =
Impl::ParallelReduce<CombinedFunctorReducer<FunctorType, ReducerType>,
Policy, Kokkos::Cuda>;
cudaFuncAttributes attr =
CudaParallelLaunch<closure_type,
LaunchBounds>::get_cuda_func_attributes();
while (
(n && (maxShmemPerBlock < shmem_size)) ||
(n >
static_cast<unsigned>(
Kokkos::Impl::cuda_get_max_block_size<FunctorType, LaunchBounds>(
m_policy.space().impl_internal_space_instance(), attr, f, 1,
shmem_size, 0)))) {
n >>= 1;
shmem_size =
cuda_single_inter_block_reduce_scan_shmem<false, WorkTag, value_type>(
f, n);
}
return n;
}
inline void execute() {
const auto nwork = m_policy.m_num_tiles;
if (nwork) {
int block_size = m_policy.m_prod_tile_dims;
// CONSTRAINT: Algorithm requires block_size >= product of tile dimensions
// Nearest power of two
int exponent_pow_two = std::ceil(std::log2(block_size));
block_size = std::pow(2, exponent_pow_two);
int suggested_blocksize =
local_block_size(m_functor_reducer.get_functor());
block_size = (block_size > suggested_blocksize)
? block_size
: suggested_blocksize; // Note: block_size must be less
// than or equal to 512
m_scratch_space =
reinterpret_cast<word_size_type*>(cuda_internal_scratch_space(
m_policy.space(),
m_functor_reducer.get_reducer().value_size() *
block_size /* block_size == max block_count */));
m_scratch_flags =
cuda_internal_scratch_flags(m_policy.space(), sizeof(size_type));
m_unified_space =
reinterpret_cast<word_size_type*>(cuda_internal_scratch_unified(
m_policy.space(), m_functor_reducer.get_reducer().value_size()));
// REQUIRED ( 1 , N , 1 )
const dim3 block(1, block_size, 1);
// Required grid.x <= block.y
const dim3 grid(std::min(int(block.y), int(nwork)), 1, 1);
// TODO @graph We need to effectively insert this in to the graph
const int shmem =
UseShflReduction
? 0
: cuda_single_inter_block_reduce_scan_shmem<false, WorkTag,
value_type>(
m_functor_reducer.get_functor(), block.y);
CudaParallelLaunch<ParallelReduce, LaunchBounds>(
*this, grid, block, shmem,
m_policy.space()
.impl_internal_space_instance()); // copy to device and execute
if (!m_result_ptr_device_accessible) {
if (m_result_ptr) {
if (m_unified_space) {
m_policy.space().fence(
"Kokkos::Impl::ParallelReduce<Cuda, MDRangePolicy>::execute: "
"Result Not Device Accessible");
const int count = m_functor_reducer.get_reducer().value_count();
for (int i = 0; i < count; ++i) {
m_result_ptr[i] = pointer_type(m_unified_space)[i];
}
} else {
const int size = m_functor_reducer.get_reducer().value_size();
DeepCopy<HostSpace, CudaSpace, Cuda>(m_policy.space(), m_result_ptr,
m_scratch_space, size);
}
}
}
} else {
if (m_result_ptr) {
// TODO @graph We need to effectively insert this in to the graph
m_functor_reducer.get_reducer().init(m_result_ptr);
}
}
}
template <class ViewType>
ParallelReduce(const CombinedFunctorReducerType& arg_functor_reducer,
const Policy& arg_policy, const ViewType& arg_result)
: m_functor_reducer(arg_functor_reducer),
m_policy(arg_policy),
m_result_ptr(arg_result.data()),
m_result_ptr_device_accessible(
MemorySpaceAccess<Kokkos::CudaSpace,
typename ViewType::memory_space>::accessible),
m_scratch_space(nullptr),
m_scratch_flags(nullptr),
m_unified_space(nullptr) {
check_reduced_view_shmem_size<WorkTag, value_type>(
m_policy, m_functor_reducer.get_functor());
}
};
} // namespace Impl
} // namespace Kokkos
#endif
#endif