timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
filters.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
31
32#include <tuple>
33#include <type_traits>
34
35namespace tim
36{
37namespace mpl
38{
39namespace impl
40{
41//======================================================================================//
42//
43// get data tuple
44//
45//======================================================================================//
46
47template <typename T>
48struct get_data_tuple_type
49{
50 static_assert(!std::is_fundamental<T>::value,
51 "get_data_tuple_type called for fundamental type");
52 using get_type = decltype(std::declval<T>().get());
53
54 static constexpr bool is_void_v = std::is_void<get_type>::value;
55
56 using type = conditional_t<(is_void_v), type_list<>, type_list<T>>;
57 using value_type = conditional_t<(is_void_v), type_list<>, type_list<get_type>>;
58 using label_type = conditional_t<(is_void_v), type_list<>,
59 type_list<std::tuple<std::string, get_type>>>;
60};
61
62template <>
63struct get_data_tuple_type<std::tuple<>>
64{
65 using type = type_list<>;
66 using value_type = type_list<>;
67 using label_type = type_list<>;
68};
69
70template <>
71struct get_data_tuple_type<type_list<>>
72{
73 using type = type_list<>;
74 using value_type = type_list<>;
75 using label_type = type_list<>;
76};
77
78//--------------------------------------------------------------------------------------//
79
80template <typename... Types>
81struct get_data_tuple
82{
83 using type = convert_t<type_concat_t<typename get_data_tuple_type<Types>::type...>,
84 std::tuple<>>;
85 using value_type =
86 convert_t<type_concat_t<typename get_data_tuple_type<Types>::value_type...>,
87 std::tuple<>>;
88 using label_type =
89 convert_t<type_concat_t<typename get_data_tuple_type<Types>::label_type...>,
90 std::tuple<>>;
91};
92
93template <typename... Types>
94struct get_data_tuple<type_list<Types...>> : public get_data_tuple<Types...>
95{};
96
97template <typename... Types>
98struct get_data_tuple<std::tuple<Types...>> : public get_data_tuple<Types...>
99{};
100
101template <>
102struct get_data_tuple<std::tuple<>>
103{
104 using type = std::tuple<>;
105 using value_type = std::tuple<>;
106 using label_type = std::tuple<>;
107};
108
109template <>
110struct get_data_tuple<type_list<>>
111{
112 using type = std::tuple<>;
113 using value_type = std::tuple<>;
114 using label_type = std::tuple<>;
115};
116
117//======================================================================================//
118//
119// get trait type tuple
120//
121//======================================================================================//
122
123template <template <typename> class TraitT, typename T>
124struct get_trait_type_tuple
125{
126 using trait_type = typename TraitT<T>::type;
127 static constexpr bool is_null_v = concepts::is_null_type<trait_type>::value;
128 using type = conditional_t<(is_null_v), type_list<>, type_list<trait_type>>;
129};
130
131template <template <typename> class TraitT, typename T>
132using get_trait_type_tuple_t = typename get_trait_type_tuple<TraitT, T>::type;
133
134//--------------------------------------------------------------------------------------//
135
136template <template <typename> class TraitT, typename... Types>
137struct get_trait_type
138{
139 using trait_type = convert_t<
140 type_concat_t<typename get_trait_type_tuple<TraitT, Types>::trait_type...>,
141 std::tuple<>>;
142 using type =
143 convert_t<type_concat_t<get_trait_type_tuple_t<TraitT, Types>...>, std::tuple<>>;
144};
145
146template <template <typename> class TraitT, typename... Types>
147struct get_trait_type<TraitT, type_list<Types...>> : get_trait_type<TraitT, Types...>
148{};
149
150template <template <typename> class TraitT, typename... T>
151using get_trait_type_t = typename get_trait_type<TraitT, T...>::type;
152
153//======================================================================================//
154// check if any types are integral types
155//
156template <typename... Tp>
158{
159 static constexpr bool value = false;
160};
161
162template <typename T, template <typename...> class Tuple, typename... Tail>
163struct is_one_of_integral<Tuple<T, Tail...>>
164{
165 static constexpr bool value =
166 std::is_integral<T>::value || is_one_of_integral<Tuple<Tail...>>::value;
167};
168
169//======================================================================================//
170
171template <typename In, typename Out>
172struct remove_duplicates;
173
174template <typename Out>
175struct remove_duplicates<type_list<>, Out>
176{
177 using type = Out;
178};
179
180template <typename In, typename... InTail, typename... Out>
181struct remove_duplicates<type_list<In, InTail...>, type_list<Out...>>
182{
183 using type = conditional_t<
184 !is_one_of<std::remove_pointer_t<In>,
185 type_list<std::remove_pointer_t<Out>...>>::value,
186 typename remove_duplicates<type_list<InTail...>, type_list<Out..., In>>::type,
187 typename remove_duplicates<type_list<InTail...>, type_list<Out...>>::type>;
188};
189
190//--------------------------------------------------------------------------------------//
191
192template <typename In, typename Out>
193struct unique;
194
195template <template <typename...> class InTuple, typename... In,
196 template <typename...> class OutTuple, typename... Out>
197struct unique<InTuple<In...>, OutTuple<Out...>>
198{
199 using tuple_type = convert_t<InTuple<In...>, OutTuple<>>;
200 using dupl_type = typename remove_duplicates<tuple_type, OutTuple<>>::type;
201 using type = convert_t<dupl_type, InTuple<>>;
202};
203
204//======================================================================================//
205
206template <template <typename> class PrioT, typename BegT, typename Tp, typename EndT>
207struct sortT;
208
209//--------------------------------------------------------------------------------------//
210
211template <template <typename> class PrioT, typename Tuple, typename BegT = type_list<>,
212 typename EndT = type_list<>>
213using sort = typename sortT<PrioT, Tuple, BegT, EndT>::type;
214
215//--------------------------------------------------------------------------------------//
216// Initiate recursion (zeroth sort operation)
217//
218template <template <typename> class PrioT, typename In, typename... InT>
219struct sortT<PrioT, type_list<In, InT...>, type_list<>, type_list<>>
220{
221 using type =
222 typename sortT<PrioT, type_list<InT...>, type_list<>, type_list<In>>::type;
223};
224
225//--------------------------------------------------------------------------------------//
226// Initiate recursion (zeroth sort operation)
227//
228template <template <typename> class PrioT, typename In, typename... InT>
229struct sortT<PrioT, type_list<type_list<In, InT...>>, type_list<>, type_list<>>
230{
231 using type =
232 typename sortT<PrioT, type_list<InT...>, type_list<>, type_list<In>>::type;
233};
234
235//--------------------------------------------------------------------------------------//
236// Terminate recursion (last sort operation)
237//
238template <template <typename> class PrioT, typename... BegTT, typename... EndTT>
239struct sortT<PrioT, type_list<>, type_list<BegTT...>, type_list<EndTT...>>
240{
241 using type = type_list<BegTT..., EndTT...>;
242};
243
244//--------------------------------------------------------------------------------------//
245// If no current end, transfer begin to end ()
246//
247template <template <typename> class PrioT, typename In, typename... InT,
248 typename... BegTT>
249struct sortT<PrioT, type_list<In, InT...>, type_list<BegTT...>, type_list<>>
250{
251 using type = typename sortT<PrioT, type_list<In, InT...>, type_list<>,
252 type_list<BegTT...>>::type;
253};
254
255//--------------------------------------------------------------------------------------//
256// Specialization for first sort operation
257//
258template <template <typename> class PrioT, typename In, typename Tp, typename... InT>
259struct sortT<PrioT, type_list<In, InT...>, type_list<>, type_list<Tp>>
260{
261 static constexpr bool value = (PrioT<In>::value < PrioT<Tp>::value);
262
263 using type = conditional_t<
264 (value),
265 typename sortT<PrioT, type_list<InT...>, type_list<>, type_list<In, Tp>>::type,
266 typename sortT<PrioT, type_list<InT...>, type_list<>, type_list<Tp, In>>::type>;
267};
268
269//--------------------------------------------------------------------------------------//
270// Specialization for second sort operation
271//
272template <template <typename> class PrioT, typename In, typename At, typename Bt,
273 typename... BegTT, typename... InT>
274struct sortT<PrioT, type_list<In, InT...>, type_list<BegTT...>, type_list<At, Bt>>
275{
276 static constexpr bool iavalue = (PrioT<In>::value < PrioT<At>::value);
277 static constexpr bool ibvalue = (PrioT<In>::value < PrioT<Bt>::value);
278 static constexpr bool abvalue = (PrioT<At>::value <= PrioT<Bt>::value);
279
280 using type = conditional_t<
281 (iavalue),
282 typename sortT<
283 PrioT, type_list<InT...>, sort<PrioT, type_list<BegTT..., In>>,
284 conditional_t<(abvalue), type_list<At, Bt>, type_list<Bt, At>>>::type,
285 typename sortT<
286 PrioT, type_list<InT...>, sort<PrioT, type_list<BegTT..., At>>,
287 conditional_t<(ibvalue), type_list<In, Bt>, type_list<Bt, In>>>::type>;
288};
289
290//--------------------------------------------------------------------------------------//
291// Specialization for all other sort operations after first and second
292//
293template <template <typename> class PrioT, typename In, typename Tp, typename... InT,
294 typename... BegTT, typename... EndTT>
295struct sortT<PrioT, type_list<In, InT...>, type_list<BegTT...>, type_list<Tp, EndTT...>>
296{
297 static constexpr bool value = (PrioT<In>::value < PrioT<Tp>::value);
298
299 using type =
300 conditional_t<(value),
301 typename sortT<PrioT, type_list<InT...>, type_list<>,
302 type_list<BegTT..., In, Tp, EndTT...>>::type,
303 typename sortT<PrioT, type_list<In, InT...>,
304 type_list<BegTT..., Tp>, type_list<EndTT...>>::type>;
305};
306
307//======================================================================================//
308
309template <typename Tp, typename OpT>
310struct negative_priority;
311
312template <typename Tp, typename OpT>
313struct positive_priority;
314
315template <typename Tp, template <typename> class OpT>
316struct negative_priority<Tp, OpT<Tp>>
317{
318 static constexpr bool value = (OpT<Tp>::value < 0);
319};
320
321template <typename Tp, template <typename> class OpT>
322struct positive_priority<Tp, OpT<Tp>>
323{
324 static constexpr bool value = (OpT<Tp>::value > 0);
325};
326
327template <typename Tp>
328struct negative_start_priority : negative_priority<Tp, trait::start_priority<Tp>>
329{};
330
331template <typename Tp>
332struct positive_start_priority : positive_priority<Tp, trait::start_priority<Tp>>
333{};
334
335template <typename Tp>
336struct negative_stop_priority : negative_priority<Tp, trait::stop_priority<Tp>>
337{};
338
339template <typename Tp>
340struct positive_stop_priority : positive_priority<Tp, trait::stop_priority<Tp>>
341{};
342
343//======================================================================================//
344
345// add a type if not already present
346//
347template <typename Tp, typename... Types>
348struct append_type;
349
350template <typename Tp, template <typename...> class Tuple, typename... Types>
351struct append_type<Tp, Tuple<Types...>>
352{
353 static constexpr auto value = is_one_of<Tp, type_list<Types...>>::value;
354 using type = conditional_t<value, Tuple<Types...>, Tuple<Types..., Tp>>;
355};
356
357// remove a type if not already present
358//
359template <typename Tp, typename... Types>
360struct remove_type;
361
362template <typename Tp, template <typename...> class Tuple, typename... Types>
363struct remove_type<Tp, Tuple<Types...>>
364{
365 static constexpr auto value = is_one_of<Tp, type_list<Types...>>::value;
366 using type = conditional_t<
367 value,
368 convert_t<type_concat_t<std::conditional_t<std::is_same<Tp, Types>::value,
369 type_list<>, type_list<Types>>...>,
370 Tuple<>>,
371 Tuple<Types...>>;
372};
373
374//======================================================================================//
375
376template <typename LhsT, typename RhsT>
377struct subtract;
378
379template <typename... LhsT, typename... RhsT>
380struct subtract<type_list<LhsT...>, type_list<RhsT...>>
381{
382 using type = type_concat_t<conditional_t<is_one_of<LhsT, type_list<RhsT...>>::value,
383 type_list<>, type_list<LhsT>>...>;
384};
385
386//======================================================================================//
387
388} // namespace impl
389
390template <typename T>
392{
393 constexpr size_t operator()()
394 {
395 return (std::is_pointer<T>::value) ? sizeof(std::remove_pointer_t<T>) : 0;
396 }
397};
398
399/// append type to a tuple/bundler
400template <typename Tp, typename... Types>
401using append_type = impl::append_type<Tp, Types...>;
402
403/// remove any instances of a type from a tuple/bundler
404template <typename Tp, typename... Types>
405using remove_type = impl::remove_type<Tp, Types...>;
406
407/// append type to a tuple/bundler
408template <typename Tp, typename Types>
409using append_type_t = typename impl::append_type<Tp, Types>::type;
410
411/// remove any instances of a type from a tuple/bundler
412template <typename Tp, typename Types>
413using remove_type_t = typename impl::remove_type<Tp, Types>::type;
414
415/// check if type is in expansion
416template <typename Types>
417using is_one_of_integral = typename impl::is_one_of_integral<Types>;
418
419template <typename T>
420using remove_duplicates_t = typename impl::unique<T, type_list<>>::type;
421
422template <typename T, typename TupleT = type_list<>>
424
425template <typename LhsT, typename RhsT>
426using subtract_t = typename impl::subtract<LhsT, RhsT>::type;
427
428//======================================================================================//
429//
430// counters
431//
432//======================================================================================//
433
434/// filter out any types that are not available
435template <typename... Types>
436using filter_gotchas = get_false_types_t<trait::is_gotcha, std::tuple<Types...>>;
437
438template <typename T>
440
441template <typename T>
443
444//======================================================================================//
445//
446// {auto,component}_{hybrid,list,tuple} get() and get_labeled() types
447//
448//======================================================================================//
449
450/// get the tuple of types
451template <typename TypeList>
452using get_data_type_t = typename impl::template get_data_tuple<TypeList>::type;
453
454/// get the tuple of values
455template <typename TypeList>
456using get_data_value_t = typename impl::template get_data_tuple<TypeList>::value_type;
457
458/// get the tuple of pair of descriptor and value
459template <typename TypeList>
460using get_data_label_t = typename impl::template get_data_tuple<TypeList>::label_type;
461
462/// get the tuple of types
463template <template <typename> class TraitT, typename TypeList>
464using get_trait_type_t = impl::get_trait_type_t<TraitT, convert_t<TypeList, type_list<>>>;
465
466//======================================================================================//
467//
468// sort
469//
470//======================================================================================//
471
472template <template <typename> class PrioT, typename Tuple, typename BegT = type_list<>,
473 typename EndT = type_list<>>
474using sort =
478 std::tuple<>>;
479
480template <typename... Types>
482 typename get_true_types<concepts::is_runtime_configurable, Types...>::type;
483
484template <typename... Types>
487
488template <typename Tp>
489using negative_start_priority = impl::negative_start_priority<Tp>;
490
491template <typename Tp>
492using positive_start_priority = impl::positive_start_priority<Tp>;
493
494template <typename Tp>
495using negative_stop_priority = impl::negative_stop_priority<Tp>;
496
497template <typename Tp>
498using positive_stop_priority = impl::positive_stop_priority<Tp>;
499
500//--------------------------------------------------------------------------------------//
501} // namespace mpl
502} // namespace tim
STL namespace.
typename impl::remove_type< Tp, Types >::type remove_type_t
remove any instances of a type from a tuple/bundler
Definition: filters.hpp:413
typename get_true_types< concepts::is_external_function_wrapper, Types... >::type external_function_wrappers_t
Definition: filters.hpp:486
convert_t< typename ::tim::mpl::impl::sortT< PrioT, convert_t< Tuple, type_list<> >, convert_t< BegT, type_list<> >, convert_t< EndT, type_list<> > >::type, std::tuple<> > sort
Definition: filters.hpp:478
impl::positive_start_priority< Tp > positive_start_priority
Definition: filters.hpp:492
typename impl::append_type< Tp, Types >::type append_type_t
append type to a tuple/bundler
Definition: filters.hpp:409
typename impl::is_one_of_integral< Types > is_one_of_integral
check if type is in expansion
Definition: filters.hpp:417
typename impl::unique< T, type_list<> >::type remove_duplicates_t
Definition: filters.hpp:420
typename impl::template get_data_tuple< TypeList >::type get_data_type_t
get the tuple of types
Definition: filters.hpp:452
impl::negative_start_priority< Tp > negative_start_priority
Definition: filters.hpp:489
typename get_true_types< Predicate, Sequence... >::type get_true_types_t
Definition: available.hpp:276
typename get_false_types< Predicate, Sequence... >::type get_false_types_t
Definition: available.hpp:301
typename get_true_types< concepts::is_runtime_configurable, Types... >::type runtime_configurable_t
Definition: filters.hpp:482
impl::get_trait_type_t< TraitT, convert_t< TypeList, type_list<> > > get_trait_type_t
get the tuple of types
Definition: filters.hpp:464
impl::remove_type< Tp, Types... > remove_type
remove any instances of a type from a tuple/bundler
Definition: filters.hpp:405
get_false_types_t< trait::is_gotcha, std::tuple< Types... > > filter_gotchas
filter out any types that are not available
Definition: filters.hpp:436
get_true_types_t< concepts::is_empty, T > filter_empty_t
Definition: filters.hpp:442
get_false_types_t< trait::is_gotcha, T > filter_gotchas_t
Definition: filters.hpp:439
impl::positive_stop_priority< Tp > positive_stop_priority
Definition: filters.hpp:498
convert_t< typename impl::unique< T, type_list<> >::type, TupleT > unique_t
Definition: filters.hpp:423
typename impl::template get_data_tuple< TypeList >::label_type get_data_label_t
get the tuple of pair of descriptor and value
Definition: filters.hpp:460
typename impl::template get_data_tuple< TypeList >::value_type get_data_value_t
get the tuple of values
Definition: filters.hpp:456
impl::append_type< Tp, Types... > append_type
append type to a tuple/bundler
Definition: filters.hpp:401
typename impl::subtract< LhsT, RhsT >::type subtract_t
Definition: filters.hpp:426
impl::negative_stop_priority< Tp > negative_stop_priority
Definition: filters.hpp:495
generic alias for extracting all types with a specified trait enabled
Definition: available.hpp:258
Definition: kokkosp.cpp:39
typename impl::type_concat< Ts... >::type type_concat_t
Definition: types.hpp:566
typename impl::is_one_of< Tp, Types > is_one_of
check if type is in expansion
Definition: types.hpp:777
typename std::conditional< B, Lhs, Rhs >::type conditional_t
Definition: types.hpp:197
typename impl::convert< T, U >::type convert_t
Definition: types.hpp:855
lightweight tuple-alternative for meta-programming logic
Definition: types.hpp:233
concept that specifies that a component type wraps external functions
Definition: concepts.hpp:350
static constexpr bool value
Definition: concepts.hpp:159
concept that specifies that a type is used to modify behavior at runtime. For example,...
Definition: concepts.hpp:344
constexpr size_t operator()()
Definition: filters.hpp:393
trait that designates the type is a gotcha
type_list
Definition: types.hpp:211