forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KokkosExp_MDRangePolicy.hpp
382 lines (340 loc) · 14.8 KB
/
KokkosExp_MDRangePolicy.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
//@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_IMPL_PUBLIC_INCLUDE
#include <Kokkos_Macros.hpp>
static_assert(false,
"Including non-public Kokkos header files is not allowed.");
#endif
#ifndef KOKKOS_CORE_EXP_MD_RANGE_POLICY_HPP
#define KOKKOS_CORE_EXP_MD_RANGE_POLICY_HPP
#include <initializer_list>
#include <Kokkos_Layout.hpp>
#include <Kokkos_Rank.hpp>
#include <Kokkos_Array.hpp>
#include <impl/KokkosExp_Host_IterateTile.hpp>
#include <Kokkos_ExecPolicy.hpp>
#include <type_traits>
namespace Kokkos {
// ------------------------------------------------------------------ //
// Moved to Kokkos_Layout.hpp for more general accessibility
/*
enum class Iterate
{
Default, // Default for the device
Left, // Left indices stride fastest
Right, // Right indices stride fastest
};
*/
template <typename ExecSpace>
struct default_outer_direction {
using type = Iterate;
static constexpr Iterate value = Iterate::Right;
};
template <typename ExecSpace>
struct default_inner_direction {
using type = Iterate;
static constexpr Iterate value = Iterate::Right;
};
namespace Impl {
// NOTE the comparison below is encapsulated to silent warnings about pointless
// comparison of unsigned integer with zero
template <class T>
constexpr std::enable_if_t<!std::is_signed<T>::value, bool>
is_less_than_value_initialized_variable(T) {
return false;
}
template <class T>
constexpr std::enable_if_t<std::is_signed<T>::value, bool>
is_less_than_value_initialized_variable(T arg) {
return arg < T{};
}
// Checked narrowing conversion that calls abort if the cast changes the value
template <class To, class From>
constexpr To checked_narrow_cast(From arg, std::size_t idx) {
constexpr const bool is_different_signedness =
(std::is_signed<To>::value != std::is_signed<From>::value);
auto const ret = static_cast<To>(arg);
if (static_cast<From>(ret) != arg ||
(is_different_signedness &&
is_less_than_value_initialized_variable(arg) !=
is_less_than_value_initialized_variable(ret))) {
auto msg =
"Kokkos::MDRangePolicy bound type error: an unsafe implicit conversion "
"is performed on a bound (" +
std::to_string(arg) + ") in dimension (" + std::to_string(idx) +
"), which may not preserve its original value.\n";
Kokkos::abort(msg.c_str());
}
return ret;
}
// NOTE prefer C array U[M] to std::initalizer_list<U> so that the number of
// elements can be deduced (https://stackoverflow.com/q/40241370)
// NOTE for some unfortunate reason the policy bounds are stored as signed
// integer arrays (point_type which is Kokkos::Array<std::int64_t>) so we
// specify the index type (actual policy index_type from the traits) and check
// ahead of time that narrowing conversions will be safe.
template <class IndexType, class Array, class U, std::size_t M>
constexpr Array to_array_potentially_narrowing(const U (&init)[M]) {
using T = typename Array::value_type;
Array a{};
constexpr std::size_t N = a.size();
static_assert(M <= N);
auto* ptr = a.data();
// NOTE equivalent to
// std::transform(std::begin(init), std::end(init), a.data(),
// [](U x) { return static_cast<T>(x); });
// except that std::transform is not constexpr.
for (std::size_t i = 0; i < M; ++i) {
*ptr++ = checked_narrow_cast<T>(init[i], i);
(void)checked_narrow_cast<IndexType>(init[i], i); // see note above
}
return a;
}
// NOTE Making a copy even when std::is_same<Array, Kokkos::Array<U, M>>::value
// is true to reduce code complexity. You may change this if you have a good
// reason to. Intentionally not enabling std::array at this time but this may
// change too.
template <class IndexType, class NVCC_WONT_LET_ME_CALL_YOU_Array, class U,
std::size_t M>
constexpr NVCC_WONT_LET_ME_CALL_YOU_Array to_array_potentially_narrowing(
Kokkos::Array<U, M> const& other) {
using T = typename NVCC_WONT_LET_ME_CALL_YOU_Array::value_type;
NVCC_WONT_LET_ME_CALL_YOU_Array a{};
constexpr std::size_t N = a.size();
static_assert(M <= N);
for (std::size_t i = 0; i < M; ++i) {
a[i] = checked_narrow_cast<T>(other[i], i);
(void)checked_narrow_cast<IndexType>(other[i], i); // see note above
}
return a;
}
struct TileSizeProperties {
int max_threads;
int default_largest_tile_size;
int default_tile_size;
int max_total_tile_size;
};
template <typename ExecutionSpace>
TileSizeProperties get_tile_size_properties(const ExecutionSpace&) {
// Host settings
TileSizeProperties properties;
properties.max_threads = std::numeric_limits<int>::max();
properties.default_largest_tile_size = 0;
properties.default_tile_size = 2;
properties.max_total_tile_size = std::numeric_limits<int>::max();
return properties;
}
} // namespace Impl
// multi-dimensional iteration pattern
template <typename... Properties>
struct MDRangePolicy : public Kokkos::Impl::PolicyTraits<Properties...> {
using traits = Kokkos::Impl::PolicyTraits<Properties...>;
using range_policy = RangePolicy<Properties...>;
typename traits::execution_space m_space;
using impl_range_policy =
RangePolicy<typename traits::execution_space,
typename traits::schedule_type, typename traits::index_type>;
using execution_policy =
MDRangePolicy<Properties...>; // needed for is_execution_space
// interrogation
template <class... OtherProperties>
friend struct MDRangePolicy;
static_assert(!std::is_void<typename traits::iteration_pattern>::value,
"Kokkos Error: MD iteration pattern not defined");
using iteration_pattern = typename traits::iteration_pattern;
using work_tag = typename traits::work_tag;
using launch_bounds = typename traits::launch_bounds;
using member_type = typename range_policy::member_type;
static constexpr int rank = iteration_pattern::rank;
static_assert(rank < 7, "Kokkos MDRangePolicy Error: Unsupported rank...");
using index_type = typename traits::index_type;
using array_index_type = std::int64_t;
using point_type = Kokkos::Array<array_index_type, rank>; // was index_type
using tile_type = Kokkos::Array<array_index_type, rank>;
// If point_type or tile_type is not templated on a signed integral type (if
// it is unsigned), then if user passes in intializer_list of
// runtime-determined values of signed integral type that are not const will
// receive a compiler error due to an invalid case for implicit conversion -
// "conversion from integer or unscoped enumeration type to integer type that
// cannot represent all values of the original, except where source is a
// constant expression whose value can be stored exactly in the target type"
// This would require the user to either pass a matching index_type parameter
// as template parameter to the MDRangePolicy or static_cast the individual
// values
point_type m_lower = {};
point_type m_upper = {};
tile_type m_tile = {};
point_type m_tile_end = {};
index_type m_num_tiles = 1;
index_type m_prod_tile_dims = 1;
bool m_tune_tile_size = false;
static constexpr auto outer_direction =
(iteration_pattern::outer_direction != Iterate::Default)
? iteration_pattern::outer_direction
: default_outer_direction<typename traits::execution_space>::value;
static constexpr auto inner_direction =
iteration_pattern::inner_direction != Iterate::Default
? iteration_pattern::inner_direction
: default_inner_direction<typename traits::execution_space>::value;
static constexpr auto Right = Iterate::Right;
static constexpr auto Left = Iterate::Left;
KOKKOS_INLINE_FUNCTION const typename traits::execution_space& space() const {
return m_space;
}
MDRangePolicy() = default;
template <typename LT, std::size_t LN, typename UT, std::size_t UN,
typename TT = array_index_type, std::size_t TN = rank,
typename = std::enable_if_t<std::is_integral<LT>::value &&
std::is_integral<UT>::value &&
std::is_integral<TT>::value>>
MDRangePolicy(const LT (&lower)[LN], const UT (&upper)[UN],
const TT (&tile)[TN] = {})
: MDRangePolicy(
Impl::to_array_potentially_narrowing<index_type, decltype(m_lower)>(
lower),
Impl::to_array_potentially_narrowing<index_type, decltype(m_upper)>(
upper),
Impl::to_array_potentially_narrowing<index_type, decltype(m_tile)>(
tile)) {
static_assert(
LN == rank && UN == rank && TN <= rank,
"MDRangePolicy: Constructor initializer lists have wrong size");
}
template <typename LT, std::size_t LN, typename UT, std::size_t UN,
typename TT = array_index_type, std::size_t TN = rank,
typename = std::enable_if_t<std::is_integral<LT>::value &&
std::is_integral<UT>::value &&
std::is_integral<TT>::value>>
MDRangePolicy(const typename traits::execution_space& work_space,
const LT (&lower)[LN], const UT (&upper)[UN],
const TT (&tile)[TN] = {})
: MDRangePolicy(
work_space,
Impl::to_array_potentially_narrowing<index_type, decltype(m_lower)>(
lower),
Impl::to_array_potentially_narrowing<index_type, decltype(m_upper)>(
upper),
Impl::to_array_potentially_narrowing<index_type, decltype(m_tile)>(
tile)) {
static_assert(
LN == rank && UN == rank && TN <= rank,
"MDRangePolicy: Constructor initializer lists have wrong size");
}
// NOTE: Keeping these two constructor despite the templated constructors
// from Kokkos arrays for backwards compability to allow construction from
// double-braced initializer lists.
MDRangePolicy(point_type const& lower, point_type const& upper,
tile_type const& tile = tile_type{})
: MDRangePolicy(typename traits::execution_space(), lower, upper, tile) {}
MDRangePolicy(const typename traits::execution_space& work_space,
point_type const& lower, point_type const& upper,
tile_type const& tile = tile_type{})
: m_space(work_space), m_lower(lower), m_upper(upper), m_tile(tile) {
init_helper(Impl::get_tile_size_properties(work_space));
}
template <typename T, std::size_t NT = rank,
typename = std::enable_if_t<std::is_integral<T>::value>>
MDRangePolicy(Kokkos::Array<T, rank> const& lower,
Kokkos::Array<T, rank> const& upper,
Kokkos::Array<T, NT> const& tile = Kokkos::Array<T, NT>{})
: MDRangePolicy(typename traits::execution_space(), lower, upper, tile) {}
template <typename T, std::size_t NT = rank,
typename = std::enable_if_t<std::is_integral<T>::value>>
MDRangePolicy(const typename traits::execution_space& work_space,
Kokkos::Array<T, rank> const& lower,
Kokkos::Array<T, rank> const& upper,
Kokkos::Array<T, NT> const& tile = Kokkos::Array<T, NT>{})
: MDRangePolicy(
work_space,
Impl::to_array_potentially_narrowing<index_type, decltype(m_lower)>(
lower),
Impl::to_array_potentially_narrowing<index_type, decltype(m_upper)>(
upper),
Impl::to_array_potentially_narrowing<index_type, decltype(m_tile)>(
tile)) {}
template <class... OtherProperties>
MDRangePolicy(const MDRangePolicy<OtherProperties...> p)
: traits(p), // base class may contain data such as desired occupancy
m_space(p.m_space),
m_lower(p.m_lower),
m_upper(p.m_upper),
m_tile(p.m_tile),
m_tile_end(p.m_tile_end),
m_num_tiles(p.m_num_tiles),
m_prod_tile_dims(p.m_prod_tile_dims),
m_tune_tile_size(p.m_tune_tile_size) {}
void impl_change_tile_size(const point_type& tile) {
m_tile = tile;
init_helper(Impl::get_tile_size_properties(m_space));
}
bool impl_tune_tile_size() const { return m_tune_tile_size; }
private:
void init_helper(Impl::TileSizeProperties properties) {
m_prod_tile_dims = 1;
int increment = 1;
int rank_start = 0;
int rank_end = rank;
if (inner_direction == Iterate::Right) {
increment = -1;
rank_start = rank - 1;
rank_end = -1;
}
for (int i = rank_start; i != rank_end; i += increment) {
const index_type length = m_upper[i] - m_lower[i];
if (m_upper[i] < m_lower[i]) {
std::string msg =
"Kokkos::MDRangePolicy bounds error: The lower bound (" +
std::to_string(m_lower[i]) + ") is greater than its upper bound (" +
std::to_string(m_upper[i]) + ") in dimension " + std::to_string(i) +
".\n";
#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4)
Kokkos::abort(msg.c_str());
#elif defined(KOKKOS_ENABLE_DEPRECATION_WARNINGS)
Kokkos::Impl::log_warning(msg);
#endif
}
if (m_tile[i] <= 0) {
m_tune_tile_size = true;
if ((inner_direction == Iterate::Right && (i < rank - 1)) ||
(inner_direction == Iterate::Left && (i > 0))) {
if (m_prod_tile_dims * properties.default_tile_size <
static_cast<index_type>(properties.max_total_tile_size)) {
m_tile[i] = properties.default_tile_size;
} else {
m_tile[i] = 1;
}
} else {
m_tile[i] = properties.default_largest_tile_size == 0
? std::max<int>(length, 1)
: properties.default_largest_tile_size;
}
}
m_tile_end[i] =
static_cast<index_type>((length + m_tile[i] - 1) / m_tile[i]);
m_num_tiles *= m_tile_end[i];
m_prod_tile_dims *= m_tile[i];
}
if (m_prod_tile_dims > static_cast<index_type>(properties.max_threads)) {
printf(" Product of tile dimensions exceed maximum limit: %d\n",
static_cast<int>(properties.max_threads));
Kokkos::abort(
"ExecSpace Error: MDRange tile dims exceed maximum number "
"of threads per block - choose smaller tile dims");
}
}
};
} // namespace Kokkos
#endif // KOKKOS_CORE_EXP_MD_RANGE_POLICY_HPP