Skip to content

Commit

Permalink
get rid of homegrown std::index_sequence (#3389)
Browse files Browse the repository at this point in the history
Co-authored-by: BrianHarrisonAMD <[email protected]>
  • Loading branch information
CAHEK7 and BrianHarrisonAMD authored Dec 5, 2024
1 parent cd7b6af commit 8e3a3e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 50 deletions.
40 changes: 5 additions & 35 deletions src/include/miopen/each_args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,25 @@
namespace miopen {
namespace detail {

template <std::size_t...>
struct seq
{
using type = seq;
};

template <class, class>
struct merge_seq;

template <std::size_t... Xs, std::size_t... Ys>
struct merge_seq<seq<Xs...>, seq<Ys...>> : seq<Xs..., (sizeof...(Xs) + Ys)...>
{
};

template <std::size_t N>
struct gens : merge_seq<typename gens<N / 2>::type, typename gens<N - N / 2>::type>
{
};

template <>
struct gens<0> : seq<>
{
};
template <>
struct gens<1> : seq<0>
{
};

template <class F, std::size_t... Ns, class... Ts>
void each_args_i_impl(F f, seq<Ns...>, Ts&&... xs)
void each_args_i_impl(F f, std::index_sequence<Ns...>, Ts&&... xs)
{
(void)std::initializer_list<int>{
(f(std::integral_constant<std::size_t, Ns>{}, std::forward<Ts>(xs)), 0)...};
}

template <class F, std::size_t... Ns, class T>
auto unpack_impl(F f, seq<Ns...>, T&& x)
auto unpack_impl(F f, std::index_sequence<Ns...>, T&& x)
{
using std::get;
return f(get<Ns>(x)...);
return f(std::get<Ns>(x)...);
}

} // namespace detail

template <class F, class... Ts>
void each_args_i(F f, Ts&&... xs)
{
detail::each_args_i_impl(
f, typename detail::gens<sizeof...(Ts)>::type{}, std::forward<Ts>(xs)...);
detail::each_args_i_impl(f, std::make_index_sequence<sizeof...(Ts)>(), std::forward<Ts>(xs)...);
}

template <class F, class... Ts>
Expand All @@ -101,7 +71,7 @@ auto unpack(F f, T&& x)
{
using type = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return detail::unpack_impl(
f, typename detail::gens<std::tuple_size<type>::value>::type{}, std::forward<T>(x));
f, std::make_index_sequence<std::tuple_size<type>::value>(), std::forward<T>(x));
}

} // namespace miopen
Expand Down
6 changes: 3 additions & 3 deletions src/include/miopen/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace miopen {
namespace detail {

template <class F, std::size_t... Ns>
auto each_i_impl(F f, seq<Ns...>) MIOPEN_RETURNS(f(std::integral_constant<std::size_t, Ns>{}...));
auto each_i_impl(F f, std::index_sequence<Ns...>)
MIOPEN_RETURNS(f(std::integral_constant<std::size_t, Ns>{}...));
} // namespace detail

template <class F, class P>
Expand Down Expand Up @@ -87,8 +88,7 @@ struct sequence_t
F f;
template <class IntegralConstant>
auto operator()(IntegralConstant) const
MIOPEN_RETURNS(detail::each_i_impl(f,
typename detail::gens<IntegralConstant::value>::type{}));
MIOPEN_RETURNS(detail::each_i_impl(f, std::make_index_sequence<IntegralConstant::value>()));
};

template <class F>
Expand Down
27 changes: 19 additions & 8 deletions src/include/miopen/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,41 @@
#include <numeric>
#include <vector>
#include <optional>
#include <utility>

namespace miopen {

template <class T, std::size_t... Ns>
auto tie_impl(T&& x, detail::seq<Ns...>) -> decltype(std::tie(x[Ns]...))
constexpr auto tie_array_impl(T&& x, std::index_sequence<Ns...>)
MIOPEN_RETURNS(std::array{x[Ns]...});

template <std::size_t N, class T>
constexpr auto tien_array(T&& x)
MIOPEN_RETURNS(tie_array_impl(std::forward<T>(x), std::make_index_sequence<N>()));

template <class T, std::size_t... Ns>
auto tie_impl(T&& x, std::index_sequence<Ns...>) -> decltype(std::tie(x[Ns]...))
{
assert(x.size() >= sizeof...(Ns));
return std::tie(x[Ns]...);
}

template <class T, class U, std::size_t... Ns>
auto tie_impl(T&& x, U y, detail::seq<Ns...>) -> decltype(std::make_tuple(x[Ns]...))
auto tie_impl(T&& x, U y, std::index_sequence<Ns...>) -> decltype(std::make_tuple(x[Ns]...))
{
return std::make_tuple((Ns < x.size() ? x[Ns] : y)...);
}

template <std::size_t N, class T>
auto tien(T&& x) MIOPEN_RETURNS(tie_impl(std::forward<T>(x), typename detail::gens<N>::type{}));
constexpr auto tien(T&& x)
MIOPEN_RETURNS(tie_impl(std::forward<T>(x), std::make_index_sequence<N>()));

template <std::size_t N, class T, class U>
auto tien(T&& x, U y)
MIOPEN_RETURNS(tie_impl(std::forward<T>(x), y, typename detail::gens<N>::type{}));
MIOPEN_RETURNS(tie_impl(std::forward<T>(x), y, std::make_index_sequence<N>()));

template <class T, std::size_t... Ns>
auto tie_pick_impl(T&& x, detail::seq<Ns...>)
auto tie_pick_impl(T&& x, std::index_sequence<Ns...>)
{
#ifndef NDEBUG
each_args([&](auto i) { assert(i < x.size()); }, Ns...);
Expand All @@ -78,19 +88,20 @@ template <std::size_t... Ns>
struct tie_pick
{
template <class T>
auto operator()(T&& x) MIOPEN_RETURNS(tie_pick_impl(std::forward<T>(x), detail::seq<Ns...>{}))
auto operator()(T&& x)
MIOPEN_RETURNS(tie_pick_impl(std::forward<T>(x), std::index_sequence<Ns...>{}))
};

template <typename F, std::size_t... Ns>
auto create_tuple_impl(F f, detail::seq<Ns...>)
auto create_tuple_impl(F f, std::index_sequence<Ns...>)
{
return std::make_tuple(std::forward<decltype(f(Ns))>(f(Ns))...);
}

template <std::size_t N, typename F>
auto create_tuple(F f)
{
return create_tuple_impl(f, typename detail::gens<N>::type{});
return create_tuple_impl(f, std::make_index_sequence<N>());
}

inline std::size_t GetTypeSize(miopenDataType_t d)
Expand Down
7 changes: 3 additions & 4 deletions test/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <functional>

#include <miopen/logger.hpp>
#include <miopen/each_args.hpp>
#include <numeric>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -183,22 +182,22 @@ struct any_value
};

template <class T, std::size_t... Ns, class Data>
auto any_construct_impl(miopen::rank<1>, miopen::detail::seq<Ns...>, const Data& d)
auto any_construct_impl(miopen::rank<1>, std::index_sequence<Ns...>, const Data& d)
-> decltype(T(any_value{d[Ns]}...))
{
return T(any_value{d[Ns]}...);
}

template <class T, std::size_t... Ns, class Data>
T any_construct_impl(miopen::rank<0>, miopen::detail::seq<Ns...>, const Data&)
T any_construct_impl(miopen::rank<0>, std::index_sequence<Ns...>, const Data&)
{
throw std::runtime_error("Cannot construct: " + miopen::get_type_name<T>());
}

template <class T, std::size_t N, class Data>
T any_construct(const Data& d)
{
return any_construct_impl<T>(miopen::rank<1>{}, typename miopen::detail::gens<N>::type{}, d);
return any_construct_impl<T>(miopen::rank<1>{}, std::make_index_sequence<N>(), d);
}

struct write_value
Expand Down

0 comments on commit 8e3a3e9

Please sign in to comment.