-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
358 lines (334 loc) · 12.2 KB
/
main.cpp
File metadata and controls
358 lines (334 loc) · 12.2 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
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include "Algorithm.cpp"
bool verbose = false;
bool iterative = false;
bool jsonOutput = false;
int testSize = 1;
struct AlgorithmType {
enum Type {
BUBBLE_SORT,
QUICK_SORT,
MERGE_SORT,
INSERTION_SORT,
SELECTION_SORT,
HEAP_SORT,
MATRIX_MULTIPLICATION,
MATRIX_ADDITION,
MATRIX_TRANSPOSE,
LINEAR_SEARCH,
BINARY_SEARCH,
UNKNOWN
};
};
AlgorithmType::Type getAlgorithmType(const std::string &algorithm) {
if (algorithm == "bubble_sort") {
return AlgorithmType::BUBBLE_SORT;
}
if (algorithm == "quick_sort") {
return AlgorithmType::QUICK_SORT;
}
if (algorithm == "merge_sort") {
return AlgorithmType::MERGE_SORT;
}
if (algorithm == "insertion_sort") {
return AlgorithmType::INSERTION_SORT;
}
if (algorithm == "selection_sort") {
return AlgorithmType::SELECTION_SORT;
}
if (algorithm == "heap_sort") {
return AlgorithmType::HEAP_SORT;
}
if (algorithm == "matrix_multiplication") {
return AlgorithmType::MATRIX_MULTIPLICATION;
}
if (algorithm == "matrix_addition") {
return AlgorithmType::MATRIX_ADDITION;
}
if (algorithm == "matrix_transpose") {
return AlgorithmType::MATRIX_TRANSPOSE;
}
if (algorithm == "linear_search") {
return AlgorithmType::LINEAR_SEARCH;
}
if (algorithm == "binary_search") {
return AlgorithmType::BINARY_SEARCH;
}
std::cerr << "Unknown algorithm '" << algorithm << "'.\n";
return AlgorithmType::UNKNOWN;
}
Measurement selectAlgorithm(const std::string& algorithm, int threadCount, long long dataSize) {
Algorithm* algo = nullptr;
AlgorithmType::Type type = getAlgorithmType(algorithm);
switch (type) {
case AlgorithmType::BUBBLE_SORT:
algo = new BubbleSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::QUICK_SORT:
algo = new QuickSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::MERGE_SORT:
algo = new MergeSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::INSERTION_SORT:
algo = new InsertionSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::SELECTION_SORT:
algo = new SelectionSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::HEAP_SORT:
algo = new HeapSort(threadCount, dataSize, verbose, &iterative);
break;
case AlgorithmType::MATRIX_MULTIPLICATION:
algo = new MatrixMultiplication(threadCount, dataSize, verbose);
break;
case AlgorithmType::MATRIX_ADDITION:
algo = new MatrixAddition(threadCount, dataSize, verbose);
break;
case AlgorithmType::MATRIX_TRANSPOSE:
algo = new MatrixTransposition(threadCount, dataSize, verbose);
break;
case AlgorithmType::LINEAR_SEARCH:
algo = new LinearSearch(threadCount, dataSize, verbose);
break;
case AlgorithmType::BINARY_SEARCH:
algo = new BinarySearch(threadCount, dataSize, verbose);
break;
default:
std::cerr << "Error: Unsupported or unknown algorithm '" << algorithm << "'.\n";
return Measurement();
}
Measurement result = algo->executeAndMeasure(threadCount, dataSize);
if (!jsonOutput) {
std::cout << result.toString() << std::endl;
}
delete algo;
return result;
}
void runAlgorithm(const std::string& algorithm, int fireStart, int fireEnd, int sizeStart, int sizeEnd) {
// limit the size of the data to LLONG_MAX
sizeStart = std::ranges::min(sizeStart, 63);
sizeStart = std::ranges::max(sizeStart, 0);
sizeEnd = std::ranges::max(sizeEnd, 0);
sizeEnd = std::ranges::min(sizeEnd, 63);
// swap if size start is greater than size end
if (sizeStart > sizeEnd) {
std::swap(sizeStart, sizeEnd);
}
fireStart = std::ranges::max(fireStart, 0);
fireEnd = std::ranges::max(fireEnd, 0);
fireStart = std::ranges::min(fireStart, 31);
fireEnd = std::ranges::min(fireEnd, 31);
// swap if fire start is greater than fire end
if (fireStart > fireEnd) {
std::swap(fireStart, fireEnd);
}
if (!jsonOutput) {
std::cout << "Running " << algorithm << " with varying threads and data sizes...\n";
}
nlohmann::json allResults = nlohmann::json::array();
for (int i = sizeStart; i <= sizeEnd; ++i) {
auto dataSize = static_cast<long long>(pow(2, i));
for (int j = fireStart; j <= fireEnd; ++j) {
int numThreads = static_cast<int>(pow(2, j));
nlohmann::json jsonObject;
jsonObject["algorithm"] = algorithm;
jsonObject["threads"] = numThreads;
jsonObject["data_size"] = dataSize;
// Call selectAlgorithm and capture its output if necessary, also redo the measurements testSize times
// and return the average
int successFullTests = 0;
Measurement finalResult = selectAlgorithm(algorithm, numThreads, dataSize);
for (int k = 0; k < testSize-1; ++k) {
Measurement result = selectAlgorithm(algorithm, numThreads, dataSize);
if (result == Measurement()) {
continue;
}
finalResult += result;
successFullTests++;
}
finalResult.duration = finalResult.duration / testSize;
// Measurement result = selectAlgorithm(algorithm, numThreads, dataSize);
// if (result == Measurement()) {
// continue;
// }
jsonObject["test_count"] = successFullTests + 1;
jsonObject["result"] = finalResult.toJson();
// Accumulate all results in a single array
allResults.push_back(jsonObject);
}
}
// Output the entire JSON structure at the end
if (jsonOutput) {
std::cout << allResults.dump(4) << std::endl;
} else {
for (const auto& result : allResults) {
std::cout << "Algorithm: " << result["algorithm"]
<< ", Threads: " << result["threads"]
<< ", Data size: " << result["data_size"] << std::endl;
}
}
}
void testAlgorithm(const std::string& algorithm, int fireStart, int fireEnd) {
if (!jsonOutput)
{
std::cout << "Testing " << algorithm << " with varying threads and data sizes...\n";
}
runAlgorithm(algorithm, fireStart, fireEnd, 5, 10);
}
void analyzeAlgorithm(const std::string& algorithm) {
if (!jsonOutput) {
std::cout << "Analyzing performance for " << algorithm << " with various configurations...\n";
}
testAlgorithm(algorithm, 0, 4);
}
void toggleVerbose(bool enableVerbose) {
verbose = enableVerbose;
std::cout << "Verbose mode " << (verbose ? "enabled" : "disabled") << ".\n";
}
void showHelp() {
std::cout << "Available commands:\n";
std::cout << "run <algorithm> <fire_start> <fire_end> <data_size_start> <data_size_end>\n";
std::cout << "test <algorithm> <fire_start> <fire_end>\n";
std::cout << "analyze <algorithm>\n";
std::cout << "verbose <true/false>\n";
std::cout << "help\n";
}
bool isValidPositive(int value) {
return value >= 0;
}
void processCommand(const std::string& commandLine) {
std::istringstream ss(commandLine);
std::string command;
ss >> command;
if (command == "run") {
std::string algorithm;
int fireStart, fireEnd, sizeStart, sizeEnd;
if (ss >> algorithm >> fireStart >> fireEnd >> sizeStart >> sizeEnd) {
if (isValidPositive(fireStart) && isValidPositive(fireEnd) &&
isValidPositive(sizeStart) && isValidPositive(sizeEnd)) {
runAlgorithm(algorithm, fireStart, fireEnd, sizeStart, sizeEnd);
} else {
std::cerr << "Error: Parameters for 'run' must be positive numbers.\n";
}
} else {
std::cerr << "Error: Invalid parameters for 'run' command.\n";
}
} else if (command == "test") {
std::string algorithm;
int fireStart, fireEnd;
if (ss >> algorithm >> fireStart >> fireEnd) {
if (isValidPositive(fireStart) && isValidPositive(fireEnd)) {
testAlgorithm(algorithm, fireStart, fireEnd);
} else {
std::cerr << "Error: Parameters for 'test' must be positive numbers.\n";
}
} else {
std::cerr << "Error: Invalid parameters for 'test' command.\n";
}
} else if (command == "analyze") {
std::string algorithm;
if (ss >> algorithm) {
analyzeAlgorithm(algorithm);
} else {
std::cerr << "Error: Invalid parameters for 'analyze' command.\n";
}
} else if (command == "verbose") {
std::string state;
if (ss >> state) {
if (state == "true") {
toggleVerbose(true);
} else if (state == "false") {
toggleVerbose(false);
} else {
std::cerr << "Error: Invalid parameter for 'verbose' command. Use 'true' or 'false'.\n";
}
} else {
std::cerr << "Error: Missing parameter for 'verbose' command.\n";
}
}
else if (command == "iterative") {
std::string state;
if (ss >> state) {
if (state == "true") {
iterative = true;
std::cout << "Iterative mode enabled.\n";
} else if (state == "false") {
iterative = false;
std::cout << "Iterative mode disabled.\n";
} else {
std::cerr << "Error: Invalid parameter for 'verbose' command. Use 'true' or 'false'.\n";
}
} else {
std::cerr << "Error: Missing parameter for 'verbose' command.\n";
}
}
else if (command == "help") {
showHelp();
} else if (command == "json_output") {
std::string state;
if (ss >> state) {
if (state == "true") {
jsonOutput = true;
std::cout << "JSON output enabled.\n";
} else if (state == "false") {
jsonOutput = false;
std::cout << "JSON output disabled.\n";
} else {
std::cerr << "Error: Invalid parameter for 'json_output' command. Use 'true' or 'false'.\n";
}
} else {
std::cerr << "Error: Missing parameter for 'json_output' command.\n";
}
}
else {
std::cerr << "Error: Unknown command.\n";
}
}
int main_infinite() {
processCommand("verbose true");
processCommand("json_output false");
processCommand("analyze heap_sort");
std::string input;
if (!jsonOutput) {
std::cout << "Enter a command (type 'help' for instructions):\n";
}
while (std::getline(std::cin, input)) {
processCommand(input);
if (!jsonOutput) {
std::cout << "Enter a command (type 'help' for instructions):\n";
}
}
return 0;
}
int main(int argc, char* argv[]) {
if (argc > 1) {
std::string commandLine;
for (int i = 1; i < argc; ++i) {
commandLine += argv[i];
if (i < argc - 1) {
commandLine += " ";
}
if (argv[i] == "--use-iterative") {
iterative = true;
}
// --repeat=INT is used to repeat the test INT times
if (std::string(argv[i]).find("--repeat=") != std::string::npos) {
std::string repeat = std::string(argv[i]).substr(9);
testSize = std::stoi(repeat);
}
}
jsonOutput = true;
// check at the end if the command is iterative by checking if the last word is --use-iterative
if (commandLine.find("--use-iterative") != std::string::npos) {
iterative = true;
}
processCommand(commandLine);
} else {
main_infinite();
}
return 0;
}