timemory  3.2.1
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"
28 #include "timemory/hash/macros.hpp"
32 
33 #include <functional>
34 #include <memory>
35 #include <string>
36 #include <type_traits>
37 #include <unordered_map>
38 #include <utility>
39 #include <vector>
40 
41 namespace tim
42 {
43 //
44 //--------------------------------------------------------------------------------------//
45 //
46 // GENERAL PURPOSE TLS DATA
47 //
48 //--------------------------------------------------------------------------------------//
49 //
50 template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
51  typename PairT = std::pair<PtrT, PtrT>>
52 PairT&
54 //
55 //--------------------------------------------------------------------------------------//
56 //
57 template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
58  typename PairT = std::pair<PtrT, PtrT>>
59 PtrT
61 //
62 //--------------------------------------------------------------------------------------//
63 //
64 template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>,
65  typename PairT = std::pair<PtrT, PtrT>>
66 PtrT
68 //
69 //--------------------------------------------------------------------------------------//
70 //
71 template <typename Tp, typename Tag = TIMEMORY_API, typename PtrT = std::shared_ptr<Tp>>
72 PtrT
74 //
75 //--------------------------------------------------------------------------------------//
76 //
77 // HASH ALIASES
78 //
79 //--------------------------------------------------------------------------------------//
80 //
81 using hash_value_t = size_t;
82 using hash_map_t = std::unordered_map<hash_value_t, std::string>;
83 using hash_alias_map_t = std::unordered_map<hash_value_t, hash_value_t>;
84 using hash_map_ptr_t = std::shared_ptr<hash_map_t>;
85 using hash_map_ptr_pair_t = std::pair<hash_map_ptr_t, hash_map_ptr_t>;
86 using hash_alias_ptr_t = std::shared_ptr<hash_alias_map_t>;
87 using hash_resolver_t = std::function<bool(hash_value_t, std::string&)>;
88 using hash_resolver_vec_t = std::vector<hash_resolver_t>;
89 //
90 //--------------------------------------------------------------------------------------//
91 //
92 // HASH DATA STRUCTURES
93 //
94 //--------------------------------------------------------------------------------------//
95 //
97 get_hash_ids() TIMEMORY_HOT;
98 //
99 //--------------------------------------------------------------------------------------//
100 //
102 get_hash_aliases() TIMEMORY_HOT;
103 //
104 //--------------------------------------------------------------------------------------//
105 //
106 std::shared_ptr<hash_resolver_vec_t>&
107 get_hash_resolvers() TIMEMORY_HOT;
108 //
109 //--------------------------------------------------------------------------------------//
110 //
111 // STRING -> HASH CONVERSION
112 //
113 //--------------------------------------------------------------------------------------//
114 //
115 // declarations
116 //
117 template <typename Tp,
118  std::enable_if_t<concepts::is_string_type<std::decay_t<Tp>>::value, int> = 0>
119 TIMEMORY_INLINE hash_value_t
120  get_hash_id(Tp&& _prefix);
121 //
122 template <typename Tp,
123  std::enable_if_t<!concepts::is_string_type<std::decay_t<Tp>>::value, int> = 0>
124 TIMEMORY_INLINE hash_value_t
125  get_hash_id(Tp&& _prefix);
126 //
127 TIMEMORY_INLINE hash_value_t
129 //
130 template <typename Tp,
131  std::enable_if_t<!std::is_integral<std::decay_t<Tp>>::value, int> = 0>
132 TIMEMORY_INLINE hash_value_t
133  get_combined_hash_id(hash_value_t _lhs, Tp&& _rhs);
134 //
135 // get hash of a string type
136 //
137 template <typename Tp,
138  std::enable_if_t<concepts::is_string_type<std::decay_t<Tp>>::value, int>>
141 {
142  return std::hash<string_view_t>{}(std::forward<Tp>(_prefix));
143 }
144 //
145 // get hash of a non-string type
146 //
147 template <typename Tp,
148  std::enable_if_t<!concepts::is_string_type<std::decay_t<Tp>>::value, int>>
150 get_hash_id(Tp&& _prefix)
151 {
152  return std::hash<std::decay_t<Tp>>{}(std::forward<Tp>(_prefix));
153 }
154 //
155 // combine two existing hashes
156 //
159 {
160  return (_lhs ^= _rhs + 0x9e3779b9 + (_lhs << 6) + (_lhs >> 2));
161 }
162 //
163 // compute the hash and combine it with an existing hash
164 //
165 template <typename Tp, std::enable_if_t<!std::is_integral<std::decay_t<Tp>>::value, int>>
168 {
169  return get_combined_hash_id(_lhs, get_hash_id(std::forward<Tp>(_rhs)));
170 }
171 //
172 //--------------------------------------------------------------------------------------//
173 //
176 //
177 //--------------------------------------------------------------------------------------//
178 //
179 /// \fn hash_value_t add_hash_id(hash_map_ptr_t&, const string_view_t&)
180 /// \brief add an string to the given hash-map (if it doesn't already exist) and return
181 /// the hash
182 ///
184 add_hash_id(hash_map_ptr_t& _hash_map, const string_view_t& _prefix) TIMEMORY_HOT;
185 //
186 inline hash_value_t
188 {
190  if(_hash_map && _hash_map->find(_hash_id) == _hash_map->end())
191  {
192  (*_hash_map)[_hash_id] = std::string{ _prefix };
193  }
194  return _hash_id;
195 }
196 //
197 //--------------------------------------------------------------------------------------//
198 //
199 /// \fn hash_value_t add_hash_id(const string_view_t&)
200 /// \brief add an string to the default hash-map (if it doesn't already exist) and return
201 /// the hash
202 ///
204 add_hash_id(const string_view_t& _prefix) TIMEMORY_HOT;
205 //
206 inline hash_value_t
208 {
209  return add_hash_id(get_hash_ids(), _prefix);
210 }
211 //
212 //--------------------------------------------------------------------------------------//
213 //
214 void
216  hash_value_t _hash_id, hash_value_t _alias_hash_id) TIMEMORY_HOT;
217 //
218 //--------------------------------------------------------------------------------------//
219 //
220 void
221 add_hash_id(hash_value_t _hash_id, hash_value_t _alias_hash_id) TIMEMORY_HOT;
222 //
223 //--------------------------------------------------------------------------------------//
224 //
225 // HASH -> STRING CONVERSION
226 //
227 //--------------------------------------------------------------------------------------//
228 //
229 /// \fn string_view_t get_hash_identifier_fast(hash_value_t)
230 /// \brief this does not check other threads or aliases. Only call this function when
231 /// you know that the hash exists on the thread and is not an alias
232 //
234 get_hash_identifier_fast(hash_value_t _hash) TIMEMORY_HOT;
235 //
236 inline string_view_t
238 {
239  auto& _hash_ids = get_hash_ids();
240  auto itr = _hash_ids->find(_hash);
241  if(itr != _hash_ids->end())
242  return itr->second;
243  return "";
244 }
245 //
246 //--------------------------------------------------------------------------------------//
247 //
251 //
252 //--------------------------------------------------------------------------------------//
253 //
256 //
257 //--------------------------------------------------------------------------------------//
258 //
259 template <typename FuncT>
260 size_t
261 add_hash_resolver(FuncT&& _func)
262 {
263  auto _resolvers = get_hash_resolvers();
264  if(!_resolvers)
265  return 0;
266  _resolvers->emplace_back(std::forward<FuncT>(_func));
267  return _resolvers->size();
268 }
269 //
270 //--------------------------------------------------------------------------------------//
271 //
272 // HASH STRING DEMANGLING
273 //
274 //--------------------------------------------------------------------------------------//
275 //
277 demangle_hash_identifier(std::string, char bdelim = '[', char edelim = ']');
278 //
279 //--------------------------------------------------------------------------------------//
280 //
281 template <typename... Args>
282 auto
284 {
285  return demangle_hash_identifier(get_hash_identifier(std::forward<Args>(_args)...));
286 }
287 //
288 //--------------------------------------------------------------------------------------//
289 //
290 } // namespace tim
Include the macros for hash.
Definition: kokkosp.cpp:38
std::vector< hash_resolver_t > hash_resolver_vec_t
Definition: types.hpp:88
size_t hash_value_t
Definition: types.hpp:81
const hash_alias_ptr_t & _hash_alias
Definition: definition.hpp:93
char const std::string & _prefix
Definition: definition.hpp:59
std::string string_view_t
Definition: language.hpp:100
std::shared_ptr< hash_map_t > hash_map_ptr_t
Definition: types.hpp:84
auto get_demangled_hash_identifier(Args &&... _args)
Definition: types.hpp:283
std::string get_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, hash_value_t > hash_alias_map_t
Definition: types.hpp:83
hash_alias_ptr_t & get_hash_aliases()
hash_value_t get_hash_id(Tp &&_prefix)
Definition: types.hpp:140
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
std::shared_ptr< hash_alias_map_t > hash_alias_ptr_t
Definition: types.hpp:86
size_t add_hash_resolver(FuncT &&_func)
Definition: types.hpp:261
PtrT get_shared_ptr_pair_instance()
Definition: declaration.hpp:55
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:237
hash_map_ptr_t & get_hash_ids()
std::string demangle_hash_identifier(std::string, char bdelim='[', char edelim=']')
hash_value_t get_combined_hash_id(hash_value_t _lhs, hash_value_t _rhs)
Definition: types.hpp:158
tim::mpl::apply< std::string > string
Definition: macros.hpp:52
hash_value_t add_hash_id(hash_map_ptr_t &_hash_map, const string_view_t &_prefix)
add an string to the given hash-map (if it doesn't already exist) and return the hash
Definition: types.hpp:187
std::pair< hash_map_ptr_t, hash_map_ptr_t > hash_map_ptr_pair_t
Definition: types.hpp:85
PairT & get_shared_ptr_pair()
Definition: declaration.hpp:42
std::shared_ptr< hash_resolver_vec_t > & get_hash_resolvers()
std::unordered_map< hash_value_t, std::string > hash_map_t
Definition: types.hpp:82
hash_value_t _hash_id
Definition: definition.hpp:83
std::function< bool(hash_value_t, std::string &)> hash_resolver_t
Definition: types.hpp:87
PtrT get_shared_ptr_lone_instance()
Definition: declaration.hpp:77
typename typename typename
Definition: types.hpp:226