-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbenchmark.c
273 lines (244 loc) · 11.6 KB
/
benchmark.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
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
// gcc -o benchmark benchmark.c -mbmi2 -mavx2 -O3 && ./benchmark
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "simdprune.h"
#ifndef __SSE3__
#error "minimal feature level (SSE3) not met. try compiling with -march=native"
#endif
#define RDTSC_START(cycles) \
do { \
uint32_t cyc_high, cyc_low; \
__asm volatile("cpuid\n" \
"rdtsc\n" \
"mov %%edx, %0\n" \
"mov %%eax, %1" : \
"=r" (cyc_high), \
"=r"(cyc_low) : \
: /* no read only */ \
"%rax", "%rbx", "%rcx", "%rdx" /* clobbers */ \
); \
(cycles) = ((uint64_t)cyc_high << 32) | cyc_low; \
} while (0)
#define RDTSC_STOP(cycles) \
do { \
uint32_t cyc_high, cyc_low; \
__asm volatile("rdtscp\n" \
"mov %%edx, %0\n" \
"mov %%eax, %1\n" \
"cpuid" : \
"=r"(cyc_high), \
"=r"(cyc_low) : \
/* no read only registers */ : \
"%rax", "%rbx", "%rcx", "%rdx" /* clobbers */ \
); \
(cycles) = ((uint64_t)cyc_high << 32) | cyc_low; \
} while (0)
static __attribute__ ((noinline))
uint64_t rdtsc_overhead_func(uint64_t dummy) {
return dummy;
}
uint64_t global_rdtsc_overhead = (uint64_t) UINT64_MAX;
#define RDTSC_SET_OVERHEAD(test, repeat) \
do { \
uint64_t cycles_start, cycles_final, cycles_diff; \
uint64_t min_diff = UINT64_MAX; \
for (int i = 0; i < repeat; i++) { \
__asm volatile("" ::: /* pretend to clobber */ "memory"); \
RDTSC_START(cycles_start); \
test; \
RDTSC_STOP(cycles_final); \
cycles_diff = (cycles_final - cycles_start); \
if (cycles_diff < min_diff) min_diff = cycles_diff; \
} \
global_rdtsc_overhead = min_diff; \
printf("rdtsc_overhead set to %d\n", (int)global_rdtsc_overhead); \
} while (0) \
/*
* Prints the best number of operations per cycle where
* test is the function call, answer is the expected answer generated by
* test, repeat is the number of times we should repeat and size is the
* number of operations represented by test.
*/
#define BEST_TIME(test, expected, pre, repeat, size, verbose) \
do { \
if (global_rdtsc_overhead == UINT64_MAX) { \
RDTSC_SET_OVERHEAD(rdtsc_overhead_func(1), repeat); \
} \
if(verbose) printf("%-60s\t: ", #test); \
fflush(NULL); \
uint64_t cycles_start, cycles_final, cycles_diff; \
uint64_t min_diff = (uint64_t)-1; \
uint64_t sum_diff = 0; \
for (int i = 0; i < repeat; i++) { \
pre; \
__asm volatile("" ::: /* pretend to clobber */ "memory"); \
RDTSC_START(cycles_start); \
if(test != expected) {printf("not expected (%d , %d )",(int)test,(int)expected);break;} \
RDTSC_STOP(cycles_final); \
cycles_diff = (cycles_final - cycles_start - global_rdtsc_overhead); \
if (cycles_diff < min_diff) min_diff = cycles_diff; \
sum_diff += cycles_diff; \
} \
uint64_t S = size; \
float cycle_per_op = (min_diff) / (double)S; \
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
if(verbose) printf(" %.3f cycles per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.3f cycles per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf("\n"); \
if(!verbose) printf(" %.3f ",cycle_per_op); \
fflush(NULL); \
} while (0)
// like BEST_TIME, but no check
#define BEST_TIME_NOCHECK(test, pre, repeat, size, verbose) \
do { \
if (global_rdtsc_overhead == UINT64_MAX) { \
RDTSC_SET_OVERHEAD(rdtsc_overhead_func(1), repeat); \
} \
if(verbose) printf("%-60s\t: ", #test); \
fflush(NULL); \
uint64_t cycles_start, cycles_final, cycles_diff; \
uint64_t min_diff = (uint64_t)-1; \
uint64_t sum_diff = 0; \
for (int i = 0; i < repeat; i++) { \
pre; \
__asm volatile("" ::: /* pretend to clobber */ "memory"); \
RDTSC_START(cycles_start); \
test; \
RDTSC_STOP(cycles_final); \
cycles_diff = (cycles_final - cycles_start - global_rdtsc_overhead); \
if (cycles_diff < min_diff) min_diff = cycles_diff; \
sum_diff += cycles_diff; \
} \
uint64_t S = size; \
float cycle_per_op = (min_diff) / (double)S; \
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
if(verbose) printf(" %.3f cycles per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.3f cycles per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf("\n"); \
if(!verbose) printf(" %.3f ",cycle_per_op); \
fflush(NULL); \
} while (0)
__attribute__ ((noinline))
void randomize(int * bitmasks, int N, int mask) {
for (int k = 0; k < N; k++) {
bitmasks[k] = rand() & mask;
}
}
__attribute__ ((noinline))
__m128i runprune_epi8(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = prune_epi8(*x, bitmasks[k]);
}
return *x;
}
__attribute__ ((noinline))
__m128i runthinprune_epi8(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = thinprune_epi8(*x, bitmasks[k]);
}
return *x;
}
__attribute__ ((noinline))
__m128i runskinnyprune_epi8(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = skinnyprune_epi8(*x, bitmasks[k]);
}
return *x;
}
#ifdef __AVX512VBMI2__
__attribute__ ((noinline))
__m128i runbmiprune_epi8(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = bmiprune_epi8(*x, ~bitmasks[k]);
}
return *x;
}
#endif //
__attribute__ ((noinline))
__m128i runprune_epi16(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = prune_epi16(*x, bitmasks[k]);
}
return *x;
}
__attribute__ ((noinline))
__m128i runprune_epi32(int * bitmasks, int N, __m128i *x) {
for (int k = 0; k < N; k++) {
*x = prune_epi32(*x, bitmasks[k]);
}
return *x;
}
#ifdef __AVX2__
__attribute__ ((noinline))
__m256i runprune256_epi32(int * bitmasks, int N, __m256i *x) {
for (int k = 0; k < N; k++) {
*x = prune256_epi32(*x, bitmasks[k]);
}
return *x;
}
__m256i runpext_prune256_epi32(int * bitmasks, int N, __m256i *x) {
for (int k = 0; k < N; k++) {
*x = pext_prune256_epi32(*x, bitmasks[k]);
}
return *x;
}
#endif // __AVX2__
void print_epi8(__m128i x) {
uint8_t buffer[16];
_mm_storeu_si128((__m128i*)buffer,x);
for(size_t i = 0; i < 16; i++) {
printf("%u ", buffer[i]);
}
printf("\n");
}
void check_prune_epi8(__m128i input, __m128i output, int mask) {
uint8_t buffer[16];
_mm_storeu_si128((__m128i*)buffer,input);
size_t pos = 0;
uint8_t outbuffer[16];
_mm_storeu_si128((__m128i*)outbuffer,output);
for(size_t i = 0; i < 16; i++) {
if((mask & (1<<i)) == 0) {
assert(buffer[i] == outbuffer[pos]);
pos ++;
}
}
}
void testepi8() {
for(int mask = 0; mask < (1<<16); mask++) {
__m128i source = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
__m128i m1 = prune_epi8(source, mask);
check_prune_epi8(source, m1, mask);
__m128i m2 = thinprune_epi8(source, mask);
check_prune_epi8(source, m2, mask);
__m128i m3 = skinnyprune_epi8(source, mask);
check_prune_epi8(source, m3, mask);
}
}
int main() {
testepi8();
printf("This test measures the latency in CPU cycles.\n");
const int N = 2048;
int * bitmasks = malloc(sizeof(int) * N);
const int repeat = 500;
__m128i x = _mm_set_epi8(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
BEST_TIME_NOCHECK(runprune_epi8(bitmasks, N, &x), randomize(bitmasks, N, (1<<16)-1), repeat, N, true);
BEST_TIME_NOCHECK(runthinprune_epi8(bitmasks, N, &x), randomize(bitmasks, N, (1<<16)-1), repeat, N, true);
BEST_TIME_NOCHECK(runskinnyprune_epi8(bitmasks, N, &x), randomize(bitmasks, N, (1<<16)-1), repeat, N, true);
#ifdef __AVX512VBMI2__
BEST_TIME_NOCHECK(runbmiprune_epi8(bitmasks, N, &x), randomize(bitmasks, N, (1<<16)-1), repeat, N, true);
#endif // __AVX512VBMI2__
BEST_TIME_NOCHECK(runprune_epi16(bitmasks, N, &x), randomize(bitmasks, N, (1<<8)-1), repeat, N, true);
BEST_TIME_NOCHECK(runprune_epi32(bitmasks, N, &x), randomize(bitmasks, N, (1<<4)-1), repeat, N, true);
#ifdef __AVX2__
__m256i xx = _mm256_set_epi32(7,6,5,4,3,2,1,0);
BEST_TIME_NOCHECK(runprune256_epi32(bitmasks, N, &xx), randomize(bitmasks, N, (1<<8)-1), repeat, N, true);
BEST_TIME_NOCHECK(runpext_prune256_epi32(bitmasks, N, &xx), randomize(bitmasks, N, (1<<8)-1), repeat, N, true);
printf("%d \n", _mm_extract_epi8(x,0) + _mm256_extract_epi8(xx,0));
#endif // __AVX2__
free(bitmasks);
return EXIT_SUCCESS;
}