56 #include <boost/functional/hash/hash.hpp> 57 #include <boost/ref.hpp> 58 #include <boost/tuple/detail/tuple_basic.hpp> 59 #include <boost/unordered/unordered_map.hpp> 60 #include <nlohmann/json.hpp> 70 static int sum(
int* val,
MPI_Comm comm);
71 static double sum(
double* val,
MPI_Comm comm);
72 static long sum(
long* val,
MPI_Comm comm);
74 static int min(
int* val,
MPI_Comm comm);
75 static double min(
double* val,
MPI_Comm comm);
76 static long min(
long* val,
MPI_Comm comm);
78 static int max(
int* val,
MPI_Comm comm);
79 static double max(
double* val,
MPI_Comm comm);
80 static long max(
long* val,
MPI_Comm comm);
84 #define CONSTEXPR_ constexpr 90 #define _PASTE(a,b) a ## b 91 #define PASTE(a,b) _PASTE(a, b) 110 #ifdef FLOW123D_DEBUG_PROFILER 111 #define START_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); TimerFrame PASTE(timer_,__LINE__) = TimerFrame( PASTE(cp_,__LINE__) ) 113 #define START_TIMER(tag) 131 #ifdef FLOW123D_DEBUG_PROFILER 132 #define START_TIMER_EXT(tag, subtag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT_EXT(tag, subtag); TimerFrame PASTE(timer_,__LINE__) = TimerFrame( PASTE(cp_,__LINE__) ) 134 #define START_TIMER_EXT(tag, subtag) 144 #ifdef FLOW123D_DEBUG_PROFILER 145 #define END_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); Profiler::instance()->stop_timer( PASTE(cp_,__LINE__) ) 147 #define END_TIMER(tag) 155 #ifdef FLOW123D_DEBUG_PROFILER 156 #define END_START_TIMER(tag) Profiler::instance()->stop_timer(); START_TIMER(tag); 158 #define END_START_TIMER(tag) 181 #ifdef FLOW123D_DEBUG_PROFILER 182 #define ADD_CALLS(n_calls) Profiler::instance()->add_calls(n_calls) 184 #define ADD_CALLS(n_calls) 191 #ifdef FLOW123D_DEBUG_PROFILER 196 #define PROFILER_EMPTY_SUBTAG "" 201 #define PROFILER_HASH_DEFAULT 0 210 inline CONSTEXPR_ unsigned int str_hash(
const char * str,
unsigned int default_value) {
211 #define SALT 0 //0xef50e38f 212 return (*str == 0 ? SALT : default_value + str_hash(str+1, PROFILER_HASH_DEFAULT) * 101 + (
unsigned int)(*str) );
218 #define CODE_POINT(tag) CodePoint(tag, __FILE__, __func__, __LINE__) 223 #define CODE_POINT_EXT(tag, subtag) CodePoint(tag, subtag, __FILE__, __func__, __LINE__) 238 CONSTEXPR_ CodePoint(
const char *tag,
const char * file,
const char * func,
const unsigned int line)
239 : tag_(tag), subtag_(PROFILER_EMPTY_SUBTAG), file_(file), func_(func), line_(line),
240 hash_(str_hash(tag, PROFILER_HASH_DEFAULT)),
241 hash_idx_( str_hash(tag, PROFILER_HASH_DEFAULT)%max_n_timer_childs )
243 CONSTEXPR_ CodePoint(
const char *tag,
const char *subtag,
const char * file,
const char * func,
const unsigned int line)
244 : tag_(tag), subtag_(subtag), file_(file), func_(func), line_(line),
245 hash_(str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))),
246 hash_idx_( str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))%max_n_timer_childs )
250 static const unsigned int max_n_timer_childs=13;
253 const char *
const tag_;
256 const char *
const subtag_;
259 const char *
const file_;
262 const char *
const func_;
265 const unsigned int line_;
271 unsigned int hash_idx_;
293 static const unsigned int max_n_childs=CodePoint::max_n_timer_childs;
298 Timer(
const CodePoint &cp,
int parent);
312 bool stop(
bool forced =
false);
316 inline string tag()
const {
317 string buf(code_point_->tag_);
318 buf.append(code_point_->subtag_);
323 inline bool running()
const 324 {
return start_count >0; }
327 std::string code_point_str()
const;
332 double cumulative_time()
const;
337 void add_child(
int child_index,
const Timer &child);
367 unsigned int call_count;
371 unsigned int start_count;
378 const CodePoint *code_point_;
380 unsigned int full_hash_;
382 unsigned int hash_idx_;
391 int child_timers[max_n_childs];
397 size_t total_allocated_;
402 size_t total_deallocated_;
408 size_t max_allocated_;
415 size_t current_allocated_;
426 #ifdef FLOW123D_HAVE_PETSC 430 PetscLogDouble petsc_start_memory;
434 PetscLogDouble petsc_end_memory;
438 PetscLogDouble petsc_memory_difference;
446 PetscLogDouble petsc_peak_memory;
452 PetscLogDouble petsc_local_peak_memory;
453 #endif // FLOW123D_HAVE_PETSC 524 static Profiler* instance(
bool clear =
false);
531 void set_task_info(
string description,
int size);
537 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build);
545 int start_timer(
const CodePoint &cp);
553 void stop_timer(
const CodePoint &cp);
561 void stop_timer(
int timer_index = -1);
567 void add_calls(
unsigned int n_calls);
572 void notify_malloc(
const size_t size,
const long p);
577 void notify_free(
const long p);
583 static double get_resolution ();
586 #ifdef FLOW123D_HAVE_MPI 598 void output(
MPI_Comm comm, std::ostream &os);
604 void output(
MPI_Comm comm,
string profiler_path =
"");
617 void output(std::ostream &os);
623 void output(
string profiler_path =
"");
628 void transform_profiler_data (
const string &output_file_suffix,
const string &formatter);
633 static void uninitialize();
639 static void*
operator new (
size_t sz);
645 static void operator delete (
void* p);
652 void static set_memory_monitoring(
const bool global_monitor,
const bool petsc_monitor);
658 bool static get_global_memory_monitoring();
664 bool static get_petsc_memory_monitoring();
678 static bool global_monitor_memory;
683 static bool petsc_monitor_memory;
691 static const long malloc_map_reserve;
696 void propagate_timers ();
701 void accept_from_child (
Timer &parent,
Timer &child);
707 int find_child(
const CodePoint &cp);
721 std::shared_ptr<std::ostream> get_default_output_stream();
727 unsigned int actual_node;
745 string task_description_;
752 string flow_version_;
756 string flow_revision_;
760 string json_filepath;
768 template<
typename ReduceFunctor>
769 void add_timer_info(ReduceFunctor reduce,
nlohmann::json* node,
int timer_idx,
double parent_time);
799 int const timer_index_;
801 inline TimerFrame(
const CodePoint &cp)
825 static unordered_map_with_alloc & malloc_map();
831 #else // FLOW123D_DEBUG_PROFILER 837 static Profiler* instance(
bool clear =
false);
841 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build)
861 static void uninitialize();
a class to store JSON values
double get_resolution() const
unsigned int actual_count() const
double actual_cumulative_time() const
void notify_free(const size_t size)
void output(MPI_Comm comm)
Global macros to enhance readability and debugging, general constants.
void set_program_info(string program_name, string program_version, string branch, string revision, string build)
STREAM & operator<<(STREAM &s, UpdateFlags u)
void transform_profiler_data(const string &output_file_suffix, const string &formatter)
static Profiler * instance(bool clear=false)
void output(MPI_Comm comm, ostream &os)
void set_task_info(string description, int size)
void notify_malloc(const size_t size)
const char * actual_tag() const