/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional2.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional2.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional2.hpp Source File
functional2.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
8 #include "ck/utility/tuple.hpp"
9 #include "ck/utility/type.hpp"
10 
11 namespace ck {
12 
13 namespace detail {
14 
15 template <class>
17 
18 template <index_t... Is>
19 struct static_for_impl<Sequence<Is...>>
20 {
21  template <class F>
22  __host__ __device__ constexpr void operator()(F f) const
23  {
24  swallow{(f(Number<Is>{}), 0)...};
25  }
26 };
27 
28 } // namespace detail
29 
30 // F signature: F(Number<Iter>)
31 template <index_t NBegin, index_t NEnd, index_t Increment>
32 struct static_for
33 {
34  __host__ __device__ constexpr static_for()
35  {
36  static_assert(Increment != 0 && (NEnd - NBegin) % Increment == 0,
37  "Wrong! should satisfy (NEnd - NBegin) % Increment == 0");
38  static_assert((Increment > 0 && NBegin <= NEnd) || (Increment < 0 && NBegin >= NEnd),
39  "wrongs! should (Increment > 0 && NBegin <= NEnd) || (Increment < 0 && "
40  "NBegin >= NEnd)");
41  }
42 
43  template <class F>
44  __host__ __device__ constexpr void operator()(F f) const
45  {
47  f);
48  }
49 };
50 
51 namespace detail {
52 
53 template <typename T, T... Is>
54 struct applier
55 {
56  template <typename F>
57  __host__ __device__ constexpr void operator()(F f) const
58  {
59  // tweak -fbracket-depth if compilation fails. Clang default limit is 256
60  (f(Number<Is>{}), ...);
61  }
62 };
63 
64 template <int32_t Size> // == sizeof...(Is)
65 using make_applier = __make_integer_seq<applier, index_t, Size>;
66 
67 } // namespace detail
68 
69 template <index_t N>
70 struct static_for<0, N, 1> : detail::make_applier<N>
71 {
73 };
74 
75 template <typename... Is>
77 {
78  template <typename F>
79  __host__ __device__ constexpr void operator()(F f) const
80  {
81  // tweak -fbracket-depth if compilation fails. Clang default limit is 256
82  (f(Is{}), ...);
83  }
84 };
85 
86 template <typename... Ts>
88 template <typename... Is>
89 struct static_for_product<Tuple<Is...>> : public static_for_range<Is...>
90 {
91 };
92 template <typename... Is, typename... Rest>
93 struct static_for_product<Tuple<Is...>, Rest...>
94 {
95  template <typename F>
96  __host__ __device__ constexpr void operator()(F f) const
97  {
98  static_for_product<Tuple<Is...>>{}([&](auto i0) { //
99  static_for_product<Rest...>{}([&](auto... is) { //
100  f(i0, is...);
101  });
102  });
103  }
104 };
105 
106 struct identity
107 {
108  template <typename T>
109  __host__ __device__ constexpr T&& operator()(T&& arg) const noexcept
110  {
111  return ck::forward<T>(arg);
112  }
113 };
114 
115 } // namespace ck
__make_integer_seq< applier, index_t, Size > make_applier
Definition: functional2.hpp:65
Definition: ck.hpp:267
int32_t index_t
Definition: ck.hpp:298
Definition: sequence.hpp:43
Definition: tuple.hpp:117
Definition: functional2.hpp:55
__host__ constexpr __device__ void operator()(F f) const
Definition: functional2.hpp:57
__host__ constexpr __device__ void operator()(F f) const
Definition: functional2.hpp:22
Definition: functional2.hpp:16
Definition: functional2.hpp:107
__host__ constexpr __device__ T && operator()(T &&arg) const noexcept
Definition: functional2.hpp:109
Definition: integral_constant.hpp:20
__host__ constexpr __device__ void operator()(F f) const
Definition: functional2.hpp:96
Definition: functional2.hpp:87
Definition: functional2.hpp:77
__host__ constexpr __device__ void operator()(F f) const
Definition: functional2.hpp:79
Definition: functional2.hpp:33
__host__ constexpr __device__ void operator()(F f) const
Definition: functional2.hpp:44
__host__ constexpr __device__ static_for()
Definition: functional2.hpp:34
Definition: functional.hpp:22