34#if defined(GEANT4_USE_TIMEMORY)
35# include <timemory/runtime/configure.hpp>
64#if defined(GEANT4_USE_TIMEMORY)
65 tim::timemory_init(argc, argv);
66 std::vector<std::string> _args;
67 for(
int i = 0; i < argc; ++i)
68 _args.push_back(argv[i]);
78#if defined(GEANT4_USE_TIMEMORY)
79 tim::timemory_init(argc, argv);
80 std::vector<std::string> _args;
81 for(
int i = 0; i < argc; ++i)
82 _args.push_back(argv[i]);
93#if defined(GEANT4_USE_TIMEMORY)
106 const std::vector<std::string>&
args)
108#if defined(GEANT4_USE_TIMEMORY)
110 using parser_err_t =
typename parser_t::result_type;
116 std::unique_lock<std::mutex> lk(mtx);
118 static auto tid = std::this_thread::get_id();
119 if(std::this_thread::get_id() != tid)
127 auto help_action = [](parser_t& p) {
133 parser.on_error([=](parser_t& p, parser_err_t _err) {
134 std::cerr << _err << std::endl;
138 auto get_bool = [](
const std::string& _str,
bool _default) {
141 using namespace std::regex_constants;
142 const auto regex_config = egrep | icase;
143 if(std::regex_match(_str, std::regex(
"on|true|yes|1", regex_config)))
145 else if(std::regex_match(_str, std::regex(
"off|false|no|0", regex_config)))
153 .names({
"-p",
"--profile" })
154 .description(
"Profiler modes")
155 .choices({
"run",
"event",
"track",
"step",
"user" })
156 .
action([&](parser_t& p) {
157 using namespace std::regex_constants;
158 const auto regex_config = egrep | icase;
159 for(
auto&& itr : p.get<std::vector<std::string>>(
"profile"))
161 if(std::regex_match(itr, std::regex(
"run", regex_config)))
163 else if(std::regex_match(itr, std::regex(
"event", regex_config)))
165 else if(std::regex_match(itr, std::regex(
"track", regex_config)))
167 else if(std::regex_match(itr, std::regex(
"step", regex_config)))
169 else if(std::regex_match(itr, std::regex(
"user", regex_config)))
177 .names({
"-r",
"--run-components" })
178 .description(
"Components for run profiling (see 'timemory-avail -s')")
179 .action([&](parser_t& p) {
181 tim::configure<G4RunProfiler>(
182 p.get<std::vector<std::string>>(
"run-components"));
185 .names({
"-e",
"--event-components" })
186 .description(
"Components for event profiling (see 'timemory-avail -s')")
187 .action([&](parser_t& p) {
189 tim::configure<G4EventProfiler>(
190 p.get<std::vector<std::string>>(
"event-components"));
193 .names({
"-t",
"--track-components" })
194 .description(
"Components for track profiling (see 'timemory-avail -s')")
195 .action([&](parser_t& p) {
197 tim::configure<G4TrackProfiler>(
198 p.get<std::vector<std::string>>(
"track-components"));
201 .names({
"-s",
"--step-components" })
202 .description(
"Components for step profiling (see 'timemory-avail -s')")
203 .action([&](parser_t& p) {
205 tim::configure<G4StepProfiler>(
206 p.get<std::vector<std::string>>(
"step-components"));
209 .names({
"-u",
"--user-components" })
210 .description(
"Components for user profiling (see 'timemory-avail -s')")
211 .action([&](parser_t& p) {
213 tim::configure<G4UserProfiler>(
214 p.get<std::vector<std::string>>(
"user-components"));
220 .names({
"-H",
"--hierarchy",
"--tree" })
221 .description(
"Display the results as a call-stack hierarchy.")
223 .action([&](parser_t& p) {
224 tim::settings::flat_profile() =
225 !get_bool(p.get<std::string>(
"tree"),
true);
228 .names({
"-F",
"--flat" })
229 .description(
"Display the results as a flat call-stack")
231 .action([&](parser_t& p) {
232 tim::settings::flat_profile() =
233 get_bool(p.get<std::string>(
"flat"),
true);
236 .names({
"-T",
"--timeline" })
238 "Do not merge duplicate entries at the same call-stack position")
240 .action([&](parser_t& p) {
241 tim::settings::timeline_profile() =
242 get_bool(p.get<std::string>(
"timeline"),
true);
245 .names({
"--per-thread" })
247 "Display the results for each individual thread (default: aggregation)")
249 .action([&](parser_t& p) {
250 tim::settings::flat_profile() =
251 get_bool(p.get<std::string>(
"per-thread"),
true);
254 .names({
"--per-event" })
255 .description(
"Each G4Event is a unique entry")
257 .action([&](parser_t& p) {
258 tim::settings::timeline_profile() =
259 get_bool(p.get<std::string>(
"per-event"),
true);
265 .names({
"-D",
"--dart" })
266 .description(
"Enable Dart output (CTest/CDash data tracking)")
268 .action([&](parser_t& p) {
269 tim::settings::dart_output() = get_bool(p.get<std::string>(
"dart"),
true);
272 .names({
"-J",
"--json" })
273 .description(
"Enable JSON output")
275 .action([&](parser_t& p) {
276 tim::settings::json_output() = get_bool(p.get<std::string>(
"json"),
true);
279 .names({
"-P",
"--plot" })
280 .description(
"Plot the JSON output")
282 .action([&](parser_t& p) {
283 tim::settings::plot_output() = get_bool(p.get<std::string>(
"plot"),
true);
286 .names({
"-X",
"--text" })
287 .description(
"Enable TEXT output")
289 .action([&](parser_t& p) {
290 tim::settings::text_output() = get_bool(p.get<std::string>(
"text"),
true);
293 .names({
"-C",
"--cout" })
294 .description(
"Enable output to terminal")
296 .action([&](parser_t& p) {
297 tim::settings::cout_output() = get_bool(p.get<std::string>(
"cout"),
true);
300 .names({
"-O",
"--output-path" })
301 .description(
"Set the output directory")
303 .action([&](parser_t& p) {
304 tim::settings::output_path() = p.get<std::string>(
"output-path");
307 .names({
"-W",
"--hw-counters" })
309 "Set the hardware counters to collect (see 'timemory-avail -H')")
310 .action([&](parser_t& p) {
311 tim::settings::papi_events() = p.get<std::string>(
"hw-counters");
314 tim::settings::time_output() =
true;
315 tim::settings::time_format() =
"%F_%I.%M_%p";
320 std::cout <<
"Error! " << err << std::endl;
333#if defined(GEANT4_USE_TIMEMORY)
346 static thread_local bool _once =
false;
348 tim::timemory_finalize();
390 std::unique_lock<std::mutex> lk(mtx);
394 static bool _first =
true;
396 return ((_first =
false), _instance);
418 return QueryHandler_t{ GetPersistent<G4ProfileOp::Query>().m_functor };
427 return LabelHandler_t{ GetPersistent<G4ProfileOp::Label>().m_functor };
436 return ToolHandler_t{ GetPersistent<G4ProfileOp::Tool>().m_functor };
447 GetPersistentFallback<G4ProfileOp::Query>().m_functor
458 GetPersistentFallback<G4ProfileOp::Label>().m_functor
468 return ToolHandler_t{ GetPersistentFallback<G4ProfileOp::Tool>().m_functor };
500#define G4PROFILE_INSTANTIATION(TYPE) \
501 template TYPE::PersistentSettings<G4ProfileOp::Query>& \
502 TYPE::GetPersistentFallback<G4ProfileOp::Query>(); \
503 template TYPE::PersistentSettings<G4ProfileOp::Label>& \
504 TYPE::GetPersistentFallback<G4ProfileOp::Label>(); \
505 template TYPE::PersistentSettings<G4ProfileOp::Tool>& \
506 TYPE::GetPersistentFallback<G4ProfileOp::Tool>(); \
508 template TYPE::PersistentSettings<G4ProfileOp::Query>& \
509 TYPE::GetPersistent<G4ProfileOp::Query>(); \
510 template TYPE::PersistentSettings<G4ProfileOp::Label>& \
511 TYPE::GetPersistent<G4ProfileOp::Label>(); \
512 template TYPE::PersistentSettings<G4ProfileOp::Tool>& \
513 TYPE::GetPersistent<G4ProfileOp::Tool>();
523#if !defined(GEANT4_USE_TIMEMORY)
524# define TIMEMORY_WEAK_PREFIX
525# define TIMEMORY_WEAK_POSTFIX
540#ifdef GEANT4_USE_TIMEMORY
543 static bool _once =
false;
548 puts(
">>> G4ProfilerInit <<<");
554 tim::settings::plot_output() =
false;
556 tim::settings::cout_output() =
false;
558 tim::settings::time_output() =
false;
560 tim::settings::time_format() =
"%F_%I.%M_%p";
562 tim::settings::timing_precision() = 6;
564 tim::settings::width() = 12;
566 tim::settings::dart_count() = 1;
571 tim::settings::dart_label() =
true;
574 tim::settings::parse();
581#ifdef GEANT4_USE_TIMEMORY
TIMEMORY_WEAK_PREFIX void G4ProfilerInit(void) TIMEMORY_WEAK_POSTFIX
#define G4PROFILE_INSTANTIATION(TYPE)
#define TIMEMORY_WEAK_PREFIX
#define TIMEMORY_WEAK_POSTFIX
FuncHandler< this_type, LabelFunc_t, std::string > LabelHandler_t
static QueryHandler_t GetFallbackQueryFunctor()
FuncHandler< this_type, ToolFunc_t, type * > ToolHandler_t
static PersistentSettings< Idx > & GetPersistentFallback()
static ToolHandler_t GetToolFunctor()
static ToolHandler_t GetFallbackToolFunctor()
static PersistentSettings< Idx > & GetPersistent()
static QueryHandler_t GetQueryFunctor()
static LabelHandler_t GetLabelFunctor()
static LabelHandler_t GetFallbackLabelFunctor()
G4ProfilerConfig()=default
FuncHandler< this_type, QueryFunc_t, bool > QueryHandler_t
static void Configure(const std::vector< std::string > &args)
static void SetEnabled(size_t v, bool val)
std::array< bool, G4ProfileType::TypeEnd > array_type
static array_type & GetEnabled()
void consume_parameters(Args &&...)