filter_tuple.hpp Source File

filter_tuple.hpp Source File#

Composable Kernel: filter_tuple.hpp Source File
filter_tuple.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
12
13namespace ck::util {
14
15template <typename Tuple, std::size_t Stride, std::size_t Offset>
17{
18 // Validate Stride and Offset.
19 static_assert(Stride > 0, "Offset must be positive.");
20 static_assert(Offset >= 0 && Offset < Stride,
21 "Offset must be positive and less than the stride.");
22
23 // Generate filtered indices for this stride and offset.
24 static constexpr int new_size = (std::tuple_size_v<Tuple> + Stride - Offset - 1) / Stride;
25
26 template <std::size_t... Is>
27 static constexpr auto to_index(std::index_sequence<Is...>)
28 {
29 return std::index_sequence<(Offset + Is * Stride)...>{};
30 }
31
32 using filtered_indices = decltype(to_index(std::make_index_sequence<new_size>{}));
33
34 // Helper struct to construct the new tuple type from the filtered indices.
35 template <typename T, typename Indices>
37
38 template <typename T, std::size_t... Is>
39 struct make_filtered_tuple_type_impl<T, std::index_sequence<Is...>>
40 {
41 using type = std::tuple<std::tuple_element_t<Is, T>...>;
42 };
43
45};
46
47// Filter a tuple with a stride and offset.
48//
49// Tuple is a std::tuple or equivalent
50// Stride is a positive integer
51// Offset is a positive integer smaller than ofset
52//
53// Evaluates to a smaller tuple type from elements of T with stride M and offset I.
54//
55// Can be used to filter a tuple of types for sharded instantiations.
56template <typename Tuple, std::size_t Stride, std::size_t Offset>
58
59// Example compile-time test:
60// using OriginalTuple =
61// std::tuple<int, double, char, float, long, short, bool, char, long long, unsigned int>;
62// using NewTuple_Every3rdFrom2nd = filter_tuple_by_modulo_t<OriginalTuple, 3, 1>;
63// static_assert(std::is_same_v<NewTuple_Every3rdFrom2nd, std::tuple<double, long, char>>,
64// "Test Case 1 Failed: Every 3rd from 2nd");
65
66} // namespace ck::util
Definition filter_tuple.hpp:13
typename filter_tuple_by_modulo< Tuple, Stride, Offset >::type filter_tuple_by_modulo_t
Definition filter_tuple.hpp:57
STL namespace.
std::tuple< std::tuple_element_t< Is, T >... > type
Definition filter_tuple.hpp:41
Definition filter_tuple.hpp:17
decltype(to_index(std::make_index_sequence< new_size >{})) filtered_indices
Definition filter_tuple.hpp:32
static constexpr auto to_index(std::index_sequence< Is... >)
Definition filter_tuple.hpp:27
typename make_filtered_tuple_type_impl< Tuple, filtered_indices >::type type
Definition filter_tuple.hpp:44
static constexpr int new_size
Definition filter_tuple.hpp:24