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.
types.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
27#include "timemory/api.hpp"
33
34#include <functional>
35#include <memory>
36#include <string>
37#include <type_traits>
38#include <unordered_map>
39#include <utility>
40#include <vector>
41
42namespace tim
43{
44//
45//--------------------------------------------------------------------------------------//
46//
47// GENERAL PURPOSE TLS DATA
48//
49//--------------------------------------------------------------------------------------//
50//
51template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
52 typename PairT = std::pair<PtrT, PtrT>>
53PairT&
55//
56//--------------------------------------------------------------------------------------//
57//
58template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
59 typename PairT = std::pair<PtrT, PtrT>>
60PtrT
62//
63//--------------------------------------------------------------------------------------//
64//
65template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
66 typename PairT = std::pair<PtrT, PtrT>>
67PtrT
69//
70//--------------------------------------------------------------------------------------//
71//
72template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>>
73PtrT
75//
76//--------------------------------------------------------------------------------------//
77//
78// HASH ALIASES
79//
80//--------------------------------------------------------------------------------------//
81//
82inline namespace hash
83{
84using hash_value_t = size_t;
85using hash_map_t = std::unordered_map<hash_value_t, std::string>;
86using hash_alias_map_t = std::unordered_map<hash_value_t, hash_value_t>;
87using hash_map_ptr_t = std::shared_ptr<hash_map_t>;
88using hash_map_ptr_pair_t = std::pair<hash_map_ptr_t, hash_map_ptr_t>;
89using hash_alias_ptr_t = std::shared_ptr<hash_alias_map_t>;
90using hash_resolver_t = std::function<bool(hash_value_t, std::string&)>;
91using hash_resolver_vec_t = std::vector<hash_resolver_t>;
92//
93//--------------------------------------------------------------------------------------//
94//
95// HASH DATA STRUCTURES
96//
97//--------------------------------------------------------------------------------------//
98//
100get_hash_ids() TIMEMORY_HOT;
101//
102//--------------------------------------------------------------------------------------//
103//
105get_hash_aliases() TIMEMORY_HOT;
106//
107//--------------------------------------------------------------------------------------//
108//
109std::shared_ptr<hash_resolver_vec_t>&
110get_hash_resolvers() TIMEMORY_HOT;
111//
112//--------------------------------------------------------------------------------------//
113//
114// STRING -> HASH CONVERSION
115//
116//--------------------------------------------------------------------------------------//
117//
118// declarations
119//
120template <typename Tp,
121 std::enable_if_t<concepts::is_string_type<std::decay_t<Tp>>::value, int> = 0>
122TIMEMORY_INLINE hash_value_t
123get_hash_id(Tp&& _prefix);
124//
125template <typename Tp,
126 std::enable_if_t<!concepts::is_string_type<std::decay_t<Tp>>::value, int> = 0>
127TIMEMORY_INLINE hash_value_t
128get_hash_id(Tp&& _prefix);
129//
130TIMEMORY_INLINE hash_value_t
132//
133template <typename Tp,
134 std::enable_if_t<!std::is_integral<std::decay_t<Tp>>::value, int> = 0>
135TIMEMORY_INLINE hash_value_t
136get_combined_hash_id(hash_value_t _lhs, Tp&& _rhs);
137//
138// get hash of a string type
139//
140template <typename Tp,
141 std::enable_if_t<concepts::is_string_type<std::decay_t<Tp>>::value, int>>
144{
145 return std::hash<string_view_t>{}(std::forward<Tp>(_prefix));
146}
147//
148// get hash of a non-string type
149//
150template <typename Tp,
151 std::enable_if_t<!concepts::is_string_type<std::decay_t<Tp>>::value, int>>
154{
155 return std::hash<std::decay_t<Tp>>{}(std::forward<Tp>(_prefix));
156}
157//
158// combine two existing hashes
159//
162{
163 return (_lhs ^= _rhs + 0x9e3779b9 + (_lhs << 6) + (_lhs >> 2));
164}
165//
166// compute the hash and combine it with an existing hash
167//
168template <typename Tp, std::enable_if_t<!std::is_integral<std::decay_t<Tp>>::value, int>>
171{
172 return get_combined_hash_id(_lhs, get_hash_id(std::forward<Tp>(_rhs)));
173}
174//
175//--------------------------------------------------------------------------------------//
176//
179//
180//--------------------------------------------------------------------------------------//
181//
182/// \fn hash_value_t add_hash_id(hash_map_ptr_t&, string_view_cref_t)
183/// \brief add an string to the given hash-map (if it doesn't already exist) and return
184/// the hash
185///
187add_hash_id(hash_map_ptr_t& _hash_map, string_view_cref_t _prefix) TIMEMORY_HOT;
188//
189inline hash_value_t
191{
193 if(_hash_map && _hash_map->find(_hash_id) == _hash_map->end())
194 {
195 (*_hash_map)[_hash_id] = std::string{ _prefix };
196 }
197 return _hash_id;
198}
199//
200//--------------------------------------------------------------------------------------//
201//
202/// \fn hash_value_t add_hash_id(string_view_cref_t)
203/// \brief add an string to the default hash-map (if it doesn't already exist) and return
204/// the hash
205///
208//
209inline hash_value_t
211{
213}
214//
215//--------------------------------------------------------------------------------------//
216//
217void
220//
221void
223 hash_value_t _alias_hash_id) TIMEMORY_HOT;
224//
225void
227//
228void
231//
232//--------------------------------------------------------------------------------------//
233//
234// HASH -> STRING CONVERSION
235//
236//--------------------------------------------------------------------------------------//
237//
238/// \fn string_view_t get_hash_identifier_fast(hash_value_t)
239/// \brief this does not check other threads or aliases. Only call this function when
240/// you know that the hash exists on the thread and is not an alias
241//
243get_hash_identifier_fast(hash_value_t _hash) TIMEMORY_HOT;
244//
245bool
246get_hash_identifier_fast(hash_value_t _hash, std::string*& _v) TIMEMORY_HOT;
247//
248bool
249get_hash_identifier_fast(hash_value_t _hash, const char*& _v) TIMEMORY_HOT;
250//
251inline string_view_t
253{
254 auto& _hash_ids = get_hash_ids();
255 auto itr = _hash_ids->find(_hash);
256 if(itr != _hash_ids->end())
257 return itr->second;
258 return string_view_t{};
259}
260//
261inline bool
263{
264 auto& _hash_ids = get_hash_ids();
265 auto itr = _hash_ids->find(_hash);
266 if(itr != _hash_ids->end())
267 return (_v = &itr->second, true);
268 return false;
269}
270//
271inline bool
273{
274 auto& _hash_ids = get_hash_ids();
275 auto itr = _hash_ids->find(_hash);
276 if(itr != _hash_ids->end())
277 return (_v = itr->second.c_str(), true);
278 return false;
279}
280//
281//--------------------------------------------------------------------------------------//
282//
283typename hash_map_t::const_iterator
286//
287typename hash_map_t::const_iterator
289//
290bool
293//
294bool
296 hash_value_t _hash_id, const char*& _ret);
297//
298bool
300//
301bool
303//
307//
310//
311//--------------------------------------------------------------------------------------//
312//
313template <typename FuncT>
314size_t
315add_hash_resolver(FuncT&& _func)
316{
317 auto _resolvers = get_hash_resolvers();
318 if(!_resolvers)
319 return 0;
320 _resolvers->emplace_back(std::forward<FuncT>(_func));
321 return _resolvers->size();
322}
323//
324//--------------------------------------------------------------------------------------//
325//
326// HASH STRING DEMANGLING
327//
328//--------------------------------------------------------------------------------------//
329//
331demangle_hash_identifier(std::string, char bdelim = '[', char edelim = ']');
332//
333//--------------------------------------------------------------------------------------//
334//
335template <typename... Args>
336auto
338{
339 return demangle_hash_identifier(get_hash_identifier(std::forward<Args>(_args)...));
340}
341//
342//--------------------------------------------------------------------------------------//
343//
344} // namespace hash
345} // namespace tim
Include the macros for hash.
STL namespace.
const hash_alias_ptr_t hash_value_t hash_value_t _alias_hash_id
Definition: definition.hpp:126
hash_value_t _hash_id
Definition: definition.hpp:114
std::shared_ptr< hash_resolver_vec_t > & get_hash_resolvers()
hash_value_t add_hash_id(hash_map_ptr_t &_hash_map, string_view_cref_t _prefix)
add an string to the given hash-map (if it doesn't already exist) and return the hash
Definition: types.hpp:190
std::shared_ptr< hash_alias_map_t > hash_alias_ptr_t
Definition: types.hpp:89
hash_alias_ptr_t & get_hash_aliases()
bool get_hash_identifier(const hash_map_ptr_t &_hash_map, const hash_alias_ptr_t &_hash_alias, hash_value_t _hash_id, std::string *&_ret)
auto get_demangled_hash_identifier(Args &&... _args)
Definition: types.hpp:337
std::shared_ptr< hash_map_t > hash_map_ptr_t
Definition: types.hpp:87
size_t add_hash_resolver(FuncT &&_func)
Definition: types.hpp:315
const hash_alias_ptr_t hash_value_t std::string *& _ret
Definition: definition.hpp:300
const hash_alias_ptr_t & _hash_alias
Definition: definition.hpp:124
string_view_t get_hash_identifier_fast(hash_value_t _hash)
this does not check other threads or aliases. Only call this function when you know that the hash exi...
Definition: types.hpp:252
std::unordered_map< hash_value_t, hash_value_t > hash_alias_map_t
Definition: types.hpp:86
hash_map_t::const_iterator find_hash_identifier(const hash_map_ptr_t &_hash_map, const hash_alias_ptr_t &_hash_alias, hash_value_t _hash_id)
std::unordered_map< hash_value_t, std::string > hash_map_t
Definition: types.hpp:85
hash_map_ptr_t & get_hash_ids()
hash_value_t get_hash_id(Tp &&_prefix)
Definition: types.hpp:143
hash_identifier_error(_hash_map, _hash_alias, _hash_id)
std::pair< hash_map_ptr_t, hash_map_ptr_t > hash_map_ptr_pair_t
Definition: types.hpp:88
hash_value_t get_combined_hash_id(hash_value_t _lhs, hash_value_t _rhs)
Definition: types.hpp:161
std::string demangle_hash_identifier(std::string, char bdelim='[', char edelim=']')
size_t hash_value_t
Definition: types.hpp:84
std::vector< hash_resolver_t > hash_resolver_vec_t
Definition: types.hpp:91
std::function< bool(hash_value_t, std::string &)> hash_resolver_t
Definition: types.hpp:90
Definition: kokkosp.cpp:39
std::array< char *, 4 > _args
char const std::string & _prefix
Definition: config.cpp:55
std::string string_view_t
Definition: language.hpp:102
const std::string & string_view_cref_t
Definition: language.hpp:103
PtrT get_shared_ptr_pair_main_instance()
Definition: declaration.hpp:66
typename std::decay< T >::type decay_t
Alias template for decay.
Definition: types.hpp:194
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
PtrT get_shared_ptr_pair_instance()
Definition: declaration.hpp:55
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
PairT & get_shared_ptr_pair()
Definition: declaration.hpp:42
PtrT get_shared_ptr_lone_instance()
Definition: declaration.hpp:77
typename typename typename
Definition: types.hpp:226