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.
print.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/**
26 * \file timemory/operations/types/print.hpp
27 * \brief Definition for various functions for print in operations
28 */
29
30#pragma once
31
37
38#include <cstdint>
39#include <ostream>
40#include <sstream>
41#include <string>
42#include <vector>
43
44namespace tim
45{
46namespace operation
47{
48//
49//--------------------------------------------------------------------------------------//
50//
51///
52/// \struct tim::operation::print
53/// \brief print routines for individual components
54//
55//--------------------------------------------------------------------------------------//
56//
57template <typename Tp>
58struct print
59{
60 using type = Tp;
61 using widths_t = std::vector<int64_t>;
62
63 // only if components are available
64 template <typename Up = Tp, enable_if_t<is_enabled<Up>::value, char> = 0>
65 TIMEMORY_COLD print(const type& _obj, std::ostream& _os, bool _endline = false)
66 {
68 return;
69
71 return;
72
73 std::stringstream ss;
74 ss << _obj;
75 if(_endline)
76 ss << '\n';
77 _os << ss.str();
78 }
79
80 template <typename Up = Tp, enable_if_t<is_enabled<Up>::value, char> = 0>
81 TIMEMORY_COLD print(std::size_t N, std::size_t Ntot, const type& _obj,
82 std::ostream& _os, bool _endline)
83 {
85 return;
86
88 return;
89
90 std::stringstream ss;
91 ss << _obj;
92 if(N + 1 < Ntot)
93 {
94 ss << ", ";
95 }
96 else if(N + 1 == Ntot && _endline)
97 {
98 ss << '\n';
99 }
100 _os << ss.str();
101 }
102
103 template <typename Vp, typename Statp, typename Up = Tp,
105 TIMEMORY_COLD print(const type& _obj, utility::stream& _os, const string_t& _prefix,
106 int64_t _laps, int64_t _depth, const Vp& _self,
107 const Statp& _stats)
108 {
109 auto _labels = common_utils::get_labels(_obj);
110 auto _units = common_utils::get_display_units(_obj);
111
113 return;
114
115 utility::write_entry(_os, "LABEL", _prefix);
116
117 if(_laps > 0)
118 {
120 utility::write_entry(_os, "COUNT", _laps);
122 utility::write_entry(_os, "DEPTH", _depth);
124 utility::write_entry(_os, "METRIC", _labels, true);
126 utility::write_entry(_os, "UNITS", _units, true);
128 utility::write_entry(_os, "SUM", _obj.get());
130 utility::write_entry(_os, "MEAN", _obj.get() / _obj.get_laps());
132 print_statistics<Tp>(_obj, _os, _self, _stats, _laps);
134 utility::write_entry(_os, "% SELF", _self);
135 }
136 else
137 {
138 std::string _filler = "-";
139 std::vector<std::string> _empty_data(_labels.size(), _filler);
141 utility::write_entry(_os, "COUNT", _filler);
143 utility::write_entry(_os, "DEPTH", _depth);
145 utility::write_entry(_os, "METRIC", _empty_data);
147 utility::write_entry(_os, "UNITS", _empty_data);
149 utility::write_entry(_os, "SUM", _empty_data);
151 utility::write_entry(_os, "MEAN", _empty_data);
153 {
155 utility::write_entry(_os, "MIN", _empty_data);
157 utility::write_entry(_os, "MAX", _empty_data);
159 utility::write_entry(_os, "VAR", _empty_data);
161 utility::write_entry(_os, "STDDEV", _empty_data);
162 }
164 utility::write_entry(_os, "% SELF", _empty_data);
165 }
166 }
167
168 //----------------------------------------------------------------------------------//
169 // only if components are available -- pointers
170 //
171 template <typename Up = Tp, enable_if_t<is_enabled<Up>::value, char> = 0>
172 TIMEMORY_COLD print(const type* _obj, std::ostream& _os, bool _endline = false)
173 {
175 return;
176
177 if(_obj && !operation::get_is_invalid<Up, false>{}(*_obj))
178 print(*_obj, _os, _endline);
179 }
180
181 template <typename Up = Tp, enable_if_t<is_enabled<Up>::value, char> = 0>
182 TIMEMORY_COLD print(std::size_t N, std::size_t Ntot, const type* _obj,
183 std::ostream& _os, bool _endline)
184 {
186 return;
187
188 if(_obj && !operation::get_is_invalid<Up, false>{}(*_obj))
189 print(N, Ntot, *_obj, _os, _endline);
190 }
191
192 template <typename Up = Tp, enable_if_t<is_enabled<Up>::value, char> = 0>
193 TIMEMORY_COLD print(const type* _obj, std::ostream& _os, const string_t& _prefix,
194 int64_t _laps, int64_t _depth, const widths_t& _output_widths,
195 bool _endline, const string_t& _suffix = "")
196 {
198 return;
199
200 if(_obj && !operation::get_is_invalid<Up, false>{}(*_obj))
201 print(*_obj, _os, _prefix, _laps, _depth, _output_widths, _endline, _suffix);
202 }
203
204 //----------------------------------------------------------------------------------//
205 // print nothing if component is not available
206 //
207 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
208 print(const type&, std::ostream&, bool = false)
209 {}
210
211 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
212 print(std::size_t, std::size_t, const type&, std::ostream&, bool)
213 {}
214
215 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
216 print(const type&, std::ostream&, const string_t&, int64_t, int64_t, const widths_t&,
217 bool, const string_t& = "")
218 {}
219
220 //----------------------------------------------------------------------------------//
221 // print nothing if component is not available -- pointers
222 //
223 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
224 print(const type*, std::ostream&, bool = false)
225 {}
226
227 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
228 print(std::size_t, std::size_t, const type*, std::ostream&, bool)
229 {}
230
231 template <typename Up = Tp, enable_if_t<!is_enabled<Up>::value, char> = 0>
232 print(const type*, std::ostream&, const string_t&, int64_t, int64_t, const widths_t&,
233 bool, const string_t& = "")
234 {}
235};
236//
237//--------------------------------------------------------------------------------------//
238//
239} // namespace operation
240} // namespace tim
auto write_entry(Args &&... args)
Definition: stream.hpp:996
Definition: kokkosp.cpp:39
char const std::string & _prefix
Definition: config.cpp:55
std::string string_t
Definition: utility.hpp:98
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
char const std::string const std::string & _suffix
Definition: config.cpp:57
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
static strvec_t get_labels(const Tp &_data)
static strvec_t get_display_units(const Tp &_data)
This operation attempts to call a member function which provides whether or not the component is in a...
Definition: types.hpp:673
prints the statistics for a type
print routines for individual components
Definition: print.hpp:59
print(const type &_obj, utility::stream &_os, const string_t &_prefix, int64_t _laps, int64_t _depth, const Vp &_self, const Statp &_stats)
Definition: print.hpp:105
print(const type &, std::ostream &, const string_t &, int64_t, int64_t, const widths_t &, bool, const string_t &="")
Definition: print.hpp:216
std::vector< int64_t > widths_t
Definition: print.hpp:61
print(const type *_obj, std::ostream &_os, bool _endline=false)
Definition: print.hpp:172
print(const type *, std::ostream &, const string_t &, int64_t, int64_t, const widths_t &, bool, const string_t &="")
Definition: print.hpp:232
print(const type &_obj, std::ostream &_os, bool _endline=false)
Definition: print.hpp:65
print(std::size_t, std::size_t, const type &, std::ostream &, bool)
Definition: print.hpp:212
print(std::size_t, std::size_t, const type *, std::ostream &, bool)
Definition: print.hpp:228
print(std::size_t N, std::size_t Ntot, const type *_obj, std::ostream &_os, bool _endline)
Definition: print.hpp:182
print(const type *, std::ostream &, bool=false)
Definition: print.hpp:224
print(const type &, std::ostream &, bool=false)
Definition: print.hpp:208
print(const type *_obj, std::ostream &_os, const string_t &_prefix, int64_t _laps, int64_t _depth, const widths_t &_output_widths, bool _endline, const string_t &_suffix="")
Definition: print.hpp:193
print(std::size_t N, std::size_t Ntot, const type &_obj, std::ostream &_os, bool _endline)
Definition: print.hpp:81
trait that allows runtime configuration of reporting certain types of values. Only applies to text ou...
trait that signifies that an implementation is enabled at runtime. The value returned from get() is f...