45#ifndef KOKKOS_KOKKOS_TUNERS_HPP
46#define KOKKOS_KOKKOS_TUNERS_HPP
48#include <Kokkos_Macros.hpp>
49#include <Kokkos_Core_fwd.hpp>
50#include <Kokkos_ExecPolicy.hpp>
51#include <KokkosExp_MDRangePolicy.hpp>
52#include <impl/Kokkos_Profiling_Interface.hpp>
68SetOrRange make_candidate_set(
size_t size, int64_t* data);
69bool have_tuning_tool();
70size_t declare_output_type(
const std::string&,
71 Kokkos::Tools::Experimental::VariableInfo);
72void request_output_values(
size_t,
size_t,
73 Kokkos::Tools::Experimental::VariableValue*);
74VariableValue make_variable_value(
size_t, int64_t);
75VariableValue make_variable_value(
size_t,
double);
76SetOrRange make_candidate_range(
double lower,
double upper,
double step,
77 bool openLower,
bool openUpper);
78size_t get_new_context_id();
79void begin_context(
size_t context_id);
80void end_context(
size_t context_id);
88template <
typename ValueType,
typename ContainedType>
89struct ValueHierarchyNode;
91template <
typename ValueType,
typename ContainedType>
93 std::vector<ValueType> root_values;
94 std::vector<ContainedType> sub_values;
95 void add_root_value(
const ValueType& in)
noexcept {
96 root_values.push_back(in);
98 void add_sub_container(
const ContainedType& in) { sub_values.push_back(in); }
99 const ValueType& get_root_value(
const size_t index)
const {
100 return root_values[index];
102 const ContainedType& get_sub_value(
const size_t index)
const {
103 return sub_values[index];
107template <
typename ValueType>
109 std::vector<ValueType> root_values;
111 : root_values(std::move(rv)) {}
112 void add_root_value(
const ValueType& in)
noexcept {
113 root_values.push_back(in);
115 const ValueType& get_root_value(
const size_t index)
const {
116 return root_values[index];
125template <
class NestedMap>
135template <
class K,
class V>
145template <
class NestedMap>
153 static return_type build(
const std::vector<T>& in) {
return return_type{in}; }
158template <
class K,
class V>
159struct ValueHierarchyConstructor<std::map<K, V>> {
160 using return_type =
typename MapTypeConverter<std::map<K, V>>::type;
161 static return_type build(
const std::map<K, V>& in) {
162 return_type node_to_build;
163 for (
auto& entry : in) {
164 node_to_build.add_root_value(entry.first);
165 node_to_build.add_sub_container(
166 ValueHierarchyConstructor<V>::build(entry.second));
168 return node_to_build;
180template <
class InspectForDepth>
186 static constexpr int value = 1;
191template <
class K,
class V>
196template <
class T,
int N>
197struct n_dimensional_sparse_structure;
200struct n_dimensional_sparse_structure<T, 1> {
201 using type = std::vector<T>;
204template <
class T,
int N>
205struct n_dimensional_sparse_structure {
207 std::map<T,
typename n_dimensional_sparse_structure<T, N - 1>::type>;
217template <
class Container>
221template <
class RootType,
class Subtype>
224 double fraction_to_traverse) {
225 size_t index = dimension.root_values.size() * fraction_to_traverse;
226 return dimension.get_root_value(index);
237template <
class HierarchyNode,
class... InterpolationIndices>
240template <
class ValueType>
243 using return_type = std::tuple<ValueType>;
244 static return_type build(
const node_type& in,
double index) {
251template <
class ValueType,
class Subtype,
class... Indices>
252struct GetMultidimensionalPoint<ValueHierarchyNode<ValueType, Subtype>, double,
254 using node_type = ValueHierarchyNode<ValueType, Subtype>;
256 typename GetMultidimensionalPoint<Subtype, Indices...>::return_type;
257 using return_type =
decltype(std::tuple_cat(
258 std::declval<std::tuple<ValueType>>(), std::declval<sub_tuple>()));
259 static return_type build(
const node_type& in,
double fraction_to_traverse,
260 Indices... indices) {
261 size_t index = in.sub_values.size() * fraction_to_traverse;
262 auto dimension_value = std::make_tuple(
263 DimensionValueExtractor<node_type>::get(in, fraction_to_traverse));
264 return std::tuple_cat(dimension_value,
265 GetMultidimensionalPoint<Subtype, Indices...>::build(
266 in.get_sub_value(index), indices...));
270template <
typename PointType,
class ArrayType,
size_t... Is>
271auto get_point_helper(
const PointType& in,
const ArrayType& indices,
272 std::index_sequence<Is...>) {
273 using helper = GetMultidimensionalPoint<
275 decltype(std::get<Is>(std::declval<ArrayType>()).value.double_value)...>;
276 return helper::build(in, std::get<Is>(indices).value.double_value...);
279template <
typename Po
intType,
typename ArrayType>
282template <
typename Po
intType,
size_t X>
283struct GetPoint<PointType,
284 std::array<Kokkos::Tools::Experimental::VariableValue, X>> {
285 using index_set_type =
286 std::array<Kokkos::Tools::Experimental::VariableValue, X>;
287 static auto build(
const PointType& in,
const index_set_type& indices) {
288 return get_point_helper(in, indices, std::make_index_sequence<X>{});
292template <
typename Po
intType,
typename ArrayType>
293auto get_point(
const PointType& point,
const ArrayType& indices) {
294 return GetPoint<PointType, ArrayType>::build(point, indices);
299template <
template <
class...>
class Container,
size_t MaxDimensionSize = 100,
300 class... TemplateArguments>
301class MultidimensionalSparseTuningProblem {
303 using ProblemSpaceInput = Container<TemplateArguments...>;
304 static constexpr int space_dimensionality =
305 Impl::get_space_dimensionality<ProblemSpaceInput>::value;
306 static constexpr size_t max_space_dimension_size = MaxDimensionSize;
307 static constexpr double tuning_min = 0.0;
308 static constexpr double tuning_max = 0.999;
309 static constexpr double tuning_step = tuning_max / max_space_dimension_size;
311 using StoredProblemSpace =
312 typename Impl::MapTypeConverter<ProblemSpaceInput>::type;
313 using HierarchyConstructor =
314 typename Impl::ValueHierarchyConstructor<Container<TemplateArguments...>>;
316 using ValueArray = std::array<Kokkos::Tools::Experimental::VariableValue,
317 space_dimensionality>;
320 StoredProblemSpace m_space;
321 std::array<size_t, space_dimensionality> variable_ids;
325 MultidimensionalSparseTuningProblem() =
default;
326 MultidimensionalSparseTuningProblem(ProblemSpaceInput space,
327 const std::vector<std::string>& names)
328 : m_space(HierarchyConstructor::build(space)) {
329 assert(names.size() == space_dimensionality);
330 for (
unsigned long x = 0; x < names.size(); ++x) {
332 info.type = Kokkos::Tools::Experimental::ValueType::kokkos_value_double;
333 info.category = Kokkos::Tools::Experimental::StatisticalCategory::
334 kokkos_value_interval;
336 Kokkos::Tools::Experimental::CandidateValueType::kokkos_value_range;
337 info.candidates = Kokkos::Tools::Experimental::make_candidate_range(
338 tuning_min, tuning_max, tuning_step,
true,
true);
339 variable_ids[x] = declare_output_type(names[x], info);
344 context = Kokkos::Tools::Experimental::get_new_context_id();
346 for (
int x = 0; x < space_dimensionality; ++x) {
347 values[x] = Kokkos::Tools::Experimental::make_variable_value(
348 variable_ids[x], 0.0);
350 begin_context(context);
351 request_output_values(context, space_dimensionality, values.data());
352 return get_point(m_space, values);
355 auto end() { end_context(context); }
358template <
size_t MaxDimensionSize = 100,
template <
class...>
class Container,
359 class... TemplateArguments>
360auto make_multidimensional_sparse_tuning_problem(
361 const Container<TemplateArguments...>& in, std::vector<std::string> names) {
362 return MultidimensionalSparseTuningProblem<Container, MaxDimensionSize,
363 TemplateArguments...>(in, names);
367 using SpaceDescription = std::map<int64_t, std::vector<int64_t>>;
368 using TunerType =
decltype(make_multidimensional_sparse_tuning_problem<20>(
369 std::declval<SpaceDescription>(),
370 std::declval<std::vector<std::string>>()));
374 TeamSizeTuner() =
default;
375 TeamSizeTuner& operator=(
const TeamSizeTuner& other) =
default;
376 TeamSizeTuner(
const TeamSizeTuner& other) =
default;
377 TeamSizeTuner& operator=(TeamSizeTuner&& other) =
default;
378 TeamSizeTuner(TeamSizeTuner&& other) =
default;
379 template <
typename ViableConfigurationCalculator,
typename Functor,
380 typename TagType,
typename... Properties>
381 TeamSizeTuner(
const std::string& name,
383 const Functor& functor,
const TagType& tag,
384 ViableConfigurationCalculator calc) {
386 auto initial_vector_length = policy.impl_vector_length();
387 if (initial_vector_length < 1) {
388 policy.impl_set_vector_length(1);
415 SpaceDescription space_description;
417 auto max_vector_length = PolicyType::vector_length_max();
418 std::vector<int64_t> allowed_vector_lengths;
420 if (policy.impl_auto_vector_length()) {
421 for (
int vector_length = max_vector_length; vector_length >= 1;
422 vector_length /= 2) {
423 policy.impl_set_vector_length(vector_length);
436 auto max_team_size = calc.get_max_team_size(policy, functor, tag);
437 if ((policy.impl_auto_team_size()) ||
438 (policy.team_size() <= max_team_size)) {
439 allowed_vector_lengths.push_back(vector_length);
443 allowed_vector_lengths.push_back(policy.impl_vector_length());
446 for (
const auto vector_length : allowed_vector_lengths) {
447 std::vector<int64_t> allowed_team_sizes;
448 policy.impl_set_vector_length(vector_length);
449 auto max_team_size = calc.get_max_team_size(policy, functor, tag);
450 if (policy.impl_auto_team_size()) {
452 for (
int team_size = max_team_size; team_size >= 1; team_size /= 2) {
453 allowed_team_sizes.push_back(team_size);
456 allowed_team_sizes.push_back(policy.team_size());
458 space_description[vector_length] = allowed_team_sizes;
460 tuner = make_multidimensional_sparse_tuning_problem<20>(
461 space_description, {std::string(name +
"_vector_length"),
462 std::string(name +
"_team_size")});
463 policy.impl_set_vector_length(initial_vector_length);
466 template <
typename... Properties>
468 if (Kokkos::Tools::Experimental::have_tuning_tool()) {
469 auto configuration = tuner.begin();
470 auto team_size = std::get<1>(configuration);
471 auto vector_length = std::get<0>(configuration);
472 if (vector_length > 0) {
473 policy.impl_set_team_size(team_size);
474 policy.impl_set_vector_length(vector_length);
479 if (Kokkos::Tools::Experimental::have_tuning_tool()) {
490void fill_tile(std::vector<T>& cont,
int tile_size) {
491 for (
int x = 1; x < tile_size; x *= 2) {
495template <
typename T,
typename Mapped>
496void fill_tile(std::map<T, Mapped>& cont,
int tile_size) {
497 for (
int x = 1; x < tile_size; x *= 2) {
498 fill_tile(cont[x], tile_size / x);
503template <
int MDRangeRank>
506 static constexpr int rank = MDRangeRank;
507 static constexpr int max_slices = 15;
508 using SpaceDescription =
509 typename Impl::n_dimensional_sparse_structure<int, rank>::type;
511 decltype(make_multidimensional_sparse_tuning_problem<max_slices>(
512 std::declval<SpaceDescription>(),
513 std::declval<std::vector<std::string>>()));
517 MDRangeTuner() =
default;
518 template <
typename Functor,
typename TagType,
typename Calculator,
519 typename... Properties>
520 MDRangeTuner(
const std::string& name,
521 const Kokkos::MDRangePolicy<Properties...>& policy,
522 const Functor& functor,
const TagType& tag, Calculator calc) {
523 SpaceDescription desc;
525 calc.get_mdrange_max_tile_size_product(policy, functor, tag);
526 Impl::fill_tile(desc, max_tile_size);
527 std::vector<std::string> feature_names;
528 for (
int x = 0; x < rank; ++x) {
529 feature_names.push_back(name +
"_tile_size_" + std::to_string(x));
531 tuner = make_multidimensional_sparse_tuning_problem<max_slices>(
532 desc, feature_names);
534 template <
typename Policy,
typename Tuple,
size_t... Indices>
535 void set_policy_tile(Policy& policy,
const Tuple& tuple,
536 const std::index_sequence<Indices...>&) {
537 policy.impl_change_tile_size({std::get<Indices>(tuple)...});
539 template <
typename... Properties>
540 void tune(Kokkos::MDRangePolicy<Properties...>& policy) {
541 if (Kokkos::Tools::Experimental::have_tuning_tool()) {
542 auto configuration = tuner.begin();
543 set_policy_tile(policy, configuration, std::make_index_sequence<rank>{});
547 if (Kokkos::Tools::Experimental::have_tuning_tool()) {
Execution policy for parallel work over a league of teams of threads.