1 #ifndef OMEGA_H_STACK_HPP
2 #define OMEGA_H_STACK_HPP
4 #include <Omega_h_timer.hpp>
5 #include <Omega_h_filesystem.hpp>
9 #ifdef OMEGA_H_USE_KOKKOS
10 #include <Omega_h_kokkos.hpp>
16 typedef std::shared_ptr<Comm> CommPtr;
21 std::vector<char> chars;
22 std::size_t save(
char const* str) {
23 auto ret = chars.size();
25 chars.push_back(*str);
26 if (*str ==
'\0')
break;
31 char const* get(std::size_t i)
const {
return &chars[i]; }
34 static constexpr std::size_t invalid = std::numeric_limits<std::size_t>::max();
38 std::size_t first_child;
39 std::size_t last_child;
40 std::size_t next_sibling;
44 std::size_t number_of_calls;
48 std::vector<Frame> frames;
49 std::size_t current_frame;
50 std::size_t last_root;
57 History(CommPtr comm =
nullptr,
bool dopercent=
false,
double chop=0.0,
bool add_filename=
false);
59 inline const char* get_name(std::size_t frame)
const {
60 return names.get(frames[frame].name_ptr);
62 inline std::size_t find_child_of(
63 std::size_t parent_index,
char const* name)
const {
64 if (parent_index == invalid)
return find_root(name);
65 for (std::size_t child = frames[parent_index].first_child; child != invalid;
66 child = frames[child].next_sibling) {
67 if (0 == std::strcmp(get_name(child), name)) {
73 inline std::size_t create_child_of(
74 std::size_t parent_index,
char const* name) {
75 if (parent_index == invalid)
return create_root(name);
76 auto index = frames.size();
77 frames.push_back(
Frame());
78 auto& frame = frames.back();
79 auto& parent_frame = frames[parent_index];
80 frame.parent = parent_index;
81 frame.first_child = invalid;
82 frame.last_child = invalid;
83 auto old_last = parent_frame.last_child;
84 if (old_last != invalid) {
85 frames[old_last].next_sibling = index;
87 parent_frame.first_child = index;
89 frame.next_sibling = invalid;
90 parent_frame.last_child = index;
91 frame.name_ptr = names.save(name);
92 frame.total_runtime = 0.0;
93 frame.number_of_calls = 0;
96 inline std::size_t create_child_of_current(
char const* name) {
97 return create_child_of(current_frame, name);
99 inline std::size_t find_root(
char const* name)
const {
100 if (frames.empty())
return invalid;
101 for (std::size_t i = 0; i != invalid; i = frames[i].next_sibling) {
102 if (frames[i].parent == invalid &&
103 (0 == std::strcmp(get_name(i), name))) {
109 inline std::size_t create_root(
char const* name) {
110 auto index = frames.size();
111 frames.push_back(
Frame());
112 auto& frame = frames.back();
113 frame.parent = invalid;
114 frame.first_child = invalid;
115 frame.last_child = invalid;
117 frames[last_root].next_sibling = index;
120 frame.next_sibling = invalid;
121 frame.name_ptr = names.save(name);
122 frame.total_runtime = 0.0;
123 frame.number_of_calls = 0;
126 inline std::size_t find(
char const* name) {
127 return find_child_of(current_frame, name);
129 inline std::size_t create(
char const* name) {
130 return create_child_of(current_frame, name);
132 inline std::size_t find_or_create(
char const* name) {
133 return find_or_create_child_of(current_frame, name);
135 inline std::size_t find_or_create_child_of(
136 std::size_t parent_index,
char const* name) {
137 auto found = find_child_of(parent_index, name);
138 if (found != invalid)
return found;
139 return create_child_of(parent_index, name);
141 inline std::size_t push(
char const* name) {
142 std::size_t
id = find_or_create(name);
146 inline void pop() { current_frame = frames[current_frame].parent; }
147 inline void start(
char const*
const name) {
148 auto id = push(name);
149 frames[id].number_of_calls += 1;
150 frames[id].start_time = now();
152 inline double measure_runtime() {
153 auto current_time = now();
154 auto current_runtime = current_time - frames[current_frame].start_time;
155 return current_runtime;
157 inline double measure_total_runtime() {
158 return frames[current_frame].total_runtime + measure_runtime();
161 frames[current_frame].total_runtime = measure_total_runtime();
164 std::size_t first(std::size_t parent)
const;
165 std::size_t next(std::size_t sibling)
const;
166 std::size_t parent(std::size_t child)
const;
167 std::size_t pre_order_next(std::size_t frame)
const;
168 double time(std::size_t frame)
const;
169 std::size_t calls(std::size_t frame)
const;
172 OMEGA_H_DLL
extern History* global_singleton_history;
176 void print_time_sorted(
History const& h);
177 void print_top_down_and_bottom_up(
History const& h,
double total_runtime);
178 void print_top_sorted(
History const& h,
double total_runtime);
185 inline void begin_code(
char const* name,
char const* file=0) {
186 #ifdef OMEGA_H_USE_KOKKOS
187 Kokkos::Profiling::pushRegion(name);
189 if (profile::global_singleton_history) {
193 if (profile::global_singleton_history->add_filename) {
195 profile::global_singleton_history->start(str.c_str());
197 profile::global_singleton_history->start(name);
202 inline double get_runtime () {
203 double runtime = 0.0;
204 if (profile::global_singleton_history) {
205 runtime = profile::global_singleton_history->measure_total_runtime();
210 inline void end_code() {
211 #ifdef OMEGA_H_USE_KOKKOS
212 Kokkos::Profiling::popRegion();
214 if (profile::global_singleton_history) {
215 profile::global_singleton_history->stop();
220 ScopedTimer(
char const* name,
char const *file=0) { begin_code(name, file); }
226 inline double total_runtime() {
return get_runtime(); }
231 #define OMEGA_H_TIME_FUNCTION \
232 ::Omega_h::ScopedTimer omega_h_scoped_function_timer(__FUNCTION__, (std::string(__FILE__)+":"+std::to_string(__LINE__)).c_str())
Definition: Omega_h_filesystem.hpp:22
Definition: amr_mpi_test.cpp:6
Definition: Omega_h_timer.hpp:10
Definition: Omega_h_profile.hpp:219
Definition: Omega_h_profile.hpp:36
Definition: Omega_h_profile.hpp:47
Definition: Omega_h_profile.hpp:20