-
Notifications
You must be signed in to change notification settings - Fork 6
/
timer.c
85 lines (65 loc) · 1.75 KB
/
timer.c
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
//
// Measures the run time
//
#include <sys/time.h>
#include "msg.h"
#include "timer.h"
#define nCategory 4
#define nSubCategory 14
static const char * CatName[]= {"Init", "2LPT", "COLA", "Snapshot"};
static const char * SubName[]= {"", "fft", "assign", "force_mesh", "pforce", "check", "comm", "evolve", "write", "kd_build", "kd_link", "interpolate", "global", "smalldata"};
static int initialized= 0;
static enum Category Cat;
static double Time[nCategory][nSubCategory], tBegin[nCategory][nSubCategory];
static double now()
{
struct timeval tp;
gettimeofday(&tp, 0);
return (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6;
}
void timer_init()
{
for(int i=0; i<nCategory; i++) {
for(int j=0; j<nCategory; j++) {
Time[i][j]= 0.0;
tBegin[i][j]= 0.0;
}
}
initialized= 1;
}
void timer_set_category(enum Category new_cat)
{
double nw= now();
if(initialized)
Time[Cat][all] += nw - tBegin[Cat][all];
else
timer_init();
Cat= new_cat;
tBegin[Cat][all]= nw;
}
void timer_start(enum SubCategory sub)
{
tBegin[Cat][sub]= now();
}
void timer_stop(enum SubCategory sub)
{
Time[Cat][sub] += now() - tBegin[Cat][sub];
}
void timer_print()
{
timer_set_category(0);
double total= 0.0;
for(int i=0; i<nCategory; i++)
total += Time[i][0];
for(int icat=0; icat<nCategory; icat++) {
msg_printf(info, "%-16s %7.2f %4.1f%%\n",
CatName[icat], Time[icat][0], 100.0*Time[icat][0]/total);
for(int isub=1; isub<nSubCategory; isub++) {
if(Time[icat][isub] > 0.0)
msg_printf(info, " %-14s %7.2f %4.1f%%\n",
SubName[isub], Time[icat][isub], 100*Time[icat][isub]/total);
}
}
msg_printf(info, "----------------------------------\n");
msg_printf(info, "%-16s %7.2f sec\n", "Total", total);
}