-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemset-bench-multi.c
432 lines (387 loc) · 12.3 KB
/
memset-bench-multi.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <assert.h>
#include <immintrin.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <x86intrin.h>
// gcc -DI_NO_HIDDEN_LABELS -O3 -march=native memset-bench-multi.c -o
// memset-bench-multi
#define ALWAYS_INLINE inline __attribute__((always_inline))
#define NEVER_INLINE __attribute__((noinline))
#define CONST_ATTR __attribute__((const))
#define PURE_ATTR __attribute__((pure))
#define BENCH_FUNC __attribute__((noinline, noclone, aligned(4096)))
#define COMPILER_OOE_BARRIER() asm volatile("lfence" : : : "memory")
#define OOE_BARRIER() asm volatile("lfence" : : :)
#define COMPILER_BARRIER() asm volatile("" : : : "memory");
#define COMPILER_DO_NOT_OPTIMIZE_OUT(X) \
asm volatile("" : : "i,r,m,v"(X) : "memory")
#define _CAT(X, Y) X##Y
#define CAT(X, Y) _CAT(X, Y)
#define _V_TO_STR(X) #X
#define V_TO_STR(X) _V_TO_STR(X)
#define NO_LSD_RD(tmp, r) "pop " #tmp "\nmovq " #r ", %%rsp\n"
#define NO_LSD_WR(tmp, r) "push " #tmp "\nmovq " #r ", %%rsp\n"
#define IMPOSSIBLE(X) \
if (X) { \
__builtin_unreachable(); \
}
#define PRINT(...) fprintf(stderr, __VA_ARGS__)
static struct timespec
get_ts() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts;
}
static uint64_t
to_ns(struct timespec ts) {
return ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
}
static uint64_t
get_ns() {
struct timespec ts = get_ts();
return to_ns(ts);
}
static uint64_t
get_ns_dif(struct timespec ts_start, struct timespec ts_end) {
return to_ns(ts_end) - to_ns(ts_start);
}
static double
gb_sec(uint64_t bytes, uint64_t ns) {
double d_bytes = bytes;
double d_ns = ns;
double d_gb = d_bytes / ((double)1024 * 1024 * 1024);
double d_sec = d_ns / (double)(1000 * 1000 * 1000);
return d_gb / d_sec;
}
static uint32_t g_iter;
static uint32_t g_reuse;
static size_t g_size;
static pthread_barrier_t g_barrier;
typedef struct targs {
uint8_t * dst;
uint8_t val;
pthread_t tid;
uint64_t ns_out;
uint8_t * sink;
} targs_t;
#define ssse3 __memset_ssse3_back
#define erms __memset_erms
#define sse2_erms __memset_sse2_unaligned_erms
#define sse2 __memset_sse2_unaligned
#define evex_erms __memset_evex_unaligned_erms
#define evex __memset_evex_unaligned
static void
memset_t(uint8_t * dst, uint8_t val, size_t len) {
__m256i v0 = _mm256_set1_epi8(val);
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"1:\n"
"vmovdqa %[v0], (" VEC_SIZE " * 0)(%[dst])\n"
"vmovdqa %[v0], (" VEC_SIZE " * 1)(%[dst])\n"
"vmovdqa %[v0], (" VEC_SIZE " * 2)(%[dst])\n"
"vmovdqa %[v0], (" VEC_SIZE " * 3)(%[dst])\n"
"subq $(" VEC_SIZE " * -4), %[dst]\n"
"addq $(" VEC_SIZE " * -4), %[len]\n"
"jg 1b"
: [dst] "+r"(dst), [len] "+r"(len)
: [v0] "v"(v0)
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
};
static void
memset_nt(uint8_t * dst, uint8_t val, size_t len) {
__m256i v0 = _mm256_set1_epi8(val);
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"1:\n"
"vmovntdq %[v0], (" VEC_SIZE " * 0)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 1)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 2)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 3)(%[dst])\n"
"subq $(" VEC_SIZE " * -4), %[dst]\n"
"addq $(" VEC_SIZE " * -4), %[len]\n"
"jg 1b"
: [dst] "+r"(dst), [len] "+r"(len)
: [v0] "v"(v0)
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
};
static void
memset_nt_ns(uint8_t * dst, uint8_t val, size_t len) {
__m256i v0 = _mm256_set1_epi8(val);
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"1:\n"
"vmovntdq %[v0], (" VEC_SIZE " * 0)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 1)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 2)(%[dst])\n"
"vmovntdq %[v0], (" VEC_SIZE " * 3)(%[dst])\n"
"subq $(" VEC_SIZE " * -4), %[dst]\n"
"addq $(" VEC_SIZE " * -4), %[len]\n"
"jg 1b\n"
"sfence"
: [dst] "+r"(dst), [len] "+r"(len)
: [v0] "v"(v0)
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
};
static void
memset_cd(uint8_t * dst, uint8_t val, size_t len) {
uint8_t tmp[64] __attribute__((aligned(64)));
__builtin_memset(tmp, val, 64);
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"1:\n"
"movdir64b (%[src]), %[dst]\n"
"subq $-64, %[dst]\n"
"movdir64b (%[src]), %[dst]\n"
"subq $-64, %[dst]\n"
"addq $-128, %[len]\n"
"jg 1b\n"
"sfence"
: [dst] "+r"(dst), [len] "+r"(len)
: [src]"r" (tmp)
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
};
static void
memset_cd_ns(uint8_t * dst, uint8_t val, size_t len) {
uint8_t tmp[64] __attribute__((aligned(64)));
__builtin_memset(tmp, val, 64);
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"1:\n"
"movdir64b (%[src]), %[dst]\n"
"subq $-64, %[dst]\n"
"movdir64b (%[src]), %[dst]\n"
"subq $-64, %[dst]\n"
"addq $-128, %[len]\n"
"jg 1b"
: [dst] "+r"(dst), [len] "+r"(len)
: [src]"r" (tmp)
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
};
static void
memset_erms(uint8_t * dst, uint8_t val, size_t len) {
__asm__ volatile("rep stosb" : "+D"(dst), "+c"(len) : "a"(val) : "memory");
}
static uint8_t
use(uint8_t * dst, size_t len) {
__m256i v0, v1, v2, v3;
#define VEC_SIZE "32"
// clang-format off
__asm__ volatile(
"vpxor %[v0], %[v0], %[v0]\n"
"vpxor %[v1], %[v1], %[v1]\n"
"vpxor %[v2], %[v2], %[v2]\n"
"vpxor %[v3], %[v3], %[v3]\n"
"1:\n"
"vpxor (" VEC_SIZE " * 0)(%[dst]), %[v0]\n"
"vpxor (" VEC_SIZE " * 1)(%[dst]), %[v1]\n"
"vpxor (" VEC_SIZE " * 2)(%[dst]), %[v2]\n"
"vpxor (" VEC_SIZE " * 3)(%[dst]), %[v3]\n"
"subq $(" VEC_SIZE " * -4), %[dst]\n"
"addq $(" VEC_SIZE " * -4), %[len]\n"
"jg 1b"
"vpxor %[v0], %[v1], %[v1]\n"
"vpxor %[v2], %[v3], %[v3]\n"
"vpxor %[v1], %[v3], %[v3]\n"
"vpcmpeqb %[v0], %[v0], %[v0]\n"
"vpor %[v0], %[v3], %[v3]\n"
"vmovq %[v3], %[len]\n"
"incq %[len]\n"
: [dst] "+r"(dst), [len] "+r"(len), [v0] "=&v"(v0),
[v1] "=&v"(v1), [v2] "=&v"(v2), [v3] "=&v"(v3)
:
: "cc", "memory");
// clang-format on
#undef VEC_SIZE
return len;
};
#ifndef TODO
# define TODO 6
#endif
#if TODO == 0
# define FUNC memset_erms
#elif TODO == 1
# define FUNC memset_t
#elif TODO == 2
# define FUNC memset_nt
#elif TODO == 3
# define FUNC memset_nt_ns
#elif TODO == 4
# define FUNC memset_cd
#elif TODO == 5
# define FUNC memset_cd_ns
#else
# error "Unknown TODO"
#endif
#define NAME V_TO_STR(FUNC)
#define LOG(...) // fprintf(stderr, __VA_ARGS__)
void * BENCH_FUNC
bench(void * arg) {
LOG("In Thread\n");
struct timespec start, end;
uint32_t bench_iter = g_iter;
uint32_t reuse = g_reuse;
size_t sz = g_size;
bench_iter &= -1;
uint8_t * dst = ((targs_t *)arg)->dst;
uint8_t val = ((targs_t *)arg)->val;
uint8_t * sink = ((targs_t *)arg)->sink;
assert(reuse <= 3);
LOG("About to run...\n");
switch (reuse) {
case 0:
pthread_barrier_wait(&g_barrier);
start = get_ts();
__asm__ volatile(".p2align 6\n" : : :);
for (; bench_iter; --bench_iter) {
FUNC(dst, val, sz);
}
end = get_ts();
break;
case 1:
pthread_barrier_wait(&g_barrier);
start = get_ts();
__asm__ volatile(".p2align 6\n" : : :);
for (; bench_iter; bench_iter -= 2) {
FUNC(dst, val, sz);
COMPILER_DO_NOT_OPTIMIZE_OUT(memcpy(sink, dst, sz));
}
end = get_ts();
break;
case 2:
pthread_barrier_wait(&g_barrier);
start = get_ts();
__asm__ volatile(".p2align 6\n" : : :);
for (; bench_iter; bench_iter -= 2) {
FUNC(dst, val, sz);
COMPILER_DO_NOT_OPTIMIZE_OUT(use(dst, sz));
}
end = get_ts();
break;
case 3:
pthread_barrier_wait(&g_barrier);
start = get_ts();
__asm__ volatile(".p2align 6\n" : : :);
for (; bench_iter; bench_iter -= 2) {
FUNC(dst, val, sz);
val |= use(dst, sz);
}
end = get_ts();
break;
default:
__builtin_unreachable();
}
((targs_t *)arg)->ns_out = get_ns_dif(start, end);
return NULL;
}
int
main(int argc, char ** argv) {
if (argc < 5) {
printf("Usage: %s <nthreads> <size> <iter> <opt:align> <opt:reuse>\n",
argv[0]);
return 0;
}
LOG("Getting Conf\n");
long nthreads = strtol(argv[1], NULL, 10);
char * end;
size_t size = strtoul(argv[2], &end, 10);
if (strncasecmp(end, "kb", 2) == 0) {
size *= 1024;
}
else if (strncasecmp(end, "mb", 2) == 0) {
size *= 1024 * 1024;
}
else if (strncasecmp(end, "gb", 2) == 0) {
size *= 1024 * 1024 * 1024;
}
uint32_t iter = strtoul(argv[3], NULL, 10);
iter &= -1;
uint32_t align = argc >= 5 ? strtoul(argv[4], NULL, 10) : 0;
uint32_t reuse = argc >= 6 ? strtoul(argv[5], NULL, 10) : 0;
LOG("Setting of threads\n");
assert(reuse <= 3);
align %= 4096;
align &= -32;
assert(nthreads != 0 && size != 0 && iter != 0);
if (nthreads < 0) {
nthreads = sysconf(_SC_NPROCESSORS_ONLN);
}
g_iter = iter;
g_size = size;
assert(pthread_barrier_init(&g_barrier, NULL, nthreads) == 0);
pthread_attr_t attr;
assert(pthread_attr_init(&attr) == 0);
assert(pthread_attr_setstacksize(&attr, 524288) == 0);
targs_t targs[nthreads];
LOG("Running\n");
for (uint8_t val = 0; val < 2; ++val) {
for (long i = 0; i < nthreads; ++i) {
LOG("Creating Memory: %lu\n", i);
uint8_t * dst =
(uint8_t *)mmap(NULL, size + align, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0L);
uint8_t * sink =
(uint8_t *)mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0L);
LOG("Checking memory\n");
assert(sink != MAP_FAILED);
assert(dst != MAP_FAILED);
assert((align % 32) == 0);
targs[i].dst = dst + align;
targs[i].val = val;
targs[i].sink = sink;
LOG("Creating actual thread\n");
assert(pthread_create(&(targs[i].tid), &attr, bench,
(void *)(targs + i)) == 0);
}
for (long i = 0; i < nthreads; ++i) {
assert(pthread_join(targs[i].tid, NULL) == 0);
}
for (long i = 0; i < nthreads; ++i) {
LOG("Unmapping Thread: %lu\n", i);
munmap(targs[i].dst, size + align);
munmap(targs[i].sink, size);
}
}
#if 0
printf("Nthreads = %ld, Size = %zu Bytes, Iter = %u, Align = %u\n", nthreads, size,
iter, align);
printf("Impl: %s\n", NAME);
double sum = 0.0;
size_t bytes = size * iter;
for (long i = 0; i < nthreads; ++i) {
double bw = gb_sec(bytes, targs[i].ns_out);
printf("Thread %-2ld: %.4lf GB/sec\n", i, bw);
sum += bw;
}
printf("Average: %.4lf GB/sec\n", sum / ((double)nthreads));
#else
printf("func,nthreads,iter,size,align,reuse,time_ns\n");
for (long i = 0; i < nthreads; ++i) {
printf("%s,%ld,%u,%zu,%u,%u,%lu\n", NAME, nthreads, iter, size, align,
reuse, targs[i].ns_out);
}
#endif
}