-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime-kernels.cpp
273 lines (200 loc) · 6.71 KB
/
time-kernels.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
#include <vector>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include "simd_helpers.hpp"
#include "simd_helpers/simd_debug.hpp"
#include "simd_helpers/downsample.hpp"
#include "simd_helpers/upsample.hpp"
using namespace std;
using namespace simd_helpers;
// -------------------------------------------------------------------------------------------------
inline double time_diff(const struct timeval &tv1, const struct timeval &tv2)
{
return (tv2.tv_sec - tv1.tv_sec) + 1.0e-6 * (tv2.tv_usec - tv1.tv_usec);
}
inline struct timeval get_time()
{
struct timeval ret;
if (gettimeofday(&ret, NULL) < 0)
throw std::runtime_error("gettimeofday() failed");
return ret;
}
void warm_up_cpu()
{
// A throwaway computation which uses the CPU for ~10^9
// clock cycles. The details (usleep, xor) are to prevent the
// compiler from optimizing it out!
//
// Empirically, this makes timing results more stable (without it,
// the CPU seems to run slow for the first ~10^9 cycles or so.)
long n = 0;
for (long i = 0; i < 1000L * 1000L * 1000L; i++)
n += (i ^ (i-1));
usleep(n % 2);
}
// -------------------------------------------------------------------------------------------------
template<typename T, int S, int N>
void time_old_downsample(T *zero, int niter)
{
struct timeval tv0 = get_time();
simd_t<T,S> dummy;
dummy.loadu(zero);
simd_ntuple<T,S,N> t;
t.loadu(zero);
for (int iter = 0; iter < niter; iter++) {
simd_t<T,S> u = downsample(t);
dummy ^= u;
t ^= u;
}
simd_store(zero, dummy);
struct timeval tv1 = get_time();
double dt_ns = 1.0e9 * time_diff(tv0,tv1) / (S*N*float(niter));
cout << "time_old_downsample<" << type_name<T>() << "," << S << "," << N << ">: " << dt_ns << " ns" << endl;
}
template<typename T, int S, int N>
void time_new_downsample(T *zero, int niter)
{
struct timeval tv0 = get_time();
simd_t<T,S> dummy;
dummy.loadu(zero);
simd_ntuple<T,S,N> t;
t.loadu(zero);
for (int iter = 0; iter < niter; iter++) {
simd_t<T,S> u = simd_downsample(t);
dummy ^= u;
t ^= u;
}
simd_store(zero, dummy);
struct timeval tv1 = get_time();
double dt_ns = 1.0e9 * time_diff(tv0,tv1) / (S*N*float(niter));
cout << "time_new_downsample<" << type_name<T>() << "," << S << "," << N << ">: " << dt_ns << " ns" << endl;
}
// -------------------------------------------------------------------------------------------------
template<typename T, int S, int N>
void time_old_upsample(T *zero, int niter)
{
struct timeval tv0 = get_time();
simd_t<T,S> dummy;
dummy.loadu(zero);
simd_ntuple<T,S,N> t;
for (int iter = 0; iter < niter; iter++) {
upsample(t, dummy);
dummy ^= t.vertical_xor();
}
simd_store(zero, dummy);
struct timeval tv1 = get_time();
double dt_ns = 1.0e9 * time_diff(tv0,tv1) / (S*N*float(niter));
cout << "time_old_upsample<" << type_name<T>() << "," << S << "," << N << ">: " << dt_ns << " ns" << endl;
}
template<typename T, int S, int N>
void time_new_upsample(T *zero, int niter)
{
struct timeval tv0 = get_time();
simd_t<T,S> dummy;
dummy.loadu(zero);
simd_ntuple<T,S,N> t;
for (int iter = 0; iter < niter; iter++) {
simd_upsample(t, dummy);
dummy ^= t.vertical_xor();
}
simd_store(zero, dummy);
struct timeval tv1 = get_time();
double dt_ns = 1.0e9 * time_diff(tv0,tv1) / (S*N*float(niter));
cout << "time_new_upsample<" << type_name<T>() << "," << S << "," << N << ">: " << dt_ns << " ns" << endl;
}
// -------------------------------------------------------------------------------------------------
void time_downsample()
{
vector<float> zero(64, 0.0);
vector<int> izero(64, 0);
time_old_downsample<float,4,2> (&zero[0], 1 << 30);
time_old_downsample<float,4,4> (&zero[0], 1 << 29);
time_old_downsample<float,8,2> (&zero[0], 1 << 29);
time_old_downsample<float,8,4> (&zero[0], 1 << 28);
time_old_downsample<float,8,8> (&zero[0], 1 << 27);
time_new_downsample<float,4,2> (&zero[0], 1 << 30);
time_new_downsample<float,4,4> (&zero[0], 1 << 29);
time_new_downsample<float,8,2> (&zero[0], 1 << 29);
time_new_downsample<float,8,4> (&zero[0], 1 << 28);
time_new_downsample<float,8,8> (&zero[0], 1 << 27);
time_new_downsample<int,4,2> (&izero[0], 1 << 30);
time_new_downsample<int,4,4> (&izero[0], 1 << 29);
time_new_downsample<int,8,2> (&izero[0], 1 << 29);
time_new_downsample<int,8,4> (&izero[0], 1 << 28);
time_new_downsample<int,8,8> (&izero[0], 1 << 27);
}
template<int S, int N>
void time_upsample(int niter)
{
vector<float> zero(64, 0.0);
vector<int> izero(64, 0.0);
time_old_upsample<int,S,N> (&izero[0], niter);
time_new_upsample<int,S,N> (&izero[0], niter);
time_old_upsample<float,S,N> (&zero[0], niter);
time_new_upsample<float,S,N> (&zero[0], niter);
}
void time_upsample()
{
time_upsample<4,2> (1 << 30);
time_upsample<4,4> (1 << 29);
time_upsample<8,2> (1 << 29);
time_upsample<8,4> (1 << 28);
time_upsample<8,8> (1 << 27);
}
// -------------------------------------------------------------------------------------------------
template<typename T, int S, int N>
void time_sort1(int niter)
{
simd_ntuple<T,S,N> x;
x.setzero();
struct timeval tv1 = get_time();
for (int i = 0; i < niter; i++)
simd_sort(x);
double ns_per_kernel = 1.0e9 * time_diff(tv1, get_time()) / double(niter);
double ns_per_scalar = ns_per_kernel / (S*N);
cout << "time_sort: " << type_name(x) << ": " << ns_per_kernel << " ns/kernel, " << ns_per_scalar << " ns/scalar" << endl;
}
template<int N>
void time_sort2(int niter)
{
cout << endl;
#ifdef __AVX__
time_sort1<int,8,N> (niter);
time_sort1<int64_t,4,N> (niter);
time_sort1<float,8,N> (niter);
time_sort1<double,4,N> (niter);
#else
time_sort1<int,4,N> (niter);
time_sort1<int64_t,2,N> (niter);
time_sort1<float,4,N> (niter);
time_sort1<double,2,N> (niter);
#endif
}
void time_sort()
{
time_sort2<2> (1 << 24);
time_sort2<3> (1 << 24);
time_sort2<4> (1 << 23);
time_sort2<5> (1 << 23);
time_sort2<6> (1 << 23);
time_sort2<7> (1 << 23);
time_sort2<8> (1 << 22);
time_sort2<9> (1 << 22);
time_sort2<10> (1 << 22);
time_sort2<11> (1 << 22);
time_sort2<12> (1 << 22);
time_sort2<13> (1 << 22);
time_sort2<14> (1 << 22);
time_sort2<15> (1 << 22);
time_sort2<16> (1 << 22);
}
// -------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
warm_up_cpu();
// time_downsample();
// time_upsample();
time_sort();
return 0;
}