25#if !defined(TIMEMORY_LIBRARY_SOURCE)
26# define TIMEMORY_LIBRARY_SOURCE 1
41#include <unordered_map>
42#include <unordered_set>
59using toolset_t =
typename library_toolset_t::component_type;
60using region_map_t = std::unordered_map<std::string, std::stack<uint64_t>>;
66 "#-------------------------------------------------------------------------#";
91 static auto _leader_tid = std::this_thread::get_id();
94 return (std::this_thread::get_id() == _leader_tid) ? _leader_instance
105 tim::get_env<std::string>(
"TIMEMORY_GLOBAL_COMPONENTS",
"wall_clock");
115 auto& _stack = get_components_stack();
121 return _stack.back();
129 static auto _instance = std::array<bool, 2>({ {
false,
false } });
149 static uint64_t uniqID = 0;
160 (*timemory_create_function)(name, id, n, ctypes);
165 static thread_local auto& _record_map = get_record_map();
167 _record_map.insert({ *id,
toolset_t(name,
true) });
169 _record_map[*id].start();
170 if(_record_map.bucket_count() > _record_map.size())
171 _record_map.rehash(_record_map.size() + 10);
181 (*timemory_delete_function)(id);
183 else if(get_record_map().find(
id) != get_record_map().end())
185 static thread_local auto& _record_map = get_record_map();
187 _record_map[id].stop();
188 _record_map.erase(
id);
204 char* _name[1] = { name };
230 if(
argv[i] ==
nullptr)
240 printf(
"%s\n", spacer.c_str());
241 printf(
"\tInitialization of timemory library...\n");
242 printf(
"%s\n\n", spacer.c_str());
263 if(
_settings->get_enabled() ==
false && get_record_map().empty())
266 auto& _record_map = get_record_map();
270 printf(
"\n%s\n", spacer.c_str());
271 printf(
"\tFinalization of timemory library...\n");
272 printf(
"%s\n\n", spacer.c_str());
277 std::unordered_set<uint64_t> keys;
278 for(
auto& itr : _record_map)
279 keys.insert(itr.first);
282 for(
const auto& itr : keys)
299 tim::dmp::set_finalized(
true);
305#if defined(__PGI) || defined(__INTEL_COMPILER)
326 tim::get_env<std::string>(
"TIMEMORY_GLOBAL_COMPONENTS", _component_string);
328 static thread_local auto& _stack = get_components_stack();
353 _stack.push_back(itr);
363 _stack.erase(std::remove(_stack.begin(), _stack.end(), itr), _stack.end());
371 static thread_local auto& _stack = get_components_stack();
380 static thread_local auto& _stack = get_components_stack();
384 va_start(args, types);
387 auto enum_arg = va_arg(args,
int);
390 comp.push_back(enum_arg);
394 _stack.push_back(comp);
402 static thread_local auto& _stack = get_components_stack();
403 if(_stack.size() > 1)
422 printf(
"beginning record for '%s' (id = %lli)...\n", name,
423 (
long long int) *
id);
443 printf(
"beginning record for '%s' (id = %lli)...\n", name,
444 (
long long int) *
id);
464 auto enum_arg = va_arg(args,
int);
467 comp.push_back(enum_arg);
475 printf(
"beginning record for '%s' (id = %lli)...\n", name,
476 (
long long int) *
id);
494 printf(
"beginning record for '%s' (id = %lli)...\n", name,
515 printf(
"beginning record for '%s' (id = %lli)...\n", name,
534 va_start(args, name);
537 auto enum_arg = va_arg(args,
int);
540 comp.push_back(enum_arg);
548 printf(
"beginning record for '%s' (id = %lli)...\n", name,
567 printf(
"ending record for %lli...\n", (
long long int)
id);
578 auto& region_map = get_region_map();
582 region_map[name].push(idx);
592 auto& region_map = get_region_map();
593 auto itr = region_map.find(name);
594 if(itr == region_map.end() || (itr != region_map.end() && itr->second.empty()))
595 fprintf(stderr,
"Warning! region '%s' does not exist!\n", name);
598 uint64_t idx = itr->second.top();
static pointer_t instance()
Get a shared pointer to the instance for the current thread.
static pointer_t master_instance()
Get a shared pointer to the instance on the primary thread.
void(* timemory_delete_func_t)(uint64_t)
function pointer type for timemory_delete_function
void(* timemory_create_func_t)(const char *, uint64_t *, int, int *)
#define TIMEMORY_COMPONENTS_END
void timemory_pause(void)
Turn off timemory collection.
void timemory_init_library_(int argc, char **argv)
void timemory_push_components_enum(int types,...)
Replace the current set of components with a new set of components with the set of enumerations provi...
void timemory_create_record(const char *name, uint64_t *id, int n, int *ctypes)
timemory_create_func_t timemory_create_function
The function pointer to set to customize which components are used by library interface.
void timemory_begin_record_(const char *name, uint64_t *id)
std::unordered_map< uint64_t, toolset_t > record_map_t
timemory_delete_func_t timemory_delete_function
The function pointer to set which deletes an entry created by timemory_create_function.
void timemory_create_record_(const char *name, uint64_t *id, int n, int *ct)
void timemory_set_environ(const char *evar, const char *eval, int over, int parse)
uint64_t timemory_get_unique_id(void)
Returns a unique integer for a thread.
void timemory_push_region(const char *name)
void timemory_delete_record(uint64_t id)
Deletes the record created by timemory_create_record.
void timemory_end_record(uint64_t id)
uint64_t timemory_get_begin_record_types(const char *name, const char *ctypes)
Variant to timemory_begin_record_types which returns a unique integer.
std::unordered_map< std::string, std::stack< uint64_t > > region_map_t
uint64_t timemory_get_begin_record_enum(const char *name,...)
Variant to timemory_begin_record_enum which returns a unique integer.
void timemory_begin_record_enum(const char *name, uint64_t *id,...)
Similar to timemory_begin_record but accepts a specific enumerated set of components,...
void timemory_pop_components(void)
Inverse of the last timemory_push_components or timemory_push_components_enum call....
void timemory_pop_components_(void)
void timemory_add_components(const char *_component_string)
Add some components to the current set of components being collected Any components which are current...
void timemory_set_default_(const char *components)
void timemory_pop_region(const char *name)
bool timemory_library_is_initialized(void)
Returns whether the library is initialized or not.
void timemory_resume(void)
Turn on timemory collection.
void timemory_begin_record_types_(const char *name, uint64_t *id, const char *ctypes)
void timemory_init_library(int argc, char **argv)
Initializes timemory. Not strictly necessary but highly recommended.
std::vector< TIMEMORY_COMPONENT > component_enum_t
void timemory_remove_components(const char *_component_string)
Remove some components to the current set of components being collected. Any components which are not...
std::array< bool, 2 > & get_library_state()
std::string & get_default_components()
void timemory_push_region_(const char *name)
uint64_t timemory_get_begin_record_types_(const char *name, const char *ctypes)
void timemory_delete_record_(uint64_t id)
void timemory_finalize_library_(void)
void timemory_push_components(const char *_component_string)
Replace the current set of components with a new set of components.
void timemory_push_components_(const char *components)
uint64_t timemory_get_begin_record_(const char *name)
void timemory_named_init_library(char *name)
void timemory_pop_region_(const char *name)
void timemory_end_record_(uint64_t id)
std::deque< component_enum_t > components_stack_t
void timemory_begin_record_types(const char *name, uint64_t *id, const char *ctypes)
Similar to timemory_begin_record but accepts a specific set of components as a string.
uint64_t timemory_get_begin_record(const char *name)
Variant to timemory_begin_record which returns a unique integer.
component_enum_t & get_current_components()
void timemory_set_default(const char *_component_string)
Pass in a default set of components to use. Will be overridden by TIMEMORY_COMPONENTS environment var...
void timemory_begin_record(const char *name, uint64_t *id)
void timemory_finalize_library(void)
Finalizes timemory. Output will be generated. Any attempt to store data within timemory storage is un...
typename library_toolset_t::component_type toolset_t
TIMEMORY_LIBRARY_TYPE library_toolset_t
::tim::statistics< Tp > max(::tim::statistics< Tp > lhs, const Tp &rhs)
A lightweight synchronization object for preventing recursion. The first template parameter should ha...
void set_env(const std::string &env_var, const Tp &_val, int override)
void timemory_finalize()
finalization of the specified types
void initialize(CompList< CompTypes... > &obj, std::initializer_list< EnumT > components)
std::vector< TIMEMORY_COMPONENT > enumerate_components(const Container< StringT, ExtraArgs... > &component_names)
description: use this function to generate an array of enumerations from a list of string that can be...
void timemory_init(Args &&... _args)
char argparse::argument_parser tim::settings * _settings
tim::mpl::apply< std::string > string
static void parse(settings *=instance< TIMEMORY_API >())
static settings * instance()
#define TIMEMORY_LIBRARY_TYPE