forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_HIP_TeamPolicyInternal.hpp
421 lines (363 loc) · 16 KB
/
Kokkos_HIP_TeamPolicyInternal.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
//@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_HIP_TEAM_POLICY_INTERNAL_HPP
#define KOKKOS_HIP_TEAM_POLICY_INTERNAL_HPP
#include <Kokkos_MinMax.hpp>
namespace Kokkos {
namespace Impl {
template <typename... Properties>
class TeamPolicyInternal<HIP, Properties...>
: public PolicyTraits<Properties...> {
public:
using execution_policy = TeamPolicyInternal;
using traits = PolicyTraits<Properties...>;
template <typename ExecSpace, typename... OtherProperties>
friend class TeamPolicyInternal;
private:
typename traits::execution_space m_space;
int m_league_size;
int m_team_size;
int m_vector_length;
size_t m_team_scratch_size[2];
size_t m_thread_scratch_size[2];
int m_chunk_size;
bool m_tune_team_size;
bool m_tune_vector_length;
public:
using execution_space = HIP;
template <class... OtherProperties>
TeamPolicyInternal(TeamPolicyInternal<OtherProperties...> const& p) {
m_league_size = p.m_league_size;
m_team_size = p.m_team_size;
m_vector_length = p.m_vector_length;
m_team_scratch_size[0] = p.m_team_scratch_size[0];
m_team_scratch_size[1] = p.m_team_scratch_size[1];
m_thread_scratch_size[0] = p.m_thread_scratch_size[0];
m_thread_scratch_size[1] = p.m_thread_scratch_size[1];
m_chunk_size = p.m_chunk_size;
m_space = p.m_space;
m_tune_team_size = p.m_tune_team_size;
m_tune_vector_length = p.m_tune_vector_length;
}
template <typename FunctorType>
int team_size_max(FunctorType const& f, ParallelForTag const&) const {
using closure_type =
Impl::ParallelFor<FunctorType, TeamPolicy<Properties...>>;
return internal_team_size_common<BlockType::Max, closure_type, void>(f);
}
template <class FunctorType>
inline int team_size_max(const FunctorType& f,
const ParallelReduceTag&) const {
using functor_analysis_type =
Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
TeamPolicyInternal, FunctorType, void>;
using closure_type = Impl::ParallelReduce<
CombinedFunctorReducer<FunctorType,
typename functor_analysis_type::Reducer>,
TeamPolicy<Properties...>, Kokkos::HIP>;
return internal_team_size_common<
BlockType::Max, closure_type,
typename functor_analysis_type::value_type>(f);
}
template <typename FunctorType, typename ReducerType>
inline int team_size_max(const FunctorType& f, const ReducerType&,
const ParallelReduceTag&) const {
using closure_type =
Impl::ParallelReduce<CombinedFunctorReducer<FunctorType, ReducerType>,
TeamPolicy<Properties...>, Kokkos::HIP>;
return internal_team_size_common<BlockType::Max, closure_type,
typename ReducerType::value_type>(f);
}
template <typename FunctorType>
int team_size_recommended(FunctorType const& f, ParallelForTag const&) const {
using closure_type =
Impl::ParallelFor<FunctorType, TeamPolicy<Properties...>>;
return internal_team_size_common<BlockType::Preferred, closure_type, void>(
f);
}
template <typename FunctorType>
inline int team_size_recommended(FunctorType const& f,
ParallelReduceTag const&) const {
using functor_analysis_type =
Impl::FunctorAnalysis<Impl::FunctorPatternInterface::REDUCE,
TeamPolicyInternal, FunctorType, void>;
using closure_type = Impl::ParallelReduce<
CombinedFunctorReducer<FunctorType,
typename functor_analysis_type::Reducer>,
TeamPolicy<Properties...>, Kokkos::HIP>;
return internal_team_size_common<
BlockType::Preferred, closure_type,
typename functor_analysis_type::value_type>(f);
}
template <typename FunctorType, typename ReducerType>
int team_size_recommended(FunctorType const& f, ReducerType const&,
ParallelReduceTag const&) const {
using closure_type =
Impl::ParallelReduce<CombinedFunctorReducer<FunctorType, ReducerType>,
TeamPolicy<Properties...>, Kokkos::HIP>;
return internal_team_size_common<BlockType::Preferred, closure_type,
typename ReducerType::value_type>(f);
}
inline bool impl_auto_vector_length() const { return m_tune_vector_length; }
inline bool impl_auto_team_size() const { return m_tune_team_size; }
static int vector_length_max() { return HIPTraits::WarpSize; }
static int verify_requested_vector_length(int requested_vector_length) {
int test_vector_length =
std::min(requested_vector_length, vector_length_max());
// Allow only power-of-two vector_length
if (!(is_integral_power_of_two(test_vector_length))) {
int test_pow2 = 1;
constexpr int warp_size = HIPTraits::WarpSize;
while (test_pow2 < warp_size) {
test_pow2 <<= 1;
if (test_pow2 > test_vector_length) {
break;
}
}
test_vector_length = test_pow2 >> 1;
}
return test_vector_length;
}
inline static int scratch_size_max(int level) {
// HIP Teams use (team_size + 2)*sizeof(double) shared memory for team
// reductions. They also use one int64_t in static shared memory for a
// shared ID. Furthermore, they use additional scratch memory in some
// reduction scenarios, which depend on the size of the value_type and is
// NOT captured here
constexpr size_t max_possible_team_size = 1024;
constexpr size_t max_reserved_shared_mem_per_team =
(max_possible_team_size + 2) * sizeof(double) + sizeof(int64_t);
// arbitrarily setting level 1 scratch limit to 20MB, for a
// MI250 that would give us about 4.4GB for 2 teams per CU
constexpr size_t max_l1_scratch_size = 20 * 1024 * 1024;
size_t max_shmem = HIP().hip_device_prop().sharedMemPerBlock;
return (level == 0 ? max_shmem - max_reserved_shared_mem_per_team
: max_l1_scratch_size);
}
inline void impl_set_vector_length(size_t size) { m_vector_length = size; }
inline void impl_set_team_size(size_t size) { m_team_size = size; }
int impl_vector_length() const { return m_vector_length; }
int team_size() const { return m_team_size; }
int league_size() const { return m_league_size; }
size_t scratch_size(int level, int team_size_ = -1) const {
if (team_size_ < 0) team_size_ = m_team_size;
return m_team_scratch_size[level] +
team_size_ * m_thread_scratch_size[level];
}
size_t team_scratch_size(int level) const {
return m_team_scratch_size[level];
}
size_t thread_scratch_size(int level) const {
return m_thread_scratch_size[level];
}
typename traits::execution_space space() const { return m_space; }
TeamPolicyInternal()
: m_space(typename traits::execution_space()),
m_league_size(0),
m_team_size(-1),
m_vector_length(0),
m_team_scratch_size{0, 0},
m_thread_scratch_size{0, 0},
m_chunk_size(HIPTraits::WarpSize),
m_tune_team_size(false),
m_tune_vector_length(false) {}
/** \brief Specify league size, request team size */
TeamPolicyInternal(const execution_space space_, int league_size_,
int team_size_request, int vector_length_request = 1)
: m_space(space_),
m_league_size(league_size_),
m_team_size(team_size_request),
m_vector_length(
(vector_length_request > 0)
? verify_requested_vector_length(vector_length_request)
: (verify_requested_vector_length(1))),
m_team_scratch_size{0, 0},
m_thread_scratch_size{0, 0},
m_chunk_size(HIPTraits::WarpSize),
m_tune_team_size(bool(team_size_request <= 0)),
m_tune_vector_length(bool(vector_length_request <= 0)) {
// Make sure league size is permissible
if (league_size_ >= static_cast<int>(hip_internal_maximum_grid_count()[0]))
Impl::throw_runtime_exception(
"Requested too large league_size for TeamPolicy on HIP execution "
"space.");
// Make sure total block size is permissible
if (m_team_size * m_vector_length > HIPTraits::MaxThreadsPerBlock) {
Impl::throw_runtime_exception(
std::string("Kokkos::TeamPolicy< HIP > the team size is too large. "
"Team size x vector length must be smaller than 1024."));
}
}
/** \brief Specify league size, request team size */
TeamPolicyInternal(const execution_space space_, int league_size_,
const Kokkos::AUTO_t& /* team_size_request */,
int vector_length_request = 1)
: TeamPolicyInternal(space_, league_size_, -1, vector_length_request) {}
// FLAG
/** \brief Specify league size and team size, request vector length*/
TeamPolicyInternal(const execution_space space_, int league_size_,
int team_size_request,
const Kokkos::AUTO_t& /* vector_length_request */
)
: TeamPolicyInternal(space_, league_size_, team_size_request, -1)
{}
/** \brief Specify league size, request team size and vector length*/
TeamPolicyInternal(const execution_space space_, int league_size_,
const Kokkos::AUTO_t& /* team_size_request */,
const Kokkos::AUTO_t& /* vector_length_request */
)
: TeamPolicyInternal(space_, league_size_, -1, -1)
{}
TeamPolicyInternal(int league_size_, int team_size_request,
int vector_length_request = 1)
: TeamPolicyInternal(typename traits::execution_space(), league_size_,
team_size_request, vector_length_request) {}
TeamPolicyInternal(int league_size_,
const Kokkos::AUTO_t& /* team_size_request */,
int vector_length_request = 1)
: TeamPolicyInternal(typename traits::execution_space(), league_size_, -1,
vector_length_request) {}
/** \brief Specify league size and team size, request vector length*/
TeamPolicyInternal(int league_size_, int team_size_request,
const Kokkos::AUTO_t& /* vector_length_request */
)
: TeamPolicyInternal(typename traits::execution_space(), league_size_,
team_size_request, -1)
{}
/** \brief Specify league size, request team size and vector length*/
TeamPolicyInternal(int league_size_,
const Kokkos::AUTO_t& /* team_size_request */,
const Kokkos::AUTO_t& /* vector_length_request */
)
: TeamPolicyInternal(typename traits::execution_space(), league_size_, -1,
-1) {}
int chunk_size() const { return m_chunk_size; }
TeamPolicyInternal& set_chunk_size(typename traits::index_type chunk_size_) {
m_chunk_size = chunk_size_;
return *this;
}
/** \brief set per team scratch size for a specific level of the scratch
* hierarchy */
TeamPolicyInternal& set_scratch_size(int level,
PerTeamValue const& per_team) {
m_team_scratch_size[level] = per_team.value;
return *this;
}
/** \brief set per thread scratch size for a specific level of the scratch
* hierarchy */
TeamPolicyInternal& set_scratch_size(int level,
PerThreadValue const& per_thread) {
m_thread_scratch_size[level] = per_thread.value;
return *this;
}
/** \brief set per thread and per team scratch size for a specific level of
* the scratch hierarchy */
TeamPolicyInternal& set_scratch_size(int level, PerTeamValue const& per_team,
PerThreadValue const& per_thread) {
m_team_scratch_size[level] = per_team.value;
m_thread_scratch_size[level] = per_thread.value;
return *this;
}
using member_type = Kokkos::Impl::HIPTeamMember;
protected:
template <BlockType BlockSize, class ClosureType, class ValueType,
class FunctorType>
int internal_team_size_common(FunctorType const& f) const {
const unsigned shmem_block = team_scratch_size(0) + 2 * sizeof(double);
unsigned shmem_thread = thread_scratch_size(0) + sizeof(double);
using Tag = typename PatternTagFromImplSpecialization<ClosureType>::type;
if constexpr (std::is_same_v<Tag, ParallelReduceTag>) {
using Interface =
typename Impl::DeduceFunctorPatternInterface<ClosureType>::type;
using Analysis =
Impl::FunctorAnalysis<Interface, typename ClosureType::Policy,
FunctorType, ValueType>;
shmem_thread +=
((Analysis::StaticValueSize != 0) ? 0 : Analysis::value_size(f));
}
const int vector_length = impl_vector_length();
const auto functor = [&f, shmem_block, shmem_thread, vector_length](
const hipFuncAttributes& attr, int block_size) {
int functor_shmem =
::Kokkos::Impl::FunctorTeamShmemSize<FunctorType>::value(
f, block_size / vector_length);
return shmem_block + shmem_thread * (block_size / vector_length) +
functor_shmem + attr.sharedSizeBytes;
};
int block_size;
if constexpr (BlockSize == BlockType::Max) {
block_size = hip_get_max_team_blocksize<ClosureType,
typename traits::launch_bounds>(
space().impl_internal_space_instance(), functor);
} else {
block_size =
hip_get_preferred_team_blocksize<ClosureType,
typename traits::launch_bounds>(
space().impl_internal_space_instance(), functor);
}
if (block_size == 0) {
Kokkos::Impl::throw_runtime_exception(std::string(
"Kokkos::Impl::ParallelFor/Reduce< HIP > could not find a valid "
"team size."));
}
if constexpr (std::is_same_v<Tag, ParallelForTag>) {
return block_size / impl_vector_length();
} else {
// Currently we require Power-of-2 team size for reductions.
int p2 = 1;
while (p2 <= block_size) p2 *= 2;
p2 /= 2;
return p2 / impl_vector_length();
}
}
};
__device__ inline int64_t hip_get_scratch_index(HIP::size_type league_size,
int32_t* scratch_locks,
size_t num_scratch_locks) {
int64_t threadid = 0;
__shared__ int64_t base_thread_id;
if (threadIdx.x == 0 && threadIdx.y == 0) {
int64_t const wraparound_len =
Kokkos::min(int64_t(league_size),
int64_t(num_scratch_locks) / (blockDim.x * blockDim.y));
threadid = (blockIdx.x * blockDim.z + threadIdx.z) % wraparound_len;
threadid *= blockDim.x * blockDim.y;
int done = 0;
while (!done) {
done = (0 == atomicCAS(&scratch_locks[threadid], 0, 1));
if (!done) {
threadid += blockDim.x * blockDim.y;
if (int64_t(threadid + blockDim.x * blockDim.y) >=
wraparound_len * blockDim.x * blockDim.y)
threadid = 0;
}
}
base_thread_id = threadid;
}
__syncthreads();
threadid = base_thread_id;
return threadid;
}
__device__ inline void hip_release_scratch_index(int32_t* scratch_locks,
int64_t threadid) {
__syncthreads();
if (threadIdx.x == 0 && threadIdx.y == 0) {
scratch_locks[threadid] = 0;
}
}
} // namespace Impl
} // namespace Kokkos
#endif