This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreaming_min_max_comparison.cpp
More file actions
425 lines (348 loc) · 10 KB
/
Copy pathstreaming_min_max_comparison.cpp
File metadata and controls
425 lines (348 loc) · 10 KB
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
#include "streaming_min_max_algorithms.h"
#include "utils.h"
#include <cmath>
#include <cstring>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <thread>
#include <cuda.h>
#include <cuda_runtime.h>
#ifdef __linux__
#include <getopt.h>
#include <error.h>
#else
#include "getopt.h"
//extern int opterr;
extern int optind;
#endif
#include <cerrno>
#include <climits>
#include <limits>
#include <algorithm>
#define ENABLE_NVTX
#ifdef ENABLE_NVTX
#include <nvToolsExt.h>
#endif
#ifndef __linux__
char *program_invocation_name("<not yet set>");
#endif
#ifdef DEBUG
bool verbose(false);
#endif // DEBUG
static std::vector<float> create_white_noise_vector(
unsigned int size
)
{
std::vector<float> data(size);
srand(time(0));
for (unsigned int k = 0; k < size; ++k)
{
data[k] = (1.0 * rand() / (RAND_MAX)) - 0.5;
}
return data;
}
static bool check_for_difference(
std::string_view minmax,
std::string_view reference_name,
std::vector<float> const & reference_values,
std::string_view compare_name,
std::vector<float> const & compare_values
)
{
if (reference_values.size() != compare_values.size())
{
ERROR(
"Number of %s values differ between %s and %s "
"(%lu vs. %lu)",
minmax.data(),
reference_name.data(),
compare_name.data(),
reference_values.size(),
compare_values.size()
);
return true;
}
auto mismatch =
std::mismatch(
reference_values.begin(),
reference_values.end(),
compare_values.begin()
);
bool retval = false;
if (mismatch.first != reference_values.end())
{
ERROR(
"Some %s values differ between %s and %s",
minmax.data(),
reference_name.data(),
compare_name.data()
);
}
while (mismatch.first != reference_values.end())
{
auto position = mismatch.first - reference_values.begin();
retval = true;
std::cerr << "\t" << position << ": " << *(mismatch.first)
<< " != " << *(mismatch.second) << '\n';
mismatch =
std::mismatch(
mismatch.first + 1,
reference_values.end(),
mismatch.second + 1
);
}
return retval;
}
static void compare_all_algos(
std::vector<std::unique_ptr<streaming_min_max_algorithm_interface>> & algorithms,
std::vector<float> const & data,
std::vector<std::chrono::duration<double>> & timings,
unsigned int window_size
)
{
for (unsigned int i = 0; i < algorithms.size(); ++i)
{
auto reference_name = algorithms[0]->get_name();
auto compare_name = algorithms[i]->get_name();
auto start = std::chrono::high_resolution_clock::now();
#ifdef ENABLE_NVTX
nvtxRangeId_t nxtxRange = nvtxRangeStartA(std::string(compare_name).c_str());
#endif
// timing with cuda events
cudaEvent_t cu_start, cu_stop;
cudaEventCreate(&cu_start);
cudaEventCreate(&cu_stop);
cudaEventRecord(cu_start, 0);
algorithms[i]->calc(data, window_size);
cudaThreadSynchronize();
cudaEventRecord(cu_stop, 0);
cudaEventSynchronize(cu_stop);
#ifdef ENABLE_NVTX
nvtxRangeEnd(nxtxRange);
#endif
auto finish = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(5)); // just to see a separation between nvtx ranges in the profiler
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, cu_start, cu_stop);
//std::cout << milliseconds << std::endl;
timings[i] = timings[i] + (finish - start); //
//timings[i] = timings[i] + std::chrono::nanoseconds((int)(milliseconds * 1000));
//
// store results of Lemire's algorithms as a reference for
// a diff-check
//
auto& reference_max_values = algorithms[0]->get_max_values();
auto& reference_min_values = algorithms[0]->get_min_values();
/*printf("Ref Values\n");
for(auto v : reference_min_values)
printf("%.2f, ", v);
printf("\n");*/
if (algorithms[i]->check_against_reference())
{
auto& compare_max_values = algorithms[i]->get_max_values();
auto& compare_min_values = algorithms[i]->get_min_values();
bool difference_min = check_for_difference(
"min",
reference_name,
reference_min_values,
compare_name,
compare_min_values
);
/*printf("%s Values\n", std::string(compare_name).c_str());
for(auto v : compare_min_values)
printf("%.2f, ", v);
printf("\n");*/
bool difference_max = check_for_difference(
"max",
reference_name,
reference_max_values,
compare_name,
compare_max_values
);
}
}
}
static void timings(
std::vector<std::unique_ptr<streaming_min_max_algorithm_interface>> & algorithms,
unsigned int window_size,
unsigned int sample_size,
unsigned int number_of_iterations
)
{
std::vector<std::chrono::duration<double>> timings(algorithms.size());
std::cout << "\nPerforming a comparison using the following parameters:"
<< "\n window_size = " << window_size
<< "\n sample_size = " << sample_size
<< "\n number_of_iterations = " << number_of_iterations
<< "\n\n";
for (unsigned int i = 0; i < number_of_iterations; ++i)
{
auto data = create_white_noise_vector(sample_size);
TRACE("Input sample contains the following %d values:\n");
#ifdef DEBUG
if (verbose)
{
for (unsigned int j = 0; j < data.size(); ++j)
{
std::cout << '\t' << j << ": " << data[j] << '\n';
}
}
#endif
compare_all_algos(algorithms, data, timings, window_size);
if(i == 0) // drop the first timing to get a more accurate average
{
timings = std::vector<std::chrono::duration<double>>(algorithms.size());
}
}
for (unsigned int i = 0; i < algorithms.size(); ++i)
{
std::cout << algorithms[i]->get_name() << " = " << std::fixed << std::setprecision( 6 ) << (1000.0f * timings[i].count() / (float)(number_of_iterations - 1)) << " milliseconds\n";
}
}
/**
* \brief Print a usage message
*
* Prints a usage message to \a stream and terminate program with \a exit_code
*
* \param fp stdio stream to write usage message to [IN]
* \param exit_code exit code returned to the environment [IN]
*/
static void usage(
std::ostream &ostr,
int exit_code
)
{
size_t i;
ostr << "Usage: " << program_invocation_name << " [OPTIONS]\n\n"
<< "where OPTIONS may be one or multiple of the following:\n"
<< "-h, --help\n"
#ifdef DEBUG
<< "-v, --verbose\n"
#endif // DEBUG
<< "-w, --window_size=WINDOW_SIZE[1, " << std::numeric_limits<int>::max() << "]\n"
<< "-s, --sample_size=SAMPLE_SIZE[1, " << std::numeric_limits<int>::max() << "]\n"
<< "-i, --iterations=ITERATIONS[1, " << std::numeric_limits<int>::max() << "]\n";
exit(exit_code);
}
#ifdef __linux__
/**
* \brief Converts a string into a unsigned int
*
* Converts a string into a unsigned int doing overflow and underflow checking.
* Terminates with a \a usage() message in case of an error
*
* \param str string to covert [IN]
* \return numerical value corresponding to string
*/
static unsigned int checked_to_uint(
const char *str
)
{
char *end_ptr = NULL;
long const result = strtol(str, &end_ptr, 10);
if (((result == LONG_MIN) || (result == LONG_MAX)) && (errno == ERANGE))
{
usage(std::cerr, EXIT_FAILURE);
}
if ((end_ptr == NULL) || (end_ptr == str) || (*end_ptr != '\0'))
{
usage(std::cerr, EXIT_FAILURE);
}
if ((result < 1) || (result > std::numeric_limits<int>::max()))
{
usage(std::cerr, EXIT_FAILURE);
}
return static_cast<unsigned int>(result);
}
#endif
int main(
int argc,
char **argv
)
{
unsigned int window_size = 500;
unsigned int sample_size = 10000000;
unsigned int number_of_iterations = 1;
program_invocation_name = argv[0];
char c;
#ifdef __linux__
struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
#ifdef DEBUG
{"verbose", no_argument, NULL, 'v'},
#endif // DEBUG
{"window_size", required_argument, NULL, 'w'},
{"sample_size", required_argument, NULL, 's'},
{"iterations", required_argument, NULL, 'i'},
{0, 0, 0, 0}
};
#endif
while (
#ifdef __linux__
(c = getopt_long(
#else
(c = getopt(
#endif
argc,
argv,
#ifdef DEBUG
"v"
#endif // DEBUG
"hw:s:i:"
#ifdef __linux__
,long_options,
NULL
#endif
)
) != -1
)
{
switch (c)
{
case 'h':
usage(std::cout, EXIT_SUCCESS);
break;
#ifdef DEBUG
case 'v':
verbose = true;
break;
#endif // DEBUG
case 'w':
window_size = atoi(optarg);
break;
case 's':
sample_size = atoi(optarg);
break;
case 'i':
number_of_iterations = atoi(optarg);
break;
case '?':
default:
usage(std::cerr, EXIT_FAILURE);
break;
}
}
if ((optind < argc))
{
usage(std::cerr, EXIT_FAILURE);
}
// #else
//printf("prog: %s\n", program_invocation_name);
/*if(argc != 4)
std::cout << "params: window_size, sample_size, iterations" << std::endl;
return -1;
std::cout << "p?" << std::endl;
std::cout << argv[1] << std::endl;
std::cout << argv[2] << std::endl;
std::cout << argv[3] << std::endl;
window_size = atoi(argv[1]);
sample_size = atoi(argv[2]);
number_of_iterations = atoi(argv[3]);
std::cout << "run?" << std::endl;*/
// #endif // __ linux __
timings(algorithms, window_size, sample_size, number_of_iterations);
return EXIT_SUCCESS;
}