-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (208 loc) · 9.51 KB
/
main.cpp
File metadata and controls
255 lines (208 loc) · 9.51 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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <TGUI/Backend/SFML-Graphics.hpp>
#include <iostream>
#include <optional>
#include <vector>
#include <algorithm>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/chrono.hpp>
#include <omp.h>
#include "ThreeD_NumberRange.h"
#include "Helper.h"
#include "TestDistribution.h"
#include "UniformTest.h"
#include "NormalTest.h"
template<typename T>
void runTests(TestDistribution<T>* pTest) {
std::cout << "\n=== Test Results ===" << std::endl;
std::cout << "Min: " << pTest->getMin() << std::endl;
std::cout << "Max: " << pTest->getMax() << std::endl;
std::cout << "Median: " << pTest->getMedian() << std::endl;
std::cout << "Range: " << pTest->getNumberRange() << std::endl;
std::cout << "\n=== Histogram (20 buckets) ===" << std::endl;
auto histogram = pTest->getHistogram();
double minVal = pTest->getMin();
double maxVal = pTest->getMax();
double bucketSize = (maxVal - minVal) / 20.0;
for (int i = 0; i < 20; i++) {
double bucketStart = minVal + (i * bucketSize);
double bucketEnd = minVal + ((i + 1) * bucketSize);
std::cout << "Bucket " << i << " [" << bucketStart << " - " << bucketEnd << "]: "
<< histogram[i] << " items" << std::endl;
}
}
// Sequential version of statistics calculation
template<typename T>
void calculateStatsSequential(TestDistribution<T>* pTest) {
const std::vector<T>& data = pTest->getData();
// Sequential calculation (normal execution)
auto min = pTest->getMin();
auto max = pTest->getMax();
auto median = pTest->getMedian();
auto range = pTest->getNumberRange();
auto histogram = pTest->getHistogram();
volatile double dummy = 0;
for (size_t i = 0; i < data.size(); i++) {
// Some extra computation to show sequential timing
dummy += data[i] * data[i] / (data[i] + 1.0);
}
}
// Parallel version using OpenMP
template<typename T>
void calculateStatsParallel(TestDistribution<T>* pTest) {
const std::vector<T>& data = pTest->getData();
size_t dataSize = data.size();
if (dataSize == 0) return;
// Parallel min/max calculation
T minVal = data[0];
T maxVal = data[0];
#pragma omp parallel for reduction(min:minVal) reduction(max:maxVal)
for (int i = 0; i < data.size(); i++) {
if (data[i] < minVal) minVal = data[i];
if (data[i] > maxVal) maxVal = data[i];
}
volatile double dummy = 0;
#pragma omp parallel for reduction(+:dummy)
for (int i = 0; i < static_cast<int>(dataSize); i++) {
// Some extra computation to show parallel difference
dummy += data[i] * data[i] / (data[i] + 1.0);
}
// Parallel histogram calculation
std::vector<int> histogram(20, 0);
T range = maxVal - minVal;
T bucketSize = range / 20.0;
if (range > 0) {
#pragma omp parallel for
for (int i = 0; i < static_cast<int>(dataSize); i++) {
int bucketIndex = static_cast<int>((data[i] - minVal) / bucketSize);
if (bucketIndex >= 20) bucketIndex = 19;
#pragma omp atomic
histogram[bucketIndex]++;
}
}
}
int main()
{
// Create window
sf::RenderWindow window(sf::VideoMode({ 800, 600 }), "CST8219 Final Project - Statistics Analysis");
tgui::Gui gui(window);
try {
// Create input fields
auto samplesInput = tgui::EditBox::create();
samplesInput->setPosition(50, 50);
samplesInput->setSize(200, 30);
samplesInput->setDefaultText("Number of samples");
gui.add(samplesInput);
auto param1Input = tgui::EditBox::create();
param1Input->setPosition(50, 100);
param1Input->setSize(200, 30);
param1Input->setDefaultText("Min/Mean");
gui.add(param1Input);
auto param2Input = tgui::EditBox::create();
param2Input->setPosition(50, 150);
param2Input->setSize(200, 30);
param2Input->setDefaultText("Max/StdDev");
gui.add(param2Input);
// Create buttons
auto uniformButton = tgui::Button::create("Run Uniform Distribution Tests");
uniformButton->setPosition(300, 50);
uniformButton->setSize(250, 40);
gui.add(uniformButton);
auto normalButton = tgui::Button::create("Run Normal Distribution Tests");
normalButton->setPosition(300, 100);
normalButton->setSize(250, 40);
gui.add(normalButton);
// Create output display
auto outputBox = tgui::TextArea::create();
outputBox->setPosition(50, 200);
outputBox->setSize(700, 350);
outputBox->setReadOnly(true);
gui.add(outputBox);
// Uniform distribution button handler
uniformButton->onClick([&samplesInput, ¶m1Input, ¶m2Input, &outputBox]() {
try {
int samples = static_cast<int>(tgui::String(samplesInput->getText()).toFloat());
double minVal = tgui::String(param1Input->getText()).toFloat();
double maxVal = tgui::String(param2Input->getText()).toFloat();
std::cout << "\n=== UNIFORM DISTRIBUTION TEST ===" << std::endl;
std::cout << "Samples: " << samples << ", Min: " << minVal << ", Max: " << maxVal << std::endl;
// Create uniform test
UniformTest<double> uniformTest(samples);
uniformTest.generateData(minVal, maxVal);
// Sequential timing
auto start = boost::chrono::high_resolution_clock::now();
calculateStatsSequential(&uniformTest);
runTests(&uniformTest);
auto end = boost::chrono::high_resolution_clock::now();
auto sequential_duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start);
std::cout << "Sequential execution time: " << sequential_duration.count() << " ms" << std::endl;
// Parallel timing
start = boost::chrono::high_resolution_clock::now();
calculateStatsParallel(&uniformTest);
end = boost::chrono::high_resolution_clock::now();
auto parallel_duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start);
std::cout << "Parallel execution time: " << parallel_duration.count() << " ms" << std::endl;
outputBox->setText("Uniform distribution test completed. Check console for results.\n"
"Sequential time: " + std::to_string(sequential_duration.count()) + " ms\n"
"Parallel time: " + std::to_string(parallel_duration.count()) + " ms");
}
catch (...) {
outputBox->setText("Error: Invalid input for uniform distribution");
}
});
// Normal distribution button handler
normalButton->onClick([&samplesInput, ¶m1Input, ¶m2Input, &outputBox]() {
try {
int samples = static_cast<int>(tgui::String(samplesInput->getText()).toFloat());
double mean = tgui::String(param1Input->getText()).toFloat();
double stdDev = tgui::String(param2Input->getText()).toFloat();
std::cout << "\n=== NORMAL DISTRIBUTION TEST ===" << std::endl;
std::cout << "Samples: " << samples << ", Mean: " << mean << ", StdDev: " << stdDev << std::endl;
// Create normal test
NormalTest<double> normalTest(samples);
normalTest.generateData(mean, stdDev);
// Sequential timing
auto start = boost::chrono::high_resolution_clock::now();
calculateStatsSequential(&normalTest);
runTests(&normalTest);
auto end = boost::chrono::high_resolution_clock::now();
auto sequential_duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start);
std::cout << "Sequential execution time: " << sequential_duration.count() << " ms" << std::endl;
// Parallel timing
start = boost::chrono::high_resolution_clock::now();
calculateStatsParallel(&normalTest);
end = boost::chrono::high_resolution_clock::now();
auto parallel_duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(end - start);
std::cout << "Parallel execution time: " << parallel_duration.count() << " ms" << std::endl;
outputBox->setText("Normal distribution test completed. Check console for results.\n"
"Sequential time: " + std::to_string(sequential_duration.count()) + " ms\n"
"Parallel time: " + std::to_string(parallel_duration.count()) + " ms");
}
catch (...) {
outputBox->setText("Error: Invalid input for normal distribution");
}
});
std::cout << "Statistics Analysis GUI loaded successfully!" << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
// Main loop
while (window.isOpen()) {
std::optional<sf::Event> event;
while ((event = window.pollEvent())) {
gui.handleEvent(*event);
if (event->is<sf::Event::Closed>())
window.close();
}
window.clear(sf::Color::White);
gui.draw();
window.display();
}
return 0;
}