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.
declaration.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
30
31#include <algorithm>
32#include <initializer_list>
33#include <sstream>
34#include <string>
35#include <type_traits>
36#include <utility>
37
38namespace tim
39{
40//
41//--------------------------------------------------------------------------------------//
42//
43// plotting
44//
45//--------------------------------------------------------------------------------------//
46//
47namespace plotting
48{
49//
50//--------------------------------------------------------------------------------------//
51//
52void
53plot(const std::string& _label, const std::string& _prefix, const std::string& _dir,
54 bool _echo_dart, const std::string& _json_file) TIMEMORY_VISIBILITY("default");
55//
56void
57echo_dart_file(const string_t& filepath, attributes_t attributes)
58 TIMEMORY_VISIBILITY("default");
59//
60template <typename... Types>
61std::enable_if_t<(sizeof...(Types) > 0), void>
64//
65//--------------------------------------------------------------------------------------//
66//
67namespace operation
68{
69//
70//--------------------------------------------------------------------------------------//
71//
72template <typename Arg, typename... Args>
73auto
74join(const char* sep, Arg&& arg, Args&&... args)
75{
76 std::stringstream ss;
77 ss << std::forward<Arg>(arg);
79 ::std::initializer_list<int>{ (ss << sep << std::forward<Args>(args), 0)... });
80 return ss.str();
81}
82//
83//--------------------------------------------------------------------------------------//
84//
85template <typename Tp>
86void
88{
89 if(std::is_same<typename Tp::value_type, void>::value)
90 {
92 PRINT_HERE("%s", "");
93 return;
94 }
95
97 {
99 PRINT_HERE("%s", "");
100 return;
101 }
102
103 if(_prefix.empty())
104 _prefix = Tp::get_description();
105
106 if(_json_file.empty())
107 _json_file = settings::compose_output_filename(Tp::get_label(), ".json");
108
109 auto _label = demangle<Tp>();
110
112}
113//
114//--------------------------------------------------------------------------------------//
115//
116} // namespace operation
117//
118//--------------------------------------------------------------------------------------//
119//
120namespace impl
121{
122//
123template <typename... Types>
124struct plot
125{
126 static void generate(string_t _prefix, const string_t& _dir, bool _echo,
127 string_t _json)
128 {
129 TIMEMORY_FOLD_EXPRESSION(operation::plot<Types>(_prefix, _dir, _echo, _json));
130 }
131};
132//
133//--------------------------------------------------------------------------------------//
134//
135template <typename... Types>
136struct plot<std::tuple<Types...>> : plot<Types...>
137{};
138//
139//--------------------------------------------------------------------------------------//
140//
141template <typename... Types>
142struct plot<type_list<Types...>> : plot<Types...>
143{};
144//
145//--------------------------------------------------------------------------------------//
146//
147} // namespace impl
148//
149//--------------------------------------------------------------------------------------//
150//
151template <typename... Types>
152std::enable_if_t<(sizeof...(Types) > 0), void>
154 const string_t& _json_file)
155{
156 impl::plot<Types...>::generate(_prefix, _dir, _echo_dart, _json_file);
157}
158//
159//--------------------------------------------------------------------------------------//
160//
161inline void
162echo_dart_file(const string_t& filepath, attributes_t attributes)
163{
164 auto attribute_string = [](const string_t& key, const string_t& item) {
165 return operation::join("", key, '=', "\"", item, "\"");
166 };
167
168 auto lowercase = [](string_t _str) {
169 for(auto& itr : _str)
170 itr = tolower(itr);
171 return _str;
172 };
173
174 auto contains = [&lowercase](const string_t& str, const std::set<string_t>& items) {
175 auto lstr = lowercase(str);
176 return std::any_of(items.begin(), items.end(), [&lstr](const auto& itr) {
177 return lstr.find(itr) != string_t::npos;
178 });
179 };
180
181 auto is_numeric = [](const string_t& str) -> bool {
182 return (str.find_first_not_of("0123456789.e+-*/") == string_t::npos);
183 };
184
185 if(attributes.find("name") == attributes.end())
186 {
187 auto name = filepath;
188 if(name.find('/') != string_t::npos)
189 name = name.substr(name.find_last_of('/') + 1);
190 if(name.find('\\') != string_t::npos)
191 name = name.substr(name.find_last_of('\\') + 1);
192 if(name.find('.') != string_t::npos)
193 name.erase(name.find_last_of('.'));
194 attributes["name"] = name;
195 }
196
197 if(attributes.find("type") == attributes.end())
198 {
199 if(contains(filepath, { ".jpeg", ".jpg" }))
200 {
201 attributes["type"] = "image/jpeg";
202 }
203 else if(contains(filepath, { ".png" }))
204 {
205 attributes["type"] = "image/png";
206 }
207 else if(contains(filepath, { ".tiff", ".tif" }))
208 {
209 attributes["type"] = "image/tiff";
210 }
211 else if(contains(filepath, { ".txt" }))
212 {
213 bool numeric_file = true;
214 std::ifstream ifs;
215 ifs.open(filepath);
216 if(ifs)
217 {
218 while(!ifs.eof())
219 {
221 ifs >> entry;
222 if(ifs.eof())
223 break;
224 if(!is_numeric(entry))
225 {
226 numeric_file = false;
227 break;
228 }
229 }
230 }
231 ifs.close();
232 if(numeric_file)
233 {
234 attributes["type"] = "numeric/double";
235 }
236 else
237 {
238 attributes["type"] = "text/string";
239 }
240 }
241 }
242
243 std::stringstream ss;
244 ss << "<DartMeasurementFile";
245 for(const auto& itr : attributes)
246 ss << " " << attribute_string(itr.first, itr.second);
247 ss << ">" << filepath << "</DartMeasurementFile>";
248 std::cout << ss.str() << std::endl;
249}
250//
251//--------------------------------------------------------------------------------------//
252//
253} // namespace plotting
254} // namespace tim
255//
256//--------------------------------------------------------------------------------------//
257//
STL namespace.
typename std::enable_if< B, T >::type enable_if_t
Definition: types.hpp:58
auto join(const char *sep, Arg &&arg, Args &&... args)
Definition: declaration.hpp:74
void plot(string_t _prefix, const string_t &_dir, bool _echo_dart, string_t _json_file)
Definition: declaration.hpp:87
const string_t const string_t bool const string_t & _json_file
Definition: definition.hpp:54
const string_t const string_t & _dir
Definition: definition.hpp:52
std::string string_t
Definition: types.hpp:46
void echo_dart_file(const string_t &filepath, attributes_t attributes)
const string_t const string_t bool _echo_dart
Definition: definition.hpp:53
void plot(const std::string &_label, const std::string &_prefix, const std::string &_dir, bool _echo_dart, const std::string &_json_file)
std::map< string_t, string_t > attributes_t
Definition: types.hpp:47
const string_t & _prefix
Definition: definition.hpp:52
data::entry entry
Definition: stream.hpp:980
Definition: kokkosp.cpp:39
std::string string_t
Definition: utility.hpp:98
json_output
Definition: settings.cpp:1625
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
dart_output
Definition: settings.cpp:1627
output_path
Definition: settings.cpp:1661
void consume_parameters(ArgsT &&...)
Definition: types.hpp:285
static string_t compose_output_filename(string_t _tag, string_t _ext, bool _use_suffix=use_output_suffix(), int32_t _suffix=default_process_suffix(), bool _make_dir=false, std::string _explicit={})
Definition: settings.cpp:322
trait that designates a type should always print a JSON output
#define PRINT_HERE(...)
Definition: macros.hpp:152
#define TIMEMORY_FOLD_EXPRESSION(...)
Definition: types.hpp:56
type_list
Definition: types.hpp:211