-
Notifications
You must be signed in to change notification settings - Fork 9
/
bw_mem.c
183 lines (156 loc) · 4.26 KB
/
bw_mem.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
#include "bw_mem.h"
#define TARGETLEN 1024*1024*1024
int TEMP = 0;
void do_mem_prepare(mem_state_t *state) {
if(state->bufsize < TARGETLEN) {
int block = TARGETLEN / state->bufsize;
state->bufsize = TARGETLEN / block;
state->iter = block;
} else {
state->iter = 1;
}
state->rbuf = (char *)malloc(state->bufsize);
if(!state->rbuf) {
perror("memory malloc error\n");
exit(1);
}
state->wbuf = (char *)malloc(state->bufsize);
if(!state->wbuf) {
perror("memory malloc error\n");
exit(1);
}
bzero(state->rbuf, state->bufsize);
bzero(state->wbuf, state->bufsize);
}
void do_mem_clean(mem_state_t *state) {
if(state->rbuf)
free(state->rbuf);
if(state->wbuf)
free(state->wbuf);
state->iter = 0;
state->bufsize = 0;
}
void do_mem_rd(mem_state_t *state) {
register int sum = 0;
register int *p = (int *)state->rbuf;
register int *end = (int *)(state->rbuf + state->bufsize);
while(p < end) {
sum +=
#define OP(i) p[i]+
OP(0) OP(4) OP(8) OP(12) OP(16)
OP(20) OP(24) OP(28) OP(32) OP(36)
OP(40) OP(44) OP(48) OP(52) OP(56)
OP(60) OP(64) OP(68) OP(72) OP(76)
OP(80) OP(84) OP(88) OP(92) OP(96)
OP(100) OP(104) OP(108) OP(112) OP(116)
OP(120) p[124];
p += 128;
}
TEMP += sum;
}
#undef OP
void do_mem_wr(mem_state_t *state) {
register int sum = 0;
register int *p = (int *)state->rbuf;
register int *end = (int *)(state->rbuf + state->bufsize);
while(p < end) {
sum +=
#define OP(i) p[i] = 1;
OP(0) OP(4) OP(8) OP(12) OP(16)
OP(20) OP(24) OP(28) OP(32) OP(36)
OP(40) OP(44) OP(48) OP(52) OP(56)
OP(60) OP(64) OP(68) OP(72) OP(76)
OP(80) OP(84) OP(88) OP(92) OP(96)
OP(100) OP(104) OP(108) OP(112) OP(116)
OP(120) OP(124)
p += 128;
}
TEMP += sum;
}
#undef OP
void do_mem_cp(mem_state_t *state) {
register int sum = 0;
register int *p = (int *)state->rbuf;
register int *d = (int *)state->wbuf;
register int *end = (int *)(state->rbuf + state->bufsize);
while(p < end) {
#define OP(i) d[i] = p[i];
OP(0) OP(4) OP(8) OP(12) OP(16)
OP(20) OP(24) OP(28) OP(32) OP(36)
OP(40) OP(44) OP(48) OP(52) OP(56)
OP(60) OP(64) OP(68) OP(72) OP(76)
OP(80) OP(84) OP(88) OP(92) OP(96)
OP(100) OP(104) OP(108) OP(112) OP(116)
OP(120) OP(124)
p += 128;
d += 128;
}
}
#undef OP
double get_bw_mem(mem_state_t *state) {
struct timespec st, ed;
unsigned long elapse;
int iter, loopcnt, loop = 40;
int index = loop >> 2;
bw_result_t *r = (bw_result_t *)malloc(sizeof(bw_result_t) * loop);
loopcnt = loop;
if(state->prepare)
state->prepare(state);
while(loopcnt --) {
iter = state->iter;
clock_gettime(CLOCK_REALTIME, &st);
while(iter--)
state->bench(state);
clock_gettime(CLOCK_REALTIME, &ed);
elapse = get_total_us(&st, &ed);
save_bw_result(r, loopcnt, elapse, state->bufsize * state->iter);
}
if(state->cooldown)
state->cooldown(state);
qsort(r, loop, sizeof(bw_result_t ), cmp_bw_result);
double bandwidth = get_bandwidth(r[index].elapse, r[index].volume);
return bandwidth;
}
void bench_mem_rd() {
printf("\tBandwidth of Memory Read\n\n");
mem_state_t state;
state.prepare = do_mem_prepare;
state.bench = do_mem_rd;
state.cooldown = do_mem_clean;
for(size_t len = 4*1024; len < 256*1024*1024; len = len << 1) {
state.bufsize = len;
printf("\tblocksize: %7ldKB\tbandwidth of mem read: %lf MB/s\n", len>>10, get_bw_mem(&state));
}
printf("\n");
}
void bench_mem_wr() {
printf("\tBandwidth of Memory Write\n\n");
mem_state_t state;
state.prepare = do_mem_prepare;
state.bench = do_mem_wr;
state.cooldown = do_mem_clean;
for(size_t len = 4*1024; len < 256*1024*1024; len = len << 1) {
state.bufsize = len;
printf("\tblocksize: %7ldKB\tbandwidth of mem write: %lf MB/s\n", len>>10, get_bw_mem(&state));
}
printf("\n");
}
void bench_mem_cp() {
printf("\tBandwidth of Memory Copy\n\n");
mem_state_t state;
state.prepare = do_mem_prepare;
state.bench = do_mem_cp;
state.cooldown = do_mem_clean;
for(size_t len = 4*1024; len < 256*1024*1024; len = len << 1) {
state.bufsize = len;
printf("\tblocksize: %7ldKB\tbandwidth of mem copy: %lf MB/s\n", len>>10, get_bw_mem(&state));
}
printf("\n");
}
void bench_bandwidth_mem() {
printf("\nbenchmark on Bandwidth of Memory\n");
printf("-------------------------------------\n\n");
bench_mem_rd();
bench_mem_wr();
bench_mem_cp();
}