XeTLA v0.3.6
IntelĀ® Xe Templates for Linear Algebra - API Definition Document
 
Loading...
Searching...
No Matches
dict.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (c) 2022-2023 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#pragma once
18
20
21namespace gpu::xetla {
22
23// meta_value type
24
25namespace impl {
27 struct _default {};
28};
29
30static constexpr meta_impl_base::_default meta_impl_base_default;
31
32template <typename T, auto value_>
34 using type = T;
35 static constexpr T value = value_;
36};
37
38template <typename T = meta_impl_base::_default>
40 using type = T;
41};
42} // namespace impl
43
44template <auto d_ = impl::meta_impl_base_default, typename T = decltype(d_)>
46
47template <auto d_ = impl::meta_impl_base_default, typename T = decltype(d_)>
49
50template <typename T = impl::meta_impl_base::_default>
52
53template <typename T>
54using meta_type_t = typename T::type;
55
56// shape type
57
58template <auto... dims>
59struct shape {
60 static constexpr size_t size = sizeof...(dims);
61
62 struct impl {
63 template <size_t i_, size_t cur_, auto... dims_>
64 struct dim_impl;
65
66 template <size_t i_, auto dim, auto... dims_>
67 struct dim_impl<i_, i_, dim, dims_...> {
68 static constexpr auto value = dim;
69 };
70
71 template <size_t i_, size_t cur_, auto dim, auto... dims_>
72 struct dim_impl<i_, cur_, dim, dims_...>
73 : dim_impl<i_, cur_ + 1, dims_...> {
74 static_assert(i_ < size, "i_ exceeded shape size");
75 static_assert(cur_ < size, "cur_ exceeded shape size");
76 };
77 };
78
79 template <size_t i>
80 static constexpr auto dim() {
81 return impl::template dim_impl<i, 0, dims...>::value;
82 }
83};
84
85template <auto... dims>
86static inline constexpr auto shape_v = shape<dims...> {};
87
88// dict type
89
90template <auto key_, typename val_>
91struct elem_t {
92 static constexpr auto key = key_;
93 using value = val_;
94};
95
96template <auto key_, typename T>
97struct elem_t_t : elem_t<key_, meta_type<T>> {};
98
99template <auto key_, auto val_, typename T = decltype(val_)>
100struct elem_v_t : elem_t<key_, meta_value<val_, T>> {};
101
102template <typename... Args>
103struct dict_t {
104 static constexpr size_t arg_size = sizeof...(Args);
105
106 struct impl {
107 template <typename T>
109 using type = T;
110 };
111
112 struct empty_dict : type_identity<dict_t<>> {};
113 using this_t = dict_t<Args...>;
114
115 static constexpr int key_not_found = -1;
116
117 template <auto value_, typename type_>
119 static constexpr auto value = value_;
120 using type = type_;
121 };
122
123 template <auto key_, size_t cur_, typename... Elems>
125
126 template <auto key_, size_t cur_, typename elem_, typename... Elems>
127 struct find_elem_impl<key_, cur_, elem_, Elems...> {
128 static constexpr bool match_key = (key_ == elem_::key);
129 using ret = typename std::conditional<match_key,
131 find_elem_impl<key_, cur_ + 1, Elems...>>::type;
132 static constexpr int value = ret::value;
133 using type = typename ret::type;
134 };
135
136 template <auto key_, size_t cur_>
137 struct find_elem_impl<key_, cur_> {
138 static constexpr int value = key_not_found;
139 using type = void;
140 };
141
142 template <auto key_>
143 static constexpr int find_elem_index
144 = find_elem_impl<key_, 0, Args...>::value;
145
146 template <auto key_>
147 struct find_elem : find_elem_impl<key_, 0, Args...>::type {};
148
149 template <typename... Elems>
150 struct prepend_key_impl : type_identity<dict_t<Elems..., Args...>> {};
151
152 template <typename... Elems>
153 struct append_key_impl : type_identity<dict_t<Args..., Elems...>> {};
154
155 template <typename dict_t_>
157
158 template <typename... Elems>
159 struct merge_dict_impl<dict_t<Elems...>> : append_key_impl<Elems...> {};
160
161 template <typename dict_t_, int cur_i, int begin_i, int end_i>
163
164 template <int cur_i, int begin_i, int end_i, typename e_,
165 typename... Elems>
166 struct slicing_key_impl<dict_t<e_, Elems...>, cur_i, begin_i, end_i> {
167 using nxt_dict = typename std::conditional<(cur_i + 1 < end_i)
168 && (sizeof...(Elems) > 0),
169 slicing_key_impl<dict_t<Elems...>, cur_i + 1, begin_i,
170 end_i>,
171 empty_dict>::type::type;
172 using type = typename std::conditional<(begin_i <= cur_i)
173 && (cur_i < end_i),
174 typename nxt_dict::impl::template prepend_key_impl<e_>,
175 type_identity<nxt_dict>>::type::type;
176 };
177
178 template <typename dict_t_, typename e_, int e_index>
180 using pre_dict = typename slicing_key_impl<dict_t_, 0, 0,
181 e_index>::type::impl::template append_key_impl<e_>::type;
182 using post_dict = typename slicing_key_impl<dict_t_, 0, e_index + 1,
183 dict_t_::arg_size>::type;
184 using type = typename pre_dict::impl::template merge_dict_impl<
186 };
187
188 template <typename dict_t_, typename... Elems>
189 struct update_dict_impl : type_identity<dict_t_> {};
190
191 template <typename dict_t_, typename e_, typename... Elems>
192 struct update_dict_impl<dict_t_, e_, Elems...>
193 : update_dict_impl<typename update_dict_impl<dict_t_, e_>::type,
194 Elems...> {};
195
196 template <typename dict_t_, typename e_, typename... Elems>
197 struct update_dict_impl<dict_t_, dict_t<e_, Elems...>>
198 : update_dict_impl<dict_t_, e_, Elems...> {};
199
200 template <typename dict_t_, typename e_>
201 struct update_dict_impl<dict_t_, e_> {
202 static constexpr int e_index
203 = dict_t_::impl::template find_elem_index<e_::key>;
204 using type = typename std::conditional<e_index != key_not_found,
206 typename dict_t_::impl::template append_key_impl<e_>>::
207 type::type;
208 };
209
210 template <typename U, template <typename> typename G>
212 using res_t = typename G<U>::type;
214 };
215 };
216
217 template <auto key_>
218 using find_elem_t = typename impl::template find_elem<key_>::value;
219
220 template <auto key_>
221 static inline constexpr auto find_elem_v = find_elem_t<key_>::value;
222
223 template <typename e_, typename... Elems>
224 using update_t =
225 typename impl::template update_dict_impl<typename impl::this_t, e_,
226 Elems...>::type;
227
228 template <typename T>
230 typename impl::template update_dict_impl<typename impl::this_t,
231 T>::type;
232
233 template <template <typename> typename G>
235 typename impl::template update_generator_impl<typename impl::this_t,
236 G>::type;
237};
238
239} // namespace gpu::xetla
C++ API.
Definition arch_config.hpp:24
typename T::type meta_type_t
Definition dict.hpp:54
typename meta_value< d_, T >::type meta_value_t
Definition dict.hpp:48
typename std::conditional< match_key, find_elem_impl_ret_type< cur_, elem_ >, find_elem_impl< key_, cur_+1, Elems... > >::type ret
Definition dict.hpp:131
static constexpr auto value
Definition dict.hpp:119
typename pre_dict::impl::template merge_dict_impl< post_dict >::type type
Definition dict.hpp:185
typename slicing_key_impl< dict_t_, 0, e_index+1, dict_t_::arg_size >::type post_dict
Definition dict.hpp:183
typename slicing_key_impl< dict_t_, 0, 0, e_index >::type::impl::template append_key_impl< e_ >::type pre_dict
Definition dict.hpp:181
typename std::conditional<(begin_i<=cur_i) &&(cur_i< end_i), typename nxt_dict::impl::template prepend_key_impl< e_ >, type_identity< nxt_dict > >::type::type type
Definition dict.hpp:175
typename std::conditional<(cur_i+1< end_i) &&(sizeof...(Elems) > 0), slicing_key_impl< dict_t< Elems... >, cur_i+1, begin_i, end_i >, empty_dict >::type::type nxt_dict
Definition dict.hpp:171
typename std::conditional< e_index !=key_not_found, replace_key_impl< dict_t_, e_, e_index >, typename dict_t_::impl::template append_key_impl< e_ > >::type::type type
Definition dict.hpp:207
typename update_dict_impl< this_t, res_t >::type type
Definition dict.hpp:213
typename G< U >::type res_t
Definition dict.hpp:212
Definition dict.hpp:106
static constexpr int find_elem_index
Definition dict.hpp:144
static constexpr int key_not_found
Definition dict.hpp:115
dict_t< Args... > this_t
Definition dict.hpp:113
Definition dict.hpp:103
static constexpr auto find_elem_v
Definition dict.hpp:221
typename impl::template update_dict_impl< typename impl::this_t, T >::type update_dict_t
Definition dict.hpp:231
typename impl::template update_generator_impl< typename impl::this_t, G >::type update_generator_t
Definition dict.hpp:236
typename impl::template find_elem< key_ >::value find_elem_t
Definition dict.hpp:218
typename impl::template update_dict_impl< typename impl::this_t, e_, Elems... >::type update_t
Definition dict.hpp:226
static constexpr size_t arg_size
Definition dict.hpp:104
Definition dict.hpp:97
Definition dict.hpp:91
val_ value
Definition dict.hpp:93
static constexpr auto key
Definition dict.hpp:92
Definition dict.hpp:100
Definition dict.hpp:26
Definition dict.hpp:39
T type
Definition dict.hpp:40
Definition dict.hpp:33
static constexpr T value
Definition dict.hpp:35
T type
Definition dict.hpp:34
Definition dict.hpp:64
Definition dict.hpp:62
Definition dict.hpp:59
static constexpr auto dim()
Definition dict.hpp:80
static constexpr size_t size
Definition dict.hpp:60