-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.cpp
More file actions
374 lines (314 loc) · 13.3 KB
/
runner.cpp
File metadata and controls
374 lines (314 loc) · 13.3 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
#include <iostream>
#include <getopt.h>
#include <cstdlib>
#include <optional>
#include <variant>
#include <charconv>
#include <string>
#include <cstdint>
#include "program_runner.hpp"
#include "prng.hpp"
#include "xorshift.hpp"
#include "linear_congruential_generator.hpp"
#include "mersenne_twister.hpp"
std::string ProgramRunner::error_string() {
std::string error_string = program_name + ": bad usage\n"
+ "Try '" + program_name + " --help' for more information.";
return error_string;
}
std::string ProgramRunner::help_string() {
std::string help_string = "Usage: " + program_name + " [OPTIONS]\n\n"
+ "Options:\n"
+ " -a ALGORITHM, --algorithm=ALGORITHM Specify the algorithm [default: xorshift]\n"
+ " -c AMOUNT, --count=AMOUNT Amount of random numbers to generate [default: 1]\n"
+ " -M NUMBER, --max=NUMBER Maximum value\n"
+ " -m NUMBER, --min=NUMBER Minimum value\n"
+ " -t TYPE, --type=TYPE Specify the output type [default: int]\n"
+ " -h, --help Display this help and exit\n"
+ " -v, --version Output version information and exit";
return help_string;
}
std::string ProgramRunner::version_string() {
std::string version_string = program_name + " " + version;
return version_string;
}
template <typename T>
concept FromCharsParsable = std::is_integral_v<T> || std::is_floating_point_v<T>;
// Helper function for parsing a string to a numeric type using std::from_chars
template <FromCharsParsable T>
std::optional<T> parse_value(const std::string& str) {
T value;
auto result = std::from_chars(str.data(), str.data() + str.size(), value);
if (result.ec != std::errc() || result.ptr != str.data() + str.size()) {
return std::nullopt;
}
return value;
}
template <FromCharsParsable T>
std::optional<std::pair<T, T>> parse_min_and_max_numbers(const std::string &min_str, const std::string &max_str) {
std::optional<T> min_value = parse_value<T>(min_str);
std::optional<T> max_value = parse_value<T>(max_str);
if (!min_value.has_value() || !max_value.has_value()) {return std::nullopt;}
if (min_value.value() <= max_value.value()) {return std::make_pair(min_value.value(), max_value.value());}
return std::nullopt;
}
void ProgramRunner::determine_generation_range_configuration(const std::optional<std::string> &min_str, const std::optional<std::string> &max_str){
bool both_max_and_min_specified = min_str.has_value() && max_str.has_value();
bool neither_max_or_min_specified = !min_str.has_value() && !max_str.has_value();
if (both_max_and_min_specified && this->behaviour == ProgramBehaviour::GenerateInteger){
auto min_and_max = parse_min_and_max_numbers<int32_t>(min_str.value(), max_str.value());
if (!min_and_max.has_value()){
this->behaviour = ProgramBehaviour::Error;
return;
}
this->min = min_and_max.value().first;
this->max = min_and_max.value().second;
} else if (both_max_and_min_specified && this->behaviour == ProgramBehaviour::GenerateFloating){
auto min_and_max = parse_min_and_max_numbers<float>(min_str.value(), max_str.value());
if (!min_and_max.has_value()){
this->behaviour = ProgramBehaviour::Error;
return;
}
this->min = min_and_max.value().first;
this->max = min_and_max.value().second;
} else if (neither_max_or_min_specified && this->behaviour == ProgramBehaviour::GenerateUnitNormal){
return;
} else {
this->behaviour = ProgramBehaviour::Error;
return;
}
}
void ProgramRunner::determine_user_message_configuration(const bool error, const bool show_version, const bool show_help){
if (error){
this->behaviour = ProgramBehaviour::Error;
return;
}
if (show_version){
this->behaviour = ProgramBehaviour::Version;
return;
}
if (show_help){
this->behaviour = ProgramBehaviour::Help;
return;
}
}
void ProgramRunner::determine_algorithm_configuration(const std::optional<std::string> &alg_str){
if (!alg_str.has_value()){
this->algorithm = Algorithm::XORShift; // default algorithm to use
} else if (algorithm_choices.contains(alg_str.value())) { // TODO: this check/get can be made more efficient using find
this->algorithm = algorithm_choices.at(alg_str.value());
} else {
this->behaviour = ProgramBehaviour::Error;
}
}
void ProgramRunner::determine_generation_type_configuration(const std::optional<std::string> &generation_type){
if (!generation_type.has_value()){
this->behaviour = DefaultGenerationType;
} else if (generation_types.contains(generation_type.value())){
this->behaviour = generation_types.at(generation_type.value());
} else {
this->behaviour = ProgramBehaviour::Error;
}
}
void ProgramRunner::determine_count_configuration(const std::optional<std::string> &count_str){
if (count_str.has_value()){
std::optional<uint32_t> count_val = parse_value<uint32_t>(count_str.value());
if (!count_val.has_value() || count_val.value() <= 0){
this->behaviour = ProgramBehaviour::Error;
return;
}
this->count = count_val;
} else {
this->count = DefaultCount;
}
}
void ProgramRunner::determine_program_configuration(const ProgramRunner::RawArguments &raw_args){
determine_user_message_configuration(raw_args.error, raw_args.show_version, raw_args.show_help);
auto &behave = this->behaviour;
if (behave.has_value() && (behave == ProgramBehaviour::Error || behave == ProgramBehaviour::Version || behave == ProgramBehaviour::Help)){
return;
}
determine_algorithm_configuration(raw_args.algorithm_str);
if (behave.has_value() && behave == ProgramBehaviour::Error){
return;
}
determine_generation_type_configuration(raw_args.type);
if (behave.has_value() && behave == ProgramBehaviour::Error){
return;
}
determine_count_configuration(raw_args.count_str);
if (behave.has_value() && behave == ProgramBehaviour::Error){
return;
}
determine_generation_range_configuration(raw_args.min_str, raw_args.max_str);
}
ProgramRunner::RawArguments ProgramRunner::parse_args(int argc, char **argv) {
const char* const short_opts = "+hva:m:M:c:t:";
const ::option long_opts[] = { // NOLINT (cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
{"algorithm", required_argument, nullptr, 'a'},
{"min", required_argument, nullptr, 'm'},
{"max", required_argument, nullptr, 'M'},
{"count", required_argument, nullptr, 'c'},
{"type", required_argument, nullptr, 't'},
{nullptr, 0, nullptr, 0}
};
RawArguments raw_arguments;
raw_arguments.error = false;
raw_arguments.show_help = false;
raw_arguments.show_version = false;
raw_arguments.algorithm_str = std::nullopt;
raw_arguments.min_str = std::nullopt;
raw_arguments.max_str = std::nullopt;
raw_arguments.count_str = std::nullopt;
raw_arguments.type = std::nullopt;
while (true) {
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr); // NOLINT (cppcoreguidelines-pro-bounds-array-to-pointer-decay)
if (opt == -1){
break;
}
switch (opt) {
case 'h':
raw_arguments.show_help = true;
break;
case 'v':
raw_arguments.show_version = true;
break;
case 'a':
raw_arguments.algorithm_str = std::string(optarg);
break;
case 'm':
raw_arguments.min_str = std::string(optarg);
break;
case 'M':
raw_arguments.max_str = std::string(optarg);
break;
case 'c':
raw_arguments.count_str = std::string(optarg);
break;
case 't':
raw_arguments.type = std::string(optarg);
break;
default:
raw_arguments.error = true;
return raw_arguments;
}
}
return raw_arguments;
}
void ProgramRunner::create_prng() {
if (!this->algorithm.has_value()) {
throw std::runtime_error("Algorithm not set, cannot create PseudoRandomNumberGenerator instance");
}
switch (this->algorithm.value()) {
case ProgramRunner::Algorithm::XORShift:
this->prng = std::make_unique<XORShift>();
break;
case ProgramRunner::Algorithm::LinearCongruentialGenerator:
this->prng = std::make_unique<LinearCongruentialGenerator>();
break;
case ProgramRunner::Algorithm::MersenneTwister:
this->prng = std::make_unique<MersenneTwister>();
break;
default:
throw std::runtime_error("Unsupported algorithm");
}
if (this->prng == nullptr) {
throw std::runtime_error("Failed to create PseudoRandomNumberGenerator instance");
}
}
ProgramRunner::ProgramRunner(int argc, char **argv, uint64_t warmup_iterations){
RawArguments raw_arguments = parse_args(argc, argv);
determine_program_configuration(raw_arguments);
if (this->algorithm.has_value()) {
create_prng();
warmup(warmup_iterations);
}
}
bool ProgramRunner::is_finished() {
if (this->finished) {
return true;
}
if ((this->count.has_value()) && (this->iteration >= this->count)) {
this->finished = true;
return true;
}
return false;
}
ProgramRunner::ProgramStatus ProgramRunner::iterate() {
if (is_finished()) {
throw std::runtime_error("ProgramRunner has finished, cannot iterate further");
}
if (!this->behaviour.has_value()) {
throw std::runtime_error("ProgramRunner not configured properly");
}
switch (this->behaviour.value()) {
case ProgramBehaviour::Error:
this->finished = true;
return {std::nullopt, error_string(), ProgramRunner::ExitCodeError};
case ProgramBehaviour::Help:
this->finished = true;
return {help_string(), std::nullopt, ProgramRunner::ExitCodeSuccess};
case ProgramBehaviour::Version:
this->finished = true;
return {version_string(), std::nullopt, ProgramRunner::ExitCodeSuccess};
case ProgramBehaviour::GenerateUnitNormal: {
this->iteration++;
double random_value = this->prng->generateUnitNormalRandomValue();
auto exit_code_based_on_count = is_finished() ? std::optional<int>(ProgramRunner::ExitCodeSuccess) : std::nullopt;
return {std::to_string(random_value), std::nullopt, exit_code_based_on_count};
}
case ProgramBehaviour::GenerateFloating: {
this->iteration++;
double random_float = this->prng->generateFloatingPointRandomValue(
std::get<float>(this->min.value_or(0.0F)),
std::get<float>(this->max.value_or(1.0F))
);
auto exit_code_based_on_count = is_finished() ? std::optional<int>(ProgramRunner::ExitCodeSuccess) : std::nullopt;
return {std::to_string(random_float), std::nullopt, exit_code_based_on_count};
}
case ProgramBehaviour::GenerateInteger: {
this->iteration++;
int64_t random_int = this->prng->generateIntegerRandomValue(
std::get<int32_t>(this->min.value_or(0)),
std::get<int32_t>(this->max.value_or(1))
);
auto exit_code_based_on_count = is_finished() ? std::optional<int>(ProgramRunner::ExitCodeSuccess) : std::nullopt;
return {std::to_string(random_int), std::nullopt, exit_code_based_on_count};
}
default:
throw std::runtime_error("ProgramRunner not configured properly, behaviour is not set");
}
return {std::nullopt, std::nullopt, ProgramRunner::ExitCodeError}; // Should never reach here
}
ProgramRunner::ProgramStatus ProgramRunner::iterate(uint64_t iterations) {
if (iterations == 0) {
throw std::invalid_argument("Number of iterations must be greater than zero");
}
ProgramStatus last_status;
for (uint64_t i = 0; i < iterations; ++i) {
last_status = iterate();
if (last_status.exit_code.has_value()) {
return last_status; // Return immediately if an exit code is present
}
}
return last_status; // Return the status of the last iteration
}
ProgramRunner::ProgramStatus ProgramRunner::run() {
while (!is_finished()) {
auto status = iterate();
if (status.exit_code.has_value()) {
return status;
}
}
throw std::runtime_error("ProgramRunner has finished without returning an exit code. This should not happen.");
}
void ProgramRunner::warmup(uint64_t iterations) {
if (this->prng == nullptr) {
throw std::runtime_error("PRNG is not initialized, cannot warmup");
}
for (uint64_t i = 0; i < iterations; ++i) {
this->prng->generateRandomValue(); // Warmup the PRNG
}
}