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.
conditional.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/** \file timemory/utility/conditional.hpp
26 * \headerfile timemory/utility/conditional.hpp "timemory/utility/conditional.hpp"
27 * Provides structure for conditionally generating a timemory component configuration
28 *
29 */
30
31#pragma once
32
33#include <cstddef>
34#include <functional>
35#include <string>
36#include <utility>
37
38namespace tim
39{
40namespace utility
41{
42///
43/// \struct tim::conditional
44/// \brief This provides a conditional generator
45///
46/// \code{.cpp}
47///
48/// struct G4ProfileType
49/// {
50/// enum : size_t
51/// {
52/// Track = 0
53/// };
54/// };
55///
56/// struct geant4_tag {};
57///
58/// using G4TrackProfileBundle =
59/// tim::component::user_bundle<G4ProfileType::Track, geant4_tag>;
60///
61/// using G4TrackProfiler =
62/// tim::conditional<G4ProfileType::Track, const G4Track*>;
63///
64///
65/// class G4Track
66/// {
67/// public:
68/// G4Track(G4DynamicParticle*);
69/// ~G4Track();
70///
71/// private:
72/// tim::auto_tuple<G4TrackProfileBundle>* fTrackProfiler = nullptr;
73/// };
74///
75///
76/// G4Track::G4Track(G4DynamicParticle* p)
77/// : fTrackProfiler(nullptr)
78/// {
79/// if(Profiler_t::enable(this))
80/// {
81/// auto label = Profiler_t::label(this);
82/// if(label.length() == 0)
83/// label = p->GetParticleDefinition()->GetParticleName();
84/// fTrackProfiler = new ProfilerHandle_t(label);
85/// }
86/// }
87///
88///
89/// ~G4Track::G4Track()
90/// {
91/// delete fTrackProfiler;
92/// }
93///
94/// \endcode
95template <size_t CategoryT, typename... EvalArgsT>
96struct conditional
97{
98public:
99 using this_type = conditional<CategoryT, EvalArgsT...>;
100 using eval_functor_t = std::function<bool(EvalArgsT...)>;
101 using label_functor_t = std::function<std::string(EvalArgsT...)>;
102
103 // invokes the functor that determines whether to enable profiling
104 static bool query(EvalArgsT... _args) { return get_evaluator()(_args...); }
105 // invokes the functor for generating a label when profiling is enabled
106 static std::string label(EvalArgsT... _args) { return get_labeler()(_args...); }
107
108 ///
109 /// \fn eval_functor_t& get_evaluator()
110 /// \brief This is the functor that determines whether to activate the profiler.
111 /// By default, returns false.
112 /// Use a lambda to customize the activation of the profiler for the category
113 ///
114 /// \code{.cpp}
115 ///
116 /// using TrackProfiler = G4Profiler<G4ProfileType::Track, const G4Track*>;
117 ///
118 /// TrackProfiler::get_evaluator() = [](const G4Track* t)
119 /// {
120 /// auto* pdef = t->GetDynamicParticle()->GetParticleDefinition();
121 /// return (pdef == G4Electron::ElectronDefinition());
122 /// };
123 ///
124 /// TrackProfiler::get_labeler() = [](const Track* t)
125 /// {
126 /// auto* p = t->GetDynamicParticle();
127 ///
128 /// G4double energy = p->GetKineticEnergy() / CLHEP::MeV;
129 /// const G4double eBin = 10 * CLHEP::MeV;
130 /// G4int eMin = energy - fmod(energy, eBin);
131 /// G4int eMax = energy + eBin - fmod(energy + eBin, eBin);
132 ///
133 /// std::stringstream ss;
134 /// ss << p->GetParticleDefinition()->GetParticleName() << "_"
135 /// << eMin << "," << eMax << "_MeV";
136 /// return ss.str();
137 /// };
138 /// \endcode
139 ///
140 static eval_functor_t& get_evaluator()
141 {
142 static eval_functor_t _instance = [](EvalArgsT...) { return false; };
143 return _instance;
144 }
145
146 ///
147 /// \fn label_functor_t& get_labeler()
148 /// \brief This is the functor that generates a label for a profiling instance.
149 /// By default, it returns an empty string.
150 /// Use a lambda to customize the label for the profiling instance
151 ///
152 /// \code{.cpp}
153 /// TrackProfiler::get_labeler() = [](const Track* t)
154 /// {
155 /// auto* p = t->GetDynamicParticle();
156 ///
157 /// G4double energy = p->GetKineticEnergy() / CLHEP::MeV;
158 /// const G4double eBin = 10 * CLHEP::MeV;
159 /// G4int eMin = energy - fmod(energy, eBin);
160 /// G4int eMax = energy + eBin - fmod(energy + eBin, eBin);
161 ///
162 /// std::stringstream ss;
163 /// ss << p->GetParticleDefinition()->GetParticleName() << "_"
164 /// << eMin << "," << eMax << "_MeV";
165 /// return ss.str();
166 /// };
167 /// \endcode
168 ///
169 static label_functor_t& get_labeler()
170 {
171 static label_functor_t _instance = [](EvalArgsT...) { return std::string(""); };
172 return _instance;
173 }
174};
175//
176} // namespace utility
177} // namespace tim
Definition: kokkosp.cpp:39
std::array< char *, 4 > _args
tim::mpl::apply< std::string > string
Definition: macros.hpp:53