-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathharness.c
219 lines (172 loc) · 4.19 KB
/
harness.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
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
#include <math.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include "bits.h"
#include "cpumap.h"
#include "benchmark.h"
#ifndef NUM_ITERS
#define NUM_ITERS 5
#endif
#ifndef MAX_PROCS
#define MAX_PROCS 512
#endif
#ifndef MAX_ITERS
#define MAX_ITERS 20
#endif
#ifndef COV_THRESHOLD
#define COV_THRESHOLD 0.02
#endif
static pthread_barrier_t barrier;
static double times[MAX_ITERS];
static double means[MAX_ITERS];
static double covs[MAX_ITERS];
static volatile int target;
static size_t elapsed_time(size_t us)
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000000 + t.tv_usec - us;
}
static double compute_mean(const double * times)
{
int i;
double sum = 0;
for (i = 0; i < NUM_ITERS; ++i) {
sum += times[i];
}
return sum / NUM_ITERS;
}
static double compute_cov(const double * times, double mean)
{
double variance = 0;
int i;
for (i = 0; i < NUM_ITERS; ++i) {
variance += (times[i] - mean) * (times[i] - mean);
}
variance /= NUM_ITERS;
double cov = sqrt(variance);;
cov /= mean;
return cov;
}
static size_t reduce_min(long val, int id, int nprocs)
{
static long buffer[MAX_PROCS];
buffer[id] = val;
pthread_barrier_wait(&barrier);
long min = LONG_MAX;
int i;
for (i = 0; i < nprocs; ++i) {
if (buffer[i] < min) min = buffer[i];
}
return min;
}
static void report(int id, int nprocs, int i, long us)
{
long ms = reduce_min(us, id, nprocs);
if (id == 0) {
times[i] = ms / 1000.0;
printf(" #%d elapsed time: %.2f ms\n", i + 1, times[i]);
if (i + 1 >= NUM_ITERS) {
int n = i + 1 - NUM_ITERS;
means[i] = compute_mean(times + n);
covs[i] = compute_cov(times + n, means[i]);
if (covs[i] < COV_THRESHOLD) {
target = i;
}
}
}
pthread_barrier_wait(&barrier);
}
static void * thread(void * bits)
{
int id = bits_hi(bits);
int nprocs = bits_lo(bits);
cpu_set_t set;
CPU_ZERO(&set);
int cpu = cpumap(id, nprocs);
CPU_SET(cpu, &set);
sched_setaffinity(0, sizeof(set), &set);
thread_init(id, nprocs);
pthread_barrier_wait(&barrier);
int i;
void * result = NULL;
for (i = 0; i < MAX_ITERS && target == 0; ++i) {
long us = elapsed_time(0);
result = benchmark(id, nprocs);
pthread_barrier_wait(&barrier);
us = elapsed_time(us);
report(id, nprocs, i, us);
}
thread_exit(id, nprocs);
return result;
}
int main(int argc, const char *argv[])
{
int nprocs = 0;
int n = 0;
/** The first argument is nprocs. */
if (argc > 1) {
nprocs = atoi(argv[1]);
}
/**
* Use the number of processors online as nprocs if it is not
* specified.
*/
if (nprocs == 0) {
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
}
if (nprocs <= 0) return 1;
else {
/** Set concurrency level. */
pthread_setconcurrency(nprocs);
}
/**
* The second argument is input size n.
*/
if (argc > 2) {
n = atoi(argv[2]);
}
pthread_barrier_init(&barrier, NULL, nprocs);
printf("===========================================\n");
printf(" Benchmark: %s\n", argv[0]);
printf(" Number of processors: %d\n", nprocs);
init(nprocs, n);
pthread_t ths[nprocs];
void * res[nprocs];
int i;
for (i = 1; i < nprocs; i++) {
pthread_create(&ths[i], NULL, thread, bits_join(i, nprocs));
}
res[0] = thread(bits_join(0, nprocs));
for (i = 1; i < nprocs; i++) {
pthread_join(ths[i], &res[i]);
}
if (target == 0) {
target = NUM_ITERS - 1;
double minCov = covs[target];
/** Pick the result that has the lowest CoV. */
int i;
for (i = NUM_ITERS; i < MAX_ITERS; ++i) {
if (covs[i] < minCov) {
minCov = covs[i];
target = i;
}
}
}
double mean = means[target];
double cov = covs[target];
int i1 = target - NUM_ITERS + 2;
int i2 = target + 1;
printf(" Steady-state iterations: %d~%d\n", i1, i2);
printf(" Coefficient of variation: %.2f\n", cov);
printf(" Number of measurements: %d\n", NUM_ITERS);
printf(" Mean of elapsed time: %.2f ms\n", mean);
printf("===========================================\n");
pthread_barrier_destroy(&barrier);
return verify(nprocs, res);
}