-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_unroll.cpp
359 lines (265 loc) · 10.1 KB
/
loop_unroll.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
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
/*
Copyright 2007-2008 Adobe Systems Incorporated
Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
or a copy at http://stlab.adobe.com/licenses.html)
Goal: Test compiler optimizations related to loop unrolling
Assumptions:
1) the compiler will unroll loops to hide instruction latency
for() {}
while() {}
do {} while()
goto
2) if the compiler unrolls the loop, it should not be slower than the original loop without unrolling
3) the compiler should unroll a multi-calculation loop as well as a single calculation loop
up to the limit of performance gain for unrolling that loop
in other words: no penalty for manually unrolling,
as long as the manual unroll is less than or equal to the optimum unroll factor
4) The compiler should recognize and unroll all loop styles with the same efficiency
in other words: do, while, for, and goto should have identical performance
*/
#include "benchmark_stdint.hpp"
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include "benchmark_results.h"
#include "benchmark_timer.h"
/******************************************************************************/
// this constant may need to be adjusted to give reasonable minimum times
// For best results, times should be about 1.0 seconds for the minimum test run
int iterations = 300000;
// 8000 items, or between 8k and 64k of data
// this is intended to remain within the L2 cache of most common CPUs
#define SIZE 8000
// initial value for filling our arrays, may be changed from the command line
double init_value = 1.0;
/******************************************************************************/
#include "benchmark_shared_tests.h"
/******************************************************************************/
/******************************************************************************/
template <typename T>
T hash_func2(T seed) {
return (914237 * (seed + 12345)) - 13;
}
template <typename T>
T complete_hash_func(T seed) {
return hash_func2( hash_func2( hash_func2( seed ) ) );
}
/******************************************************************************/
template <typename T>
inline void check_sum(T result) {
T temp = (T)SIZE * complete_hash_func( (T)init_value );
if (!tolerance_equal<T>(result,temp)) printf("test %i failed\n", current_test);
}
/******************************************************************************/
// this is the heart of our loop unrolling - a class that unrolls itself to generate the inner loop code
// at least as long as we keep F < 50 (or some compilers won't compile it)
template< int F, typename T >
struct loop_inner_body {
inline static void do_work(T &result, const T *first, int n) {
loop_inner_body<F-1,T>::do_work(result, first, n);
T temp = first[ n + (F-1) ];
temp = complete_hash_func( temp );
result += temp;
}
};
template< typename T >
struct loop_inner_body<0,T> {
inline static void do_work(T &, const T *, int) {
}
};
/******************************************************************************/
/******************************************************************************/
// F is the unrolling factor
template <int F, typename T >
void test_for_loop_unroll_factor(const T* first, int count, const char *label) {
int i;
start_timer();
for(i = 0; i < iterations; ++i) {
T result = 0;
int n = 0;
for (; n < (count - (F-1)); n += F) {
loop_inner_body<F,T>::do_work(result,first, n);
}
for (; n < count; ++n) {
result += complete_hash_func( first[n] );
}
check_sum<T>(result);
}
record_result( timer(), label );
}
/******************************************************************************/
// F is the unrolling factor
template <int F, typename T >
void test_while_loop_unroll_factor(const T* first, int count, const char *label) {
int i;
start_timer();
for(i = 0; i < iterations; ++i) {
T result = 0;
int n = 0;
while ( n < (count - (F-1)) ) {
loop_inner_body<F,T>::do_work(result,first, n);
n += F;
}
while ( n < count ) {
result += complete_hash_func( first[n] );
++n;
}
check_sum<T>(result);
}
record_result( timer(), label );
}
/******************************************************************************/
// F is the unrolling factor
template <int F, typename T >
void test_do_loop_unroll_factor(const T* first, int count, const char *label) {
int i;
start_timer();
for(i = 0; i < iterations; ++i) {
T result = 0;
int n = 0;
if ((count - n) >= F)
do {
loop_inner_body<F,T>::do_work(result,first, n);
n += F;
} while (n < (count - (F-1)));
if (n < count)
do {
result += complete_hash_func( first[n] );
++n;
} while (n != count);
check_sum<T>(result);
}
record_result( timer(), label );
}
/******************************************************************************/
// F is the unrolling factor
template <int F, typename T >
void test_goto_loop_unroll_factor(const T* first, int count, const char *label) {
int i;
start_timer();
for(i = 0; i < iterations; ++i) {
T result = 0;
int n = 0;
if ((count - n) >= F) {
loop2_start:
loop_inner_body<F,T>::do_work(result,first, n);
n += F;
if (n < (count - (F-1)))
goto loop2_start;
}
if (n < count) {
loop_start:
result += complete_hash_func( first[n] );
++n;
if (n != count)
goto loop_start;
}
check_sum<T>(result);
}
record_result( timer(), label );
}
/******************************************************************************/
/******************************************************************************/
// our global arrays of numbers to be operated upon
double dataDouble[SIZE];
int32_t data32[SIZE];
// not elegant, but I need strings to hang around until we print the results
// and I don't want to pull in STL
const int UnrollLimit = 32;
char temp_string[UnrollLimit][100];
/******************************************************************************/
/******************************************************************************/
// another unrolled loop to create all of our tests
template< int N, typename T >
struct for_loop_tests {
static void do_test( const T *data, const char *label_base ) {
for_loop_tests<N-1, T>::do_test(data, label_base);
sprintf( temp_string[N-1], "%s %d", label_base, N );
test_for_loop_unroll_factor<N>( data, SIZE, temp_string[N-1] );
}
};
template<typename T>
struct for_loop_tests<0,T> {
static void do_test( const T *, const char * ) {
}
};
/******************************************************************************/
template< int N, typename T >
struct while_loop_tests {
static void do_test( const T *data, const char *label_base ) {
while_loop_tests<N-1, T>::do_test(data, label_base);
sprintf( temp_string[N-1], "%s %d", label_base, N );
test_while_loop_unroll_factor<N>( data, SIZE, temp_string[N-1] );
}
};
template<typename T>
struct while_loop_tests<0,T> {
static void do_test( const T *, const char * ) {
}
};
/******************************************************************************/
template< int N, typename T >
struct do_loop_tests {
static void do_test( const T *data, const char *label_base ) {
do_loop_tests<N-1, T>::do_test(data, label_base);
sprintf( temp_string[N-1], "%s %d", label_base, N );
test_do_loop_unroll_factor<N>( data, SIZE, temp_string[N-1] );
}
};
template<typename T>
struct do_loop_tests<0,T> {
static void do_test( const T *, const char * ) {
}
};
/******************************************************************************/
template< int N, typename T >
struct goto_loop_tests {
static void do_test( const T *data, const char *label_base ) {
goto_loop_tests<N-1, T>::do_test(data, label_base);
sprintf( temp_string[N-1], "%s %d", label_base, N );
test_goto_loop_unroll_factor<N>( data, SIZE, temp_string[N-1] );
}
};
template<typename T>
struct goto_loop_tests<0,T> {
static void do_test( const T *, const char * ) {
}
};
/******************************************************************************/
/******************************************************************************/
int main(int argc, char** argv) {
// output command for documentation:
int i;
for (i = 0; i < argc; ++i)
printf("%s ", argv[i] );
printf("\n");
if (argc > 1) iterations = atoi(argv[1]);
if (argc > 2) init_value = (double) atof(argv[2]);
// int32_t
::fill(data32, data32+SIZE, int32_t(init_value));
for_loop_tests<UnrollLimit, int32_t>::do_test( data32, "int32_t for loop unroll" );
summarize("int32_t for loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
while_loop_tests<UnrollLimit, int32_t>::do_test( data32, "int32_t while loop unroll" );
summarize("int32_t while loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
do_loop_tests<UnrollLimit, int32_t>::do_test( data32, "int32_t do loop unroll" );
summarize("int32_t do loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
goto_loop_tests<UnrollLimit, int32_t>::do_test( data32, "int32_t goto loop unroll" );
summarize("int32_t goto loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
// double
iterations /= 4;
::fill(dataDouble, dataDouble+SIZE, double(init_value));
for_loop_tests<UnrollLimit, double>::do_test( dataDouble, "double for loop unroll" );
summarize("double for loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
while_loop_tests<UnrollLimit, double>::do_test( dataDouble, "double while loop unroll" );
summarize("double while loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
do_loop_tests<UnrollLimit, double>::do_test( dataDouble, "double do loop unroll" );
summarize("double do loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
goto_loop_tests<UnrollLimit, double>::do_test( dataDouble, "double goto loop unroll" );
summarize("double goto loop unrolling", SIZE, iterations, kDontShowGMeans, kDontShowPenalty );
return 0;
}
// the end
/******************************************************************************/
/******************************************************************************/