25 #if !defined(TIMEMORY_LIBRARY_SOURCE)
26 # define TIMEMORY_LIBRARY_SOURCE 1
40 #include <unordered_map>
41 #include <unordered_set>
58 using toolset_t =
typename library_toolset_t::component_type;
59 using region_map_t = std::unordered_map<std::string, std::stack<uint64_t>>;
65 "#-------------------------------------------------------------------------#";
88 get_components_stack()
90 static auto _leader_tid = std::this_thread::get_id();
93 return (std::this_thread::get_id() == _leader_tid) ? _leader_instance
104 tim::get_env<std::string>(
"TIMEMORY_GLOBAL_COMPONENTS",
"wall_clock");
114 auto& _stack = get_components_stack();
120 return _stack.back();
128 static auto _instance = std::array<bool, 2>({ {
false,
false } });
148 static uint64_t uniqID = 0;
159 (*timemory_create_function)(name, id, n, ctypes);
164 static thread_local
auto& _record_map = get_record_map();
166 _record_map.insert({ *id,
toolset_t(name,
true) });
168 _record_map[*id].start();
169 if(_record_map.bucket_count() > _record_map.size())
170 _record_map.rehash(_record_map.size() + 10);
180 (*timemory_delete_function)(id);
182 else if(get_record_map().find(
id) != get_record_map().end())
184 static thread_local
auto& _record_map = get_record_map();
186 _record_map[id].stop();
187 _record_map.erase(
id);
203 char* _name[1] = { name };
229 if(
argv[i] ==
nullptr)
239 printf(
"%s\n", spacer.c_str());
240 printf(
"\tInitialization of timemory library...\n");
241 printf(
"%s\n\n", spacer.c_str());
262 if(
_settings->get_enabled() ==
false && get_record_map().empty())
265 auto& _record_map = get_record_map();
269 printf(
"\n%s\n", spacer.c_str());
270 printf(
"\tFinalization of timemory library...\n");
271 printf(
"%s\n\n", spacer.c_str());
276 std::unordered_set<uint64_t> keys;
277 for(
auto& itr : _record_map)
278 keys.insert(itr.first);
281 for(
auto& itr : keys)
298 tim::dmp::set_finalized(
true);
304 #if defined(__PGI) || defined(__INTEL_COMPILER)
325 tim::get_env<std::string>(
"TIMEMORY_GLOBAL_COMPONENTS", _component_string);
327 static thread_local
auto& _stack = get_components_stack();
352 _stack.push_back(itr);
362 _stack.erase(std::remove(_stack.begin(), _stack.end(), itr), _stack.end());
370 static thread_local
auto& _stack = get_components_stack();
379 static thread_local
auto& _stack = get_components_stack();
383 va_start(args, types);
386 auto enum_arg = va_arg(args,
int);
389 comp.push_back(enum_arg);
393 _stack.push_back(comp);
401 static thread_local
auto& _stack = get_components_stack();
402 if(_stack.size() > 1)
413 *
id = std::numeric_limits<uint64_t>::max();
421 printf(
"beginning record for '%s' (id = %lli)...\n", name,
422 (
long long int) *
id);
433 *
id = std::numeric_limits<uint64_t>::max();
442 printf(
"beginning record for '%s' (id = %lli)...\n", name,
443 (
long long int) *
id);
454 *
id = std::numeric_limits<uint64_t>::max();
463 auto enum_arg = va_arg(args,
int);
466 comp.push_back(enum_arg);
474 printf(
"beginning record for '%s' (id = %lli)...\n", name,
475 (
long long int) *
id);
485 return std::numeric_limits<uint64_t>::max();
493 printf(
"beginning record for '%s' (id = %lli)...\n", name,
506 return std::numeric_limits<uint64_t>::max();
514 printf(
"beginning record for '%s' (id = %lli)...\n", name,
527 return std::numeric_limits<uint64_t>::max();
533 va_start(args, name);
536 auto enum_arg = va_arg(args,
int);
539 comp.push_back(enum_arg);
547 printf(
"beginning record for '%s' (id = %lli)...\n", name,
559 if(!lk ||
id == std::numeric_limits<uint64_t>::max())
566 printf(
"ending record for %lli...\n", (
long long int)
id);
577 auto& region_map = get_region_map();
581 region_map[name].push(idx);
591 auto& region_map = get_region_map();
592 auto itr = region_map.find(name);
593 if(itr == region_map.end() || (itr != region_map.end() && itr->second.empty()))
594 fprintf(stderr,
"Warning! region '%s' does not exist!\n", name);
597 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.
component_enum_t & get_current_components()
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)
std::string & get_default_components()
void timemory_init_library(int argc, char **argv)
Initializes timemory. Not strictly necessary but highly recommended.
std::vector< TIMEMORY_COMPONENT > component_enum_t
std::array< bool, 2 > & get_library_state()
void timemory_remove_components(const char *_component_string)
Remove some components to the current set of components being collected. Any components which are not...
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.
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
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