-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.c
161 lines (137 loc) · 3.9 KB
/
main.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
// Copyright 2013 Alex Reece.
//
// A simple memory bandwidth profiler.
//
// Each of the write_memory_* functions read from a 1GB array. Each of the
// write_memory_* writes to the 1GB array. The goal is to get the max memory
// bandwidth as advertised by the intel specs: 23.8 GiB/s (http://goo.gl/r8Aab)
#include <assert.h>
#include <math.h>
#ifdef WITH_OPENMP
#include <omp.h>
#endif // WITH_OPENMP
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "./functions.h"
#include "./monotonic_timer.h"
#define SAMPLES 5
#define TIMES 5
#define BYTES_PER_GB (1024*1024*1024LL)
#define SIZE (1*BYTES_PER_GB)
#define PAGE_SIZE (1<<12)
// This must be at least 32 byte aligned to make some AVX instructions happy.
// Have PAGE_SIZE buffering so we don't have to do math for prefetching.
char array[SIZE + PAGE_SIZE] __attribute__((aligned (32)));
// Compute the bandwidth in GiB/s.
static inline double to_bw(size_t bytes, double secs) {
double size_bytes = (double) bytes;
double size_gb = size_bytes / ((double) BYTES_PER_GB);
return size_gb / secs;
}
#ifdef WITH_OPENMP
// Time a function, printing out time to perform the memory operation and
// the computed memory bandwidth. Use openmp to do threading (set environment
// variable OMP_NUM_THREADS to control threads use.
#define timefunp(f) timeitp(f, #f)
void timeitp(void (*function)(void*, size_t), char* name) {
double min = INFINITY;
size_t i;
for (i = 0; i < SAMPLES; i++) {
double before, after, total;
assert(SIZE % omp_get_max_threads() == 0);
size_t chunk_size = SIZE / omp_get_max_threads();
#pragma omp parallel
{
#pragma omp barrier
#pragma omp master
before = monotonic_time();
int j;
for (j = 0; j < TIMES; j++) {
function(&array[chunk_size * omp_get_thread_num()], chunk_size);
}
#pragma omp barrier
#pragma omp master
after = monotonic_time();
}
total = after - before;
if (total < min) {
min = total;
}
}
printf("%28s_omp: %5.2f GiB/s\n", name, to_bw(SIZE * TIMES, min));
}
#endif // WITH_OPENMP
// Time a function, printing out time to perform the memory operation and
// the computed memory bandwidth.
#define timefun(f) timeit(f, #f)
void timeit(void (*function)(void*, size_t), char* name) {
double min = INFINITY;
size_t i;
for (i = 0; i < SAMPLES; i++) {
double before, after, total;
before = monotonic_time();
int j;
for (j = 0; j < TIMES; j++) {
function(array, SIZE);
}
after = monotonic_time();
total = after - before;
if (total < min) {
min = total;
}
}
printf("%32s: %5.2f GiB/s\n", name, to_bw(SIZE * TIMES, min));
}
int main() {
memset(array, 0xFF, SIZE); // un-ZFOD the page.
* ((uint64_t *) &array[SIZE]) = 0;
// TODO(awreece) iopl(0) and cli/sti?
timefun(read_memory_rep_lodsq);
timefun(read_memory_loop);
#ifdef __SSE4_1__
timefun(read_memory_sse);
#endif
#ifdef __AVX__
timefun(read_memory_avx);
timefun(read_memory_prefetch_avx);
#endif
timefun(write_memory_loop);
timefun(write_memory_rep_stosq);
#ifdef __SSE4_1__
timefun(write_memory_sse);
timefun(write_memory_nontemporal_sse);
#endif
#ifdef __AVX__
timefun(write_memory_avx);
timefun(write_memory_nontemporal_avx);
#endif
timefun(write_memory_memset);
#ifdef WITH_OPENMP
memset(array, 0xFF, SIZE); // un-ZFOD the page.
* ((uint64_t *) &array[SIZE]) = 0;
timefunp(read_memory_rep_lodsq);
timefunp(read_memory_loop);
#ifdef __SSE4_1__
timefunp(read_memory_sse);
#endif
#ifdef __AVX__
timefunp(read_memory_avx);
timefunp(read_memory_prefetch_avx);
#endif
timefunp(write_memory_loop);
timefunp(write_memory_rep_stosq);
#ifdef __SSE4_1__
timefunp(write_memory_sse);
timefunp(write_memory_nontemporal_sse);
#endif
#ifdef __AVX__
timefunp(write_memory_avx);
timefunp(write_memory_nontemporal_avx);
#endif
timefunp(write_memory_memset);
#endif // WITH_OPENMP
return 0;
}