-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-granularity.h
166 lines (143 loc) · 4.9 KB
/
task-granularity.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
#if _LIBGOMP_TASK_GRANULARITY_
/* Header file for tracking tasks granularity at run-time. */
#define HTABLE_N 251ULL
#define HTABLE_A 13ULL
#define HTABLE_B 5ULL
#ifndef RDTSC
#define RDTSC() ({ \
unsigned int cycles_low; \
unsigned int cycles_high; \
asm volatile ( \
"RDTSC\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t" \
: \
"=r" (cycles_high), "=r" (cycles_low) \
: \
: \
"%rax", "%rdx" \
); \
(((uint64_t) cycles_high << 32) | cycles_low); \
})
#endif
extern struct gomp_task_icv gomp_global_icv;
struct gomp_task_granularity
{
uint64_t num;
uint64_t sum;
};
struct gomp_task_granularity_node
{
struct gomp_task_granularity_node *next;
void (*fn) (void *);
struct gomp_task_granularity task_granularity;
};
struct gomp_task_granularity_table
{
struct gomp_task_granularity_node *task_granularity_list[HTABLE_N];
};
static inline __attribute__((always_inline)) void
gomp_init_task_granularity_table (struct gomp_task_granularity_table **table)
{
if ((*table) == NULL)
(*table) = gomp_malloc (sizeof(struct gomp_task_granularity_table));
memset((*table), 0, sizeof(struct gomp_task_granularity_table));
}
static inline __attribute__((always_inline)) struct gomp_task_granularity_node *
gomp_alloc_task_granularity_node (struct gomp_task_granularity_node *next, void (*fn) (void *), uint64_t occur, uint64_t time)
{
struct gomp_task_granularity_node *node = gomp_malloc (sizeof(struct gomp_task_granularity_node));
node->next = next;
node->fn = fn;
node->task_granularity.num = occur;
node->task_granularity.sum = time;
return node;
}
static inline __attribute__((always_inline)) void
gomp_save_task_granularity (struct gomp_task_granularity_table *table, void (*fn) (void *), uint64_t time)
{
uint64_t key;
struct gomp_task_granularity_node *node;
key = (((((uint64_t) fn) << 4) * HTABLE_A) + HTABLE_B) % HTABLE_N;
if ((node = table->task_granularity_list[key]) != NULL)
{
do
{
if (node->fn == fn)
{
node->task_granularity.num += 1ULL;
node->task_granularity.sum += time;
return;
}
node = node->next;
}
while (node != NULL);
}
table->task_granularity_list[key] = gomp_alloc_task_granularity_node (table->task_granularity_list[key], fn, 1ULL, time);
}
static inline __attribute__((always_inline)) void
gomp_save_cumulative_granularities (struct gomp_task_granularity_table *table, void (*fn) (void *), uint64_t num, uint64_t sum)
{
uint64_t key;
struct gomp_task_granularity_node *node;
key = (((((uint64_t) fn) << 4) * HTABLE_A) + HTABLE_B) % HTABLE_N;
if ((node = table->task_granularity_list[key]) != NULL)
{
do
{
if (node->fn == fn)
{
node->task_granularity.num += num;
node->task_granularity.sum += sum;
return;
}
node = node->next;
}
while (node != NULL);
}
table->task_granularity_list[key] = gomp_alloc_task_granularity_node (table->task_granularity_list[key], fn, num, sum);
}
static inline __attribute__((always_inline)) void
gomp_print_task_granularity (struct gomp_task_granularity_table **table, unsigned nthreads)
{
unsigned tid;
uint64_t key;
struct gomp_task_granularity_node *node;
size_t length;
time_t rawtime;
struct tm *timeinfo;
char filename[128];
for (tid = 1; tid < nthreads; tid++)
for (key = 0; key < HTABLE_N; key++)
if ((node = table[tid]->task_granularity_list[key]) != NULL)
do
{
gomp_save_cumulative_granularities (table[0], node->fn, node->task_granularity.num, node->task_granularity.sum);
table[tid]->task_granularity_list[key] = node->next;
free (node);
node = table[tid]->task_granularity_list[key];
}
while (node != NULL);
time(&rawtime);
timeinfo = localtime(&rawtime);
if (gomp_global_icv.ult_var)
length = strftime(filename, 128, "ult_omp_task_granularity_%d-%m-%Y_%H-%M-%S-", timeinfo);
else
length = strftime(filename, 128, "bsl_omp_task_granularity_%d-%m-%Y_%H-%M-%S-", timeinfo);
snprintf(&filename[length], (128 - length), "(%llu).txt", (unsigned long long int) RDTSC());
FILE *file = fopen(filename, "w");
for (key = 0; key < HTABLE_N; key++)
if ((node = table[0]->task_granularity_list[key]) != NULL)
do
{
fprintf(file, "\nFunction: %p\nNumber: %llu\nSum: %llu\nMean: %.0f\n\n", node->fn, (unsigned long long int) node->task_granularity.num,
(unsigned long long int) node->task_granularity.sum, ((double) node->task_granularity.sum / (double) node->task_granularity.num));
table[0]->task_granularity_list[key] = node->next;
free (node);
node = table[0]->task_granularity_list[key];
}
while (node != NULL);
fflush(file);
fclose(file);
}
#endif