forked from RWTH-HPC/OTF-CPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriticalPath.h
311 lines (275 loc) · 9.14 KB
/
criticalPath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#ifndef CRITICALPATH_H
#define CRITICALPATH_H 1
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <algorithm>
#include <atomic>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <inttypes.h>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include <sys/resource.h>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#if (defined __APPLE__ && defined __MACH__)
#include <dlfcn.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <omp-tools.h>
//#define DEBUG_CLOCKS 1
#define ANALYSIS_FLAGS "OTFCPT_OPTIONS"
extern int myProcId;
extern bool useMpi;
extern double localTimeOffset;
double getTime();
double getStartTimeNoOffset(double time);
double getStopTimeNoOffset(double time);
struct THREAD_CLOCK;
extern thread_local THREAD_CLOCK *thread_local_clock;
extern ompt_finalize_tool_t critical_ompt_finalize_tool;
class AnalysisFlags {
public:
#if (LLVM_VERSION) >= 40
int flush_shadow{0};
#endif
int print_max_rss{0};
int verbose{0};
int enabled{1};
int stopped{0};
bool running{false};
int report_data_leak{0};
int ignore_serial{0};
int analyze_pattern{1};
int colorize{1};
AnalysisFlags(const char *env) {
if (env) {
std::vector<std::string> tokens;
std::string token;
std::string str(env);
std::istringstream iss(str);
while (std::getline(iss, token, ' '))
tokens.push_back(token);
for (std::vector<std::string>::iterator it = tokens.begin();
it != tokens.end(); ++it) {
#if (LLVM_VERSION) >= 40
if (sscanf(it->c_str(), "flush_shadow=%d", &flush_shadow))
continue;
#endif
if (sscanf(it->c_str(), "print_max_rss=%d", &print_max_rss))
continue;
if (sscanf(it->c_str(), "verbose=%d", &verbose))
continue;
if (sscanf(it->c_str(), "start_stopped=%d", &stopped))
continue;
if (sscanf(it->c_str(), "report_data_leak=%d", &report_data_leak))
continue;
if (sscanf(it->c_str(), "enable=%d", &enabled))
continue;
if (sscanf(it->c_str(), "colorize=%d", &colorize))
continue;
if (sscanf(it->c_str(), "ignore_serial=%d", &ignore_serial))
continue;
std::cerr << "Illegal values for ANALYSIS_OPTIONS variable: " << token
<< std::endl;
}
}
}
};
extern AnalysisFlags *analysis_flags;
template <typename T>
static void update_maximum(std::atomic<T> &maximum_value,
T const &value) noexcept {
T prev_value = maximum_value;
while (prev_value < value &&
!maximum_value.compare_exchange_weak(prev_value, value)) {
}
}
template <typename value_type>
value_type atomic_add(std::atomic<value_type> &operand,
value_type value_to_add) {
value_type old = operand.load(std::memory_order_consume);
value_type desired = old + value_to_add;
while (!operand.compare_exchange_weak(old, desired, std::memory_order_release,
std::memory_order_consume))
desired = old + value_to_add;
return desired;
}
struct SYNC_CLOCK {
std::atomic<double> useful_computation_thread{0};
std::atomic<double> useful_computation_proc{0};
std::atomic<double> useful_computation_critical{0};
std::atomic<double> outsidempi_proc{-1e50};
std::atomic<double> outsidempi_thread{-1e50};
std::atomic<double> outsidempi_critical{-1e50};
std::atomic<double> outsideomp_thread{0};
std::atomic<double> outsideomp_proc{0};
std::atomic<double> outsideomp_critical{0};
std::atomic<double> outsideomp_critical_nooffset{0};
SYNC_CLOCK(double _useful_computation)
: useful_computation_critical(_useful_computation) {}
SYNC_CLOCK(double _useful_computation, double _mpi_start_time)
: useful_computation_critical(_useful_computation),
outsidempi_proc(_mpi_start_time), outsidempi_thread(_mpi_start_time),
outsidempi_critical(_mpi_start_time) {}
SYNC_CLOCK() {}
void OmpHBefore();
void OmpHAfter();
void Print(const char *prefix1, const char *prefix2 = "") {
printf("%s %s: ucp=%lf, uct=%lf, ucc=%lf, omp=%lf, omt=%lf, omc=%lf, "
"oop=%lf, oot=%lf, ooc=%lf, oocno=%lf\n",
prefix1, prefix2, useful_computation_proc.load(),
useful_computation_thread.load(), useful_computation_critical.load(),
outsidempi_proc.load(), outsidempi_thread.load(),
outsidempi_critical.load(), outsideomp_proc.load(),
outsideomp_thread.load(), outsideomp_critical.load(),
outsideomp_critical_nooffset.load());
}
};
enum ClockContext {
CLOCK_OMP,
CLOCK_OMP_ONLY,
CLOCK_MPI,
CLOCK_MPI_ONLY,
CLOCK_ALL
};
struct MPI_COUNTS {
int send{0}, recv{0}, isend{0}, irecv{0}, coll{0}, icoll{0}, test{0}, wait{0},
pers{0};
void add(MPI_COUNTS o) {
send += o.send;
recv += o.recv;
isend += o.isend;
irecv += o.irecv;
coll += o.coll;
icoll += o.icoll;
test += o.test;
wait += o.wait;
pers += o.pers;
}
};
struct THREAD_CLOCK : public SYNC_CLOCK, MPI_COUNTS {
int thread_id{-1};
bool openmp_thread{false};
bool stopped_clock{true};
// bool stopped_mpi_clock{false};
bool stopped_mpi_clock{true};
bool stopped_omp_clock{true};
THREAD_CLOCK(int threadid, double _useful_computation,
bool _openmp_thread = false)
: SYNC_CLOCK(_useful_computation,
(!analysis_flags->running) ? 0 : -getTime()),
thread_id(threadid), openmp_thread(_openmp_thread),
stopped_mpi_clock(!analysis_flags->running) {}
THREAD_CLOCK() {}
void Stop(ClockContext cc = CLOCK_OMP, const char *loc = NULL) {
if (cc != CLOCK_OMP_ONLY && !analysis_flags->running)
return;
Stop(getTime(), cc, loc);
}
void Stop(double time, ClockContext cc = CLOCK_OMP, const char *loc = NULL) {
assert(loc);
#if DEBUG_CLOCKS
Print("Stopping at ", loc);
#endif
if (cc != CLOCK_OMP_ONLY) {
if (!analysis_flags->running)
return;
if (cc != CLOCK_MPI_ONLY) {
assert(!openmp_thread || stopped_clock == false);
stopped_clock = true;
atomic_add(useful_computation_critical, time);
atomic_add(useful_computation_thread, time);
atomic_add(useful_computation_proc, time);
}
if (cc != CLOCK_OMP) {
assert(stopped_mpi_clock == false);
stopped_mpi_clock = true;
atomic_add(outsidempi_proc, time);
atomic_add(outsidempi_critical, time);
atomic_add(outsidempi_thread, time);
}
}
if (cc != CLOCK_MPI && cc != CLOCK_MPI_ONLY) {
assert(!openmp_thread || stopped_omp_clock == false);
stopped_omp_clock = true;
atomic_add(outsideomp_thread, time);
atomic_add(outsideomp_critical, time);
atomic_add(outsideomp_critical_nooffset, getStopTimeNoOffset(time));
atomic_add(outsideomp_proc, time);
}
#if DEBUG_CLOCKS
Print("Stopped at ", loc);
#endif
}
void Start(ClockContext cc = CLOCK_OMP, const char *loc = NULL) {
if (!analysis_flags->running)
return;
Start(-getTime(), cc, loc);
}
void Start(double time, ClockContext cc = CLOCK_OMP, const char *loc = NULL) {
assert(loc);
#if DEBUG_CLOCKS
Print("Starting at ", loc);
#endif
if (cc != CLOCK_OMP_ONLY) {
if (!analysis_flags->running)
return;
if (cc != CLOCK_MPI_ONLY) {
assert(!openmp_thread || stopped_clock == true);
stopped_clock = false;
atomic_add(useful_computation_critical, time);
atomic_add(useful_computation_thread, time);
atomic_add(useful_computation_proc, time);
}
if (cc != CLOCK_OMP) {
assert(stopped_mpi_clock == true);
stopped_mpi_clock = false;
atomic_add(outsidempi_proc, time);
atomic_add(outsidempi_critical, time);
atomic_add(outsidempi_thread, time);
}
}
if (cc != CLOCK_MPI && cc != CLOCK_MPI_ONLY) {
assert(!openmp_thread || stopped_omp_clock == true);
stopped_omp_clock = false;
atomic_add(outsideomp_thread, time);
atomic_add(outsideomp_critical, time);
atomic_add(outsideomp_critical_nooffset, getStartTimeNoOffset(time));
atomic_add(outsideomp_proc, time);
}
#if DEBUG_CLOCKS
Print("Started at ", loc);
#endif
}
};
typedef SYNC_CLOCK ompt_tsan_clockid;
extern thread_local THREAD_CLOCK *thread_local_clock;
extern std::vector<THREAD_CLOCK *> thread_clocks;
extern double startProgrammTime;
extern double crit_path_useful_time;
uint64_t my_next_id();
#define stopClock(t) (t)->Stop(CLOCK_OMP, __func__)
#define startClock(t) (t)->Start(CLOCK_OMP, __func__)
#define stopMpiClock(t) (t)->Stop(CLOCK_MPI, __func__)
#define startMpiClock(t) (t)->Start(CLOCK_MPI, __func__)
#define stopOmpClock(t) (t)->Stop(CLOCK_OMP_ONLY, __func__)
void resetMpiClock(THREAD_CLOCK *thread_clock);
void startTool(bool toolControl = true, ClockContext cc = CLOCK_ALL);
void stopTool();
#define OmpHappensBefore(cv) (cv)->OmpHBefore();
#define OmpHappensAfter(cv) (cv)->OmpHAfter();
void OmpClockReset(THREAD_CLOCK *cv);
void OmpClockReset(SYNC_CLOCK *cv);
void startMeasurement(double time = getTime());
void stopMeasurement(double time = getTime());
void finishMeasurement();
#endif