-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.hpp
454 lines (408 loc) · 9.92 KB
/
timer.hpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#pragma once
#include <cstdio>
#include <cstdint>
// Default take statistics
// #define TIMER_VERBOSE // save all the timing for visualization
// #define TIMER_SILENT // Disable all timer
#ifdef TIMER_VERBOSE
#include <vector>
# define TIMER_NUM 2
#elif defined(TIMER_SILENT)
# define TIMER_NUM 0
#else
# define TIMER_NUM 1
#endif
extern "C" int MPI_Get_processor_name(char *, int *);
#ifdef __aarch64__
static int64_t get_utime(){
uint64_t tsc;
asm volatile ("mrs %0, cntvct_el0" : "=r" (tsc));
return tsc;
}
static double tick2second(uint64_t tick){
auto frequency = []{
uint64_t frq;
asm volatile ("mrs %0, cntfrq_el0" : "=r" (frq));
return frq;
};
static double invfreq = 1.0 / frequency();
return invfreq * (double)tick;
}
#else
# ifdef __APPLE__
# include <sys/time.h>
static int64_t get_utime(){
timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec*1000ll + tv.tv_sec*1000000000ll;
}
# elif defined __linux__
# include <time.h>
static int64_t get_utime(){
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_nsec + ts.tv_sec*1000000000ll;
}
# endif
static double tick2second(uint64_t tick){
return 1.e-9 * (double)tick;
}
#endif
struct Timer_template_base {
enum Items{
DIAG_BCAST = 0,
LCOL_BCAST,
RROW_BCAST,
TEST,
WAIT,
DIAG_LU,
TRSM_L,
TRSM_R,
CONV_L,
CONV_R,
GEMM_UPDATE,
GEMM_PROGRESS,
LAZY_INIT,
WRITE_BACK,
IR_GEMV,
IR_GEMV_COMM,
IR_TRSV,
IR_TRSV_MV,
IR_TRSV_COMM,
// don't touch below
TOTAL,
MISC,
NUM_ITEMS,
};
};
template <int>
struct Timer_template: Timer_template_base {
// caleld fromo initialize()
static void flush(){}
// initialize timer with 0
static void initialize(){}
// start a region
// elem: which item to collect
// reuse: reuse last timing for reduce timer cost.
static void beg(const Items /*elem*/, bool const /*reuse*/ = false){}
// stop a region
// elem: which item to collect
// reuse: reuse last timing for reduce timer cost.
// acc: number of operations in this region. ex. flops, mips, or, bytes
static void end(const Items /*elem*/, bool const /*reuse*/ = false, int64_t /*acc*/ = 0ll){}
// save a timing
static double put(const Items /*elem*/, bool const /*reuse*/ = false){ return 0.0; }
// dump out all the data
static void show(FILE* /*fp*/ = stderr, char const */*fmt*/ = ""){}
// open file and call show()
// size: number of procs
// rank: process id
// row : row id
// col : col id
// filename : filename
static void dump_mp( const int /*size*/, const int /*rank*/,
const int /*row*/, const int /*col*/, const char* /*filename*/ = ""){} // do nothing
};
template<>
struct Timer_template<1> : Timer_template_base{
static char const *name(int const i){
static const char *strs[NUM_ITEMS] = {
"DIAG_BCAST",
"LCOL_BCAST",
"RROW_BCAST",
"TEST",
"WAIT",
"DIAG_LU",
"TRSM_L",
"TRSM_R",
"CONV_L",
"CONV_R",
"GEMM_UPDATE",
"GEMM_PROGRESS",
"LAZY_INIT",
"WRITE_BACK",
"IR_GEMV",
"IR_GEMV_COMM",
"IR_TRSV",
"IR_TRSV_MV",
"IR_TRSV_COMM",
"TOTAL",
"MISC",
};
return strs[i];
}
static void flush(){
for(int i=0; i<NUM_ITEMS; i++){
time (i) = 0ll;
accum(i) = 0ll;
}
}
static void initialize(){
flush();
tprev(1) = get_utime();
}
static void beg(const Items elem, bool const reuse = false){
if(reuse) time(elem) -= tprev();
else time(elem) -= (tprev() = get_utime());
}
static void end(const Items elem, bool const reuse = false){
if(reuse) time(elem) += tprev();
else time(elem) += (tprev() = get_utime());
}
static void end(const Items elem, bool const reuse, int64_t acc){
end(elem, reuse);
accum(elem) += acc;
}
static double put(const Items /*elem*/, bool const reuse = false){
uint64_t tt = reuse ? get_utime() : tprev();
return tick2second(tt - tprev(1));
}
static void show(
FILE *fp = stderr,
const char *fmt = " %-12s : %e sec : %6.2f %% : %20lld : %e Gop/s\n")
{
fflush(fp);
time(MISC) = time(TOTAL);
for(int i=0; i<NUM_ITEMS-2; i++){
time(MISC) -= time(i);
}
for(int i=0; i<NUM_ITEMS; i++){
fprintf(fp, fmt, name(i),
rtime(i),
100.0 * time(i)/ time(TOTAL),
accum(i),
1e-9*accum(i) / rtime(i));
}
const double flops = (double)(accum(GEMM_UPDATE)+accum(GEMM_PROGRESS)) /
(rtime(GEMM_UPDATE)+rtime(GEMM_PROGRESS));
fprintf(fp, "GEMM_TOTAL: %f Tflops\n", 1.e-12 * flops);
fflush(fp);
}
static void dump_mp(
const int size,
const int rank,
const int row,
const int col,
const char *fmt = "Timerdump.%04d.%04d")
{
static char filename[1024];
sprintf(filename, fmt, size, rank);
FILE *fp = fopen(filename, "w");
if(fp){
int len;
MPI_Get_processor_name(filename, &len);
fprintf(fp, "# row=%d, col=%d, host=%s\n", row, col, filename);
show(fp);
fclose(fp);
}
}
private:
static int64_t &time(int const i){
static int64_t buf[NUM_ITEMS];
return buf[i];
}
static int64_t &accum(int const i){
static int64_t buf[NUM_ITEMS];
return buf[i];
}
static int64_t &tprev(int const ch=0){
static int64_t t[2]; /* 0 : previous time */
/* 1 : initial time */
return t[ch];
}
static double rtime(int const i){
return tick2second(time(i));
}
};
#ifdef TIMER_VERBOSE
// pritout all the timings with binary format
template<>
struct Timer_template<2> : Timer_template_base{
static char const *name(int const i){
static const char *strs[NUM_ITEMS] = {
"DIAG_BCAST",
"LCOL_BCAST",
"RROW_BCAST",
"TEST",
"WAIT",
"DIAG_LU",
"TRSM_L",
"TRSM_R",
"CONV_L",
"CONV_R",
"GEMM_UPDATE",
"GEMM_PROGRESS",
"LAZY_INIT",
"WRITE_BACK",
"IR_GEMV",
"IR_GEMV_COMM",
"IR_TRSV",
"IR_TRSV_MV",
"IR_TRSV_COMM",
"TOTAL",
"MISC",
};
return strs[i];
}
static void flush(){
for(int i=0; i<NUM_ITEMS; i++){
time(i) = 0ll;
accum(i) = 0ll;
tvec_beg(i).clear();
tvec_end(i).clear();
tvec_put(i).clear();
avec(i).clear();
}
}
static void initialize(){
flush();
for(int i=0; i<NUM_ITEMS; i++){
tvec_beg(i).reserve(10000);
tvec_end(i).reserve(10000);
tvec_put(i).reserve(10000);
avec(i).reserve(10000);
}
tprev(1) = get_utime();
}
static void beg(const Items elem, bool const reuse = false){
//fprintf(stderr, "%s: DEBUG BEG %s\n", hostname(), name(elem)); fflush(stderr);
if(reuse) time(elem) -= tprev();
else time(elem) -= (tprev() = get_utime());
tvec_beg(elem).push_back(tprev());
}
static void end(const Items elem, bool const reuse = false, int64_t acc=0ll){
//fprintf(stderr, "%s: DEBUG END %s\n", hostname(), name(elem)); fflush(stderr);
if(reuse) time(elem) += tprev();
else time(elem) += (tprev() = get_utime());
accum(elem) += acc;
tvec_end(elem).push_back(tprev());
avec(elem).push_back(acc);
}
static double put(const Items elem, bool const reuse = false){
uint64_t tt = reuse ? get_utime() : tprev();
tvec_put(elem).push_back(tt);
return tick2second(tt - tprev(1));
}
static void show(
FILE *fp = stderr,
const char *fmt = " %-12s : %e sec : %6.2f %% : %20ld : %e Gop/s\n",
FILE *fp2 = nullptr)
{
fflush(fp);
time(MISC) = time(TOTAL);
for(int i=0; i<NUM_ITEMS-2; i++){
time(MISC) -= time(i);
}
for(int i=0; i<NUM_ITEMS; i++){
fprintf(fp, fmt, name(i),
rtime(i),
100.0 * time(i)/ time(TOTAL),
accum(i),
1e-9*accum(i)/rtime(i));
}
const double flops = (double)(accum(GEMM_UPDATE)+accum(GEMM_PROGRESS)) /
(rtime(GEMM_UPDATE)+rtime(GEMM_PROGRESS));
fprintf(fp, "GEMM_TOTAL: %f Tflops\n", 1.e-12 * flops);
fflush(fp);
// dump event vectors
if(!fp2) fp2 = fp;
for(int i=0; i<NUM_ITEMS; i++){
dump_vector(fp2, tvec_beg(i), "BEG_", name(i));
dump_vector(fp2, tvec_end(i), "END_", name(i));
dump_vector(fp2, tvec_put(i), "PUT_", name(i));
dump_accum( fp2, avec(i), "ACC_", name(i));
}
fflush(fp);
}
static void dump_mp(
const int /*size*/,
const int rank,
const int row,
const int col,
const char *filename)
{
fprintf(stderr, "%d: (%d, %d)\n", rank, row, col);
FILE *fp = fopen(filename, "w");
if(fp){
int len;
char hostname[1024];
MPI_Get_processor_name(hostname, &len);
fprintf(fp, "# row=%d, col=%d, host=%s\n", row, col, hostname);
show(fp);
fclose(fp);
}
}
static const char *hostname(){
static char name[1024] = {0,};
if(!name[0]){
int len;
MPI_Get_processor_name(name, &len);
}
return name;
}
private:
static int64_t &time(int const i){
static int64_t buf[NUM_ITEMS];
return buf[i];
}
static int64_t &accum(int const i){
static int64_t buf[NUM_ITEMS];
return buf[i];
}
using tvec = std::vector<int64_t>;
static tvec &tvec_beg(int const i){
static tvec buf[NUM_ITEMS];
return buf[i];
}
static tvec &tvec_end(int const i){
static tvec buf[NUM_ITEMS];
return buf[i];
}
static tvec &tvec_put(int const i){
static tvec buf[NUM_ITEMS];
return buf[i];
}
static tvec &avec(int const i){
static tvec buf[NUM_ITEMS];
return buf[i];
}
static void dump_vector(FILE *fp,const tvec &v, const char *s0, const char *s1){
const int n = v.size();
if(fp!=stderr){
fprintf(fp, "bio, %d, %s%s\n", n, s0, s1);
for(int i=0; i<n; i++){
unsigned long utime = v[i];
double dtime = tick2second(utime - tprev(1));
fwrite(&dtime, sizeof(double), 1, fp);
}
}
else {
for(int i=0; i<n; i++){
unsigned long utime = v[i];
double dtime = tick2second(utime - tprev(1));
fprintf(fp,"%ld, %16.12f, %s%s, %d\n", utime, dtime, s0, s1, i);
}
}
}
static void dump_accum(FILE *fp,const tvec &v, const char *s0, const char *s1){
const int n = v.size();
if(fp!=stderr){
fprintf(fp, "bio, %d, %s%s\n", n, s0, s1);
fwrite(v.data(), sizeof(int64_t), n, fp);
}
else {
for(int i=0; i<n; i++){
fprintf(fp,"%ld, 0.0, %s%s, %d\n", v[i], s0, s1, i);
}
}
}
static int64_t &tprev(int const ch=0){
static int64_t t[2];
return t[ch];
}
static double rtime(int const i){
return tick2second(time(i));
}
};
#endif
using Timer = Timer_template<TIMER_NUM>;