-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEST3.cpp
218 lines (182 loc) · 5.46 KB
/
TEST3.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
#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <omp.h>
#define all(c) c.begin(), c.end()
using namespace std;
class Time {
private:
int Year;
int Month;
int Day;
int Hours;
int Minutes;
double Seconds;
double All_Seconds;
int round(double x) {
return floor(x + 0.5);
}
void Clear() {
this->Year = 0;
this->Month = 0;
this->Day = 0;
this->Hours = 0;
this->Minutes = 0;
this->Seconds = 0;
this->All_Seconds = 0;
}
public:
void Modify(bool From_All_Seconds = false) {
if (From_All_Seconds)
this->Seconds = this->All_Seconds;
if (this->Seconds >= 60) {
this->Minutes += round(this->Seconds / 60);
this->Seconds -= round(this->Seconds / 60);
}
if (this->Minutes >= 60) {
this->Hours += this->Minutes / 60;
this->Minutes %= 60;
}
if (this->Hours >= 60) {
this->Day += this->Hours / 60;
this->Hours %= 60;
}
if (this->Day >= 30) {
this->Month += this->Day / 30;
this->Day %= 30;
}
if (this->Month >= 12) {
this->Year += this->Month / 12;
this->Month %= 12;
}
this->All_Seconds = Seconds + 60*(this->Minutes + 60*(this->Hours + 24*(this->Day + 30*(this->Month + 12*this->Year))));
}
double Get_All_Seconds() {
Modify();
return this->All_Seconds;
}
void Set_All_Seconds(const double &_Seconds = 0) {
this->All_Seconds = _Seconds;
Modify(true);
}
void Set_Time(const int &_Year = 0, const int &_Month = 0, const int &_Day = 0, const int &_Hours = 0, const int &_Minutes = 0, const double &_Seconds = 0) {
this->Year = _Year;
this->Month = _Month;
this->Day = _Day;
this->Hours = _Hours;
this->Minutes = _Minutes;
this->Seconds = _Seconds;
Modify();
}
Time(const int &_Year, const int &_Month, const int &_Day, const int &_Hours, const int &_Minutes, const double &_Seconds) {
Clear();
Set_Time(_Year, _Month, _Day, _Hours, _Minutes, _Seconds);
}
Time(const double &_Seconds = 0) {
Clear();
Set_All_Seconds(_Seconds);
}
~Time() {
Clear();
}
friend Time operator + (Time &T1, Time &T2) {
return Time(T1.Get_All_Seconds() + T2.Get_All_Seconds());
}
friend Time operator - (Time &T1, Time &T2) {
if (T1.Get_All_Seconds() < T2.Get_All_Seconds()) {
cerr << "Negative time!";
throw -1;
}
return Time(T1.Get_All_Seconds() - T2.Get_All_Seconds());
}
friend Time operator * (double m, Time &T) {
return Time(m*T.Get_All_Seconds());
}
friend std::ostream& operator << (std::ostream& outputstream, Time &T) {
outputstream << T.Year << "y" << T.Month << "m" << T.Day << "d" << T.Hours << "h" << T.Minutes << "m" << T.Seconds << "s";
return outputstream;
}
friend std::istream& operator >> (std::istream& inputstream, Time &T) {
double _Seconds;
inputstream >> _Seconds;
T.Set_All_Seconds(_Seconds);
return inputstream;
}
friend bool operator == (Time &left, Time &right) {
return (left.Get_All_Seconds() == right.Get_All_Seconds());
}
friend bool operator != (Time &left, Time &right) {
return (!(left == right));
}
friend bool operator < (Time &left, Time &right) {
return (left.Get_All_Seconds() < right.Get_All_Seconds());
}
friend bool operator > (Time &left, Time &right) {
return (left.Get_All_Seconds() > right.Get_All_Seconds());
}
};
class Experiment_Data {
public:
Time T;
int Threads_Ammount;
};
bool comparator(Experiment_Data D1, Experiment_Data D2) {
return (D1.T > D2.T);
}
class Experiment {
private:
int Array_size;
int Threads_max_ammount;
vector < int > A;
vector < int > B;
vector < int > C;
vector < Experiment_Data > Data;
void Fill_Default_Arrays() {
this->A.resize(this->Array_size);
this->B.resize(this->Array_size);
this->C.clear();
for (int i = 0; i < this->Array_size; i++) {
this->A[i] = i;
this->B[i] = this->Array_size - i;
}
}
void Run_Sum(int Threads) {
C.clear();
C.resize(Array_size);
omp_set_num_threads(Threads);
int i;
Experiment_Data Current_Data;
Current_Data.Threads_Ammount = Threads;
Time T1(omp_get_wtime());
#pragma omp parallel for
for (i = 0; i < Array_size; i++)
C[i] = A[i] + B[i];
Time T2(omp_get_wtime());
Current_Data.T = T2 - T1;
Data.push_back(Current_Data);
}
public:
void Run() {
Fill_Default_Arrays();
int Threads = 1;
while (Threads <= this->Threads_max_ammount) {
Run_Sum(Threads);
Threads *= 2;
}
sort(all(Data), comparator);
for (size_t i = 0; i < Data.size(); i++)
cout << "Threads:" << Data[i].Threads_Ammount << ", Time:" << Data[i].T << endl;
}
Experiment(int _Array_size = 1000000, int _Threads_max_ammount = 16) {
this->Array_size = _Array_size;
this->Threads_max_ammount = _Threads_max_ammount;
}
~Experiment() {
}
};
int main() {
Experiment Ex;
Ex.Run();
return 0;
}