-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.cpp
233 lines (187 loc) · 5.72 KB
/
bench.cpp
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
#include <immintrin.h>
#include <x86intrin.h>
#include <cpuid.h>
#include <string.h>
#include "common.hpp"
bool output_csv = false;
FILE *logs;
cpuinfo info;
/* x64 regisuter usage
* http://msdn.microsoft.com/en-US/library/9z1stfyw(v=vs.80).aspx
* RAX Volatile Return value register
* RCX Volatile First integer argument
* RDX Volatile Second integer argument
* R8 Volatile Third integer argument
* R9 Volatile Fourth integer argument
* R10:R11 Volatile Must be preserved as needed by caller; used in syscall/sysret instructions
* R12:R15 Nonvolatile Must be preserved by callee
* RDI Nonvolatile Must be preserved by callee
* RSI Nonvolatile Must be preserved by callee
* RBX Nonvolatile Must be preserved by callee
* RBP Nonvolatile May be used as a frame pointer; must be preserved by callee
* RSP Nonvolatile Stack pointer
* XMM0 Volatile First FP argument
* XMM1 Volatile Second FP argument
* XMM2 Volatile Third FP argument
* XMM3 Volatile Fourth FP argument
* XMM4:XMM5 Volatile Must be preserved as needed by caller
* XMM6:XMM15 Nonvolatile Must be preserved as needed by callee.
*/
#ifdef __linux
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <sys/eventfd.h>
static int
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags )
{
int ret;
ret = syscall( __NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags );
return ret;
}
int perf_fd;
static void
cycle_counter_init(void)
{
struct perf_event_attr attr;
memset(&attr, 0, sizeof(attr));
attr.type = PERF_TYPE_HARDWARE;
attr.size = sizeof(attr);
attr.config = PERF_COUNT_HW_CPU_CYCLES;
attr.exclude_kernel = 1;
perf_fd = perf_event_open(&attr, 0, -1, -1, 0);
if (perf_fd == -1) {
perror("perf_event_open");
exit(1);
}
}
#else
#define cycle_counter_init() ((void)0)
#endif
char MIE_ALIGN(2048*1024) zero_mem[4096*1024];
char MIE_ALIGN(2048*1024) data_mem[4096*1024];
int
main(int argc, char **argv)
{
if (argc >= 2) {
if (strcmp(argv[1],"--csv") == 0) {
output_csv = true;
}
}
cycle_counter_init();
#ifdef _WIN32
#define x_cpuid(p,eax) __cpuid(p, eax)
typedef int cpuid_t;
#else
#define x_cpuid(p,eax) __get_cpuid(eax, &(p)[0], &(p)[1], &(p)[2], &(p)[3]);
typedef unsigned int cpuid_t;
#endif
#ifdef _WIN32
std::string path = "logs/w32/";
#else
std::string path = "logs/linux/";
#endif
{
cpuid_t data[4*3+1];
char data_nospace[4*3*4+1];
x_cpuid(data+4*0, 0x80000002);
x_cpuid(data+4*1, 0x80000003);
x_cpuid(data+4*2, 0x80000004);
data[12] = 0;
puts((char*)data);
char *d0 = (char*)data;
int out = 0;
for (int i=0; i<4*3*4; i++) {
if (d0[i] != ' ') {
data_nospace[out++] = d0[i];
}
}
data_nospace[out] = '\0';
path += data_nospace;
path += ".csv";
}
logs = fopen(path.c_str(), "wb");
if (logs == NULL) {
perror(path.c_str());
return 1;
}
fprintf(logs,
"class,inst,l/t,cpi,ipc\n");
if (!output_csv) {
printf("== latency/throughput ==\n");
}
{
int reg[4];
#ifdef _WIN32
__cpuidex(reg, 7, 0);
#else
__cpuid_count(7, 0, reg[0], reg[1], reg[2], reg[3]);
#endif
if (reg[1] & (1<<5)) {
info.have_avx2 = true;
}
if (reg[1] & (1<<16)) {
info.have_avx512f = true;
}
if (reg[1] & (1<<27)) {
info.have_avx512er = true;
}
#ifdef _WIN32
__cpuid(reg, 1);
#else
__cpuid(1, reg[0], reg[1], reg[2], reg[3]);
#endif
if (reg[2] & (1<<1)) {
info.have_pclmulqdq = true;
}
if (reg[2] & (1<<12)) {
info.have_fma = true;
}
if (reg[2] & (1<<20)) {
info.have_sse42 = true;
}
if (reg[2] & (1<<28)) {
info.have_avx = true;
}
if (reg[2] & (1<<23)) {
info.have_popcnt = true;
}
if (reg[2] & (1<<25)) {
info.have_aes = true;
}
if (info.have_avx512f) {
if (reg[2] & (1<<11)) {
info.have_avx512vnni = true;
}
}
#ifdef _WIN32
__cpuidex(reg, 7, 1);
#else
__cpuid_count(7, 1, reg[0], reg[1], reg[2], reg[3]);
#endif
if (reg[0] & (1<<5)) {
info.have_avx512bf16 = true;
}
}
test_generic();
test_sse();
test_avx();
test_avx512();
if (info.have_popcnt) {
GEN(Reg64, "popcnt", (g->popcnt(dst, src)), false, OT_INT);
}
if (info.have_aes) {
GEN(Xmm, "aesenc", (g->aesenc(dst,src)), false, OT_INT);
GEN(Xmm, "aesenclast", (g->aesenclast(dst,src)), false, OT_INT);
GEN(Xmm, "aesdec", (g->aesdec(dst,src)), false, OT_INT);
GEN(Xmm, "aesdeclast", (g->aesdeclast(dst,src)), false, OT_INT);
}
if (info.have_pclmulqdq) {
GEN(Xmm, "pclmulqdq", (g->pclmulqdq(dst,src,0)), false, OT_INT);
}
fclose(logs);
}