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.
vsettings.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/backends/process.hpp"
29#include "timemory/backends/threading.hpp"
33
34#include <cstdint>
35#include <iomanip>
36#include <map>
37#include <memory>
38#include <sstream>
39#include <string>
40#include <typeindex>
41#include <typeinfo>
42#include <utility>
43#include <vector>
44
45namespace tim
46{
47//
48namespace argparse
49{
50struct argument_parser;
51}
52//
53/// \struct tim::vsettings
54/// \brief Virtual base class for storing settings
56{
58 using parser_func_t = std::function<void(parser_t&)>;
59 using display_map_t = std::map<std::string, std::string>;
60
61 struct noparse
62 {};
63
64 vsettings(std::string _name = "", std::string _env_name = "",
65 std::string _descript = "", std::vector<std::string> _cmdline = {},
66 int32_t _count = -1, int32_t _max_count = -1,
67 std::vector<std::string> _choices = {});
68
69 virtual ~vsettings() = default;
70
71 vsettings(const vsettings&) = default;
72 vsettings(vsettings&&) = default;
73
74 vsettings& operator=(const vsettings&) = default;
76
77 virtual std::string as_string() const = 0;
78 virtual void reset() = 0;
79 virtual void parse() = 0;
80 virtual void parse(const std::string&) = 0;
82 virtual std::shared_ptr<vsettings> clone() = 0;
83 virtual void clone(std::shared_ptr<vsettings> rhs);
84
85 virtual display_map_t get_display(std::ios::fmtflags fmt = {}, int _w = -1,
86 int _p = -1);
87
88 const auto& get_name() const { return m_name; }
89 const auto& get_env_name() const { return m_env_name; }
90 const auto& get_description() const { return m_description; }
91 const auto& get_command_line() const { return m_cmdline; }
92 const auto& get_choices() const { return m_choices; }
93 const auto& get_count() const { return m_count; }
94 const auto& get_max_count() const { return m_max_count; }
95
96 void set_count(int32_t v) { m_count = v; }
97 void set_max_count(int32_t v) { m_max_count = v; }
98 void set_choices(const std::vector<std::string>& v) { m_choices = v; }
99 void set_command_line(const std::vector<std::string>& v) { m_cmdline = v; }
100
101 auto get_type_index() const { return m_type_index; }
102 auto get_value_index() const { return m_value_index; }
103
104 virtual bool matches(const std::string&, bool exact = true) const;
105
106 template <typename Tp>
107 std::pair<bool, Tp> get() const;
108
109 template <typename Tp>
110 bool get(Tp& _val) const;
111
112 template <typename Tp, enable_if_t<std::is_fundamental<decay_t<Tp>>::value> = 0>
113 bool set(const Tp& _val);
114 void set(const std::string& _val) { parse(_val); }
115
117
118 template <typename Tp>
119 static auto cast(std::shared_ptr<vsettings>& _val)
120 {
121 return static_cast<tsettings<decay_t<Tp>, Tp>*>(_val.get());
122 }
123
124 template <typename Tp>
125 static auto cast(const std::shared_ptr<vsettings>& _val)
126 {
127 return static_cast<const tsettings<decay_t<Tp>, Tp>*>(_val.get());
128 }
129
130protected:
131 static int get_debug()
132 {
133 static bool _bool_val = get_env("TIMEMORY_DEBUG_SETTINGS", false);
134 static int _int_val = get_env("TIMEMORY_DEBUG_SETTINGS", 0);
135 return (_bool_val) ? _int_val : 0;
136 }
137
138 template <typename Tp>
139 void report_change(Tp _old, const Tp& _new);
140
141protected:
142 std::type_index m_type_index = std::type_index(typeid(void)); // NOLINT
143 std::type_index m_value_index = std::type_index(typeid(void)); // NOLINT
144 int32_t m_count = -1; // NOLINT
145 int32_t m_max_count = -1; // NOLINT
146 std::string m_name = ""; // NOLINT
147 std::string m_env_name = ""; // NOLINT
149 std::vector<std::string> m_cmdline = {}; // NOLINT
150 std::vector<std::string> m_choices = {}; // NOLINT
151};
152//
153template <typename Tp>
154std::pair<bool, Tp>
156{
157 auto _ref = dynamic_cast<const tsettings<Tp, Tp&>*>(this);
158 if(_ref)
159 {
160 return { true, _ref->get() };
161 }
162
163 auto _nref = dynamic_cast<const tsettings<Tp, Tp>*>(this);
164 if(_nref)
165 return { true, _nref->get() };
166
167 return { false, Tp{} };
168}
169//
170template <typename Tp>
171bool
172vsettings::get(Tp& _val) const
173{
174 auto&& _ret = this->get<Tp>();
175 if(_ret.first)
176 _val = _ret.second;
177 return _ret.first;
178}
179//
180template <typename Tp, enable_if_t<std::is_fundamental<decay_t<Tp>>::value>>
181bool
182vsettings::set(const Tp& _val)
183{
184 auto _ref = dynamic_cast<tsettings<Tp, Tp&>*>(this);
185 if(_ref)
186 {
187 _ref->set(_val);
188 return true;
189 }
190
191 auto _nref = dynamic_cast<tsettings<Tp, Tp>*>(this);
192 if(_nref)
193 {
194 _nref->set(_val);
195 return true;
196 }
197
198 return false;
199}
200//
201template <typename Tp>
202void
203vsettings::report_change(Tp _old, const Tp& _new)
204{
205 if(get_debug() < 1)
206 return;
207
208 if(_old != _new)
209 {
210 std::ostringstream oss;
211 oss << std::boolalpha;
212 oss << "[timemory::settings] " << m_name << " (env: " << m_env_name
213 << ") changed: " << _old << " --> " << _new << "\n";
214 if(get_debug() > 1)
215 {
216 print_demangled_backtrace<6, 3>(oss);
217 }
218 std::cerr << oss.str() << std::flush;
219 }
220}
221//
222} // namespace tim
223
224#if defined(TIMEMORY_SETTINGS_HEADER_MODE)
226#endif
const hash_alias_ptr_t hash_value_t std::string *& _ret
Definition: definition.hpp:300
Definition: kokkosp.cpp:39
Tp get_env(const std::string &env_id, Tp _default, bool _store)
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
Implements a specific setting.
Definition: tsettings.hpp:56
void set(Tp)
Definition: tsettings.hpp:220
Virtual base class for storing settings.
Definition: vsettings.hpp:56
std::type_index m_type_index
Definition: vsettings.hpp:142
const auto & get_description() const
Definition: vsettings.hpp:90
const auto & get_name() const
Definition: vsettings.hpp:88
vsettings(const vsettings &)=default
int32_t m_count
Definition: vsettings.hpp:144
auto get_value_index() const
Definition: vsettings.hpp:102
int32_t m_max_count
Definition: vsettings.hpp:145
virtual display_map_t get_display(std::ios::fmtflags fmt={}, int _w=-1, int _p=-1)
virtual void add_argument(argparse::argument_parser &)=0
void report_change(Tp _old, const Tp &_new)
Definition: vsettings.hpp:203
virtual bool matches(const std::string &, bool exact=true) const
virtual void reset()=0
virtual void clone(std::shared_ptr< vsettings > rhs)
std::vector< std::string > m_choices
Definition: vsettings.hpp:150
bool set(const Tp &_val)
Definition: vsettings.hpp:182
vsettings(std::string _name="", std::string _env_name="", std::string _descript="", std::vector< std::string > _cmdline={}, int32_t _count=-1, int32_t _max_count=-1, std::vector< std::string > _choices={})
Definition: vsettings.cpp:44
std::string m_name
Definition: vsettings.hpp:146
virtual std::string as_string() const =0
const auto & get_max_count() const
Definition: vsettings.hpp:94
auto get_type_index() const
Definition: vsettings.hpp:101
virtual parser_func_t get_action(TIMEMORY_API)=0
void set_choices(const std::vector< std::string > &v)
Definition: vsettings.hpp:98
std::pair< bool, Tp > get() const
Definition: vsettings.hpp:155
std::type_index m_value_index
Definition: vsettings.hpp:143
void set(const std::string &_val)
Definition: vsettings.hpp:114
virtual void parse()=0
virtual ~vsettings()=default
vsettings & operator=(const vsettings &)=default
std::map< std::string, std::string > display_map_t
Definition: vsettings.hpp:59
void set_count(int32_t v)
Definition: vsettings.hpp:96
vsettings & operator=(vsettings &&)=default
static int get_debug()
Definition: vsettings.hpp:131
virtual void parse(const std::string &)=0
const auto & get_env_name() const
Definition: vsettings.hpp:89
std::string m_description
Definition: vsettings.hpp:148
const auto & get_command_line() const
Definition: vsettings.hpp:91
std::string m_env_name
Definition: vsettings.hpp:147
static auto cast(std::shared_ptr< vsettings > &_val)
Definition: vsettings.hpp:119
const auto & get_count() const
Definition: vsettings.hpp:93
std::vector< std::string > m_cmdline
Definition: vsettings.hpp:149
virtual std::shared_ptr< vsettings > clone()=0
void set_max_count(int32_t v)
Definition: vsettings.hpp:97
std::function< void(parser_t &)> parser_func_t
Definition: vsettings.hpp:58
static auto cast(const std::shared_ptr< vsettings > &_val)
Definition: vsettings.hpp:125
vsettings(vsettings &&)=default
void set_command_line(const std::vector< std::string > &v)
Definition: vsettings.hpp:99
const auto & get_choices() const
Definition: vsettings.hpp:92