-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
203 lines (182 loc) · 7.34 KB
/
Source.cpp
File metadata and controls
203 lines (182 loc) · 7.34 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
#include <iostream>
#include <vector>
#include <iomanip> //Formatted output
#include <future> // This includes, <thread> and <time.h> as well.
using namespace std;
const double PI = 3.141592653589793;
double taskForPlusMinusEvenInterval(double starting, double end)
{
double ret = 0;
for (double i = starting; i < -end; i += 4)
{
ret += 1 / i;
ret -= 1 / (i + 2);
//cout << endl << " +" << i << " -" << i + 2;
}
return ret;
}
double taskForMinusPlusEvenInterval(double starting, double end)
{
double ret = 0;
for (double i = -starting; i < end; i += 4)
{
ret -= 1 / i;
ret += 1 / (i + 2);
//cout << endl << " -" << i << " +" << i + 2;
}
return ret;
}
double taskForMinusMinusOddInterval(double starting, double end)
{
double ret = 0;
double i = -starting;
for (; i < -end - 4; i += 4)
{
ret -= 1 / i;
ret += 1 / (i + 2);
//cout << endl << " -" << i << " +" << i + 2;
}
ret -= 1 / i;
//cout << endl << " -" << i;
return ret;
}
double taskForPlusPlusOddInterval(double starting, double end)
{
double ret = 0;
double i = starting;
for (; i < end - 4 ; i += 4)
{
ret += 1 / i;
ret -= 1 / (i+2);
//cout << endl << " +" << i << " -" << i + 2;
}
ret += 1 / i;
//cout << endl << " +" << i ;
return ret;
}
double makeCalculation(long long int numberOfThreads, long long int numberOfOperations)
{
vector<future<double>> valuesWillBeReturnedByThreads;
long long int intervalForEachThread = numberOfOperations / numberOfThreads * 2; //Going to be used for calculation the denominator
long long int totalIntervalToBeDistrubutedToThreads;
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now(); //For measuring the calculation time
if (numberOfThreads == 1) // No new thread will be created. All calculations will be done on the main thread
{
totalIntervalToBeDistrubutedToThreads = 0;
}
else // New threads will be created at the amount of user requested besides the main thread. So: Total thread number = user request + 1 (main thread)
{
totalIntervalToBeDistrubutedToThreads = intervalForEachThread * numberOfThreads;
}
//Distributing calculations to threads
if (intervalForEachThread / 2 % 2 == 0 && intervalForEachThread >= 2) //When number of calculations per thread is an EVEN number
{
// Since starting index is one there is only one possibility in terms of inputs' mark in sequences: "+,-"
for (long long int startIndexForAThread = 1; startIndexForAThread < totalIntervalToBeDistrubutedToThreads + 1; startIndexForAThread += intervalForEachThread)
{
valuesWillBeReturnedByThreads.push_back(async(&taskForPlusMinusEvenInterval, startIndexForAThread, -(startIndexForAThread + intervalForEachThread)));
}
}
else //When number of calculations per thread is an ODD number
{
bool changeMark = true;
for (long long int startIndexForAThread = 1; startIndexForAThread < totalIntervalToBeDistrubutedToThreads + 1; startIndexForAThread += intervalForEachThread)
{
// 2 possibility in terms of inputs' mark in sequences: "-,+,-" or "+,-,+"
if (changeMark)
{
valuesWillBeReturnedByThreads.push_back(async(&taskForPlusPlusOddInterval, startIndexForAThread, startIndexForAThread + intervalForEachThread));
changeMark = false;
}
else
{
valuesWillBeReturnedByThreads.push_back(async(&taskForMinusMinusOddInterval, -startIndexForAThread, -(startIndexForAThread + intervalForEachThread)));
changeMark = true;
}
}
}
//Calculation of remaining part which wasnt distrubuted to threads
vector<double> valuesCalculatedByMainThread;
long long int endOfWholeInterval = numberOfOperations * 2;
long long int numberOfOperationDoneByThreads = totalIntervalToBeDistrubutedToThreads / 2; // Needed for determining the mark of first element in the following calculations.
if (numberOfOperations % numberOfThreads != 0 || numberOfThreads == 1) // If there are remaining operations which wasnt distrubuted to threads or If user requested only 1 thread
{
if (numberOfOperationDoneByThreads % 2 == 0 && (numberOfOperations - numberOfOperationDoneByThreads) % 2 == 1)
{
valuesCalculatedByMainThread.push_back(taskForPlusPlusOddInterval(+(totalIntervalToBeDistrubutedToThreads + 1), endOfWholeInterval));
}
else if (numberOfOperationDoneByThreads % 2 == 1 && (numberOfOperations - numberOfOperationDoneByThreads) % 2 == 1)
{
valuesCalculatedByMainThread.push_back(taskForMinusMinusOddInterval(-(totalIntervalToBeDistrubutedToThreads + 1), -endOfWholeInterval));
}
else if (numberOfOperationDoneByThreads % 2 == 0 && (numberOfOperations - numberOfOperationDoneByThreads) % 2 == 0)
{
valuesCalculatedByMainThread.push_back(taskForPlusMinusEvenInterval(+(totalIntervalToBeDistrubutedToThreads + 1), -endOfWholeInterval));
}
else if (numberOfOperationDoneByThreads % 2 == 1 && (numberOfOperations - numberOfOperationDoneByThreads) % 2 == 0)
{
valuesCalculatedByMainThread.push_back(taskForMinusPlusEvenInterval(-(totalIntervalToBeDistrubutedToThreads + 1), endOfWholeInterval));
}
}
//Calculation of Final Result
double result = 0;
for (long long int i = 0; i < valuesCalculatedByMainThread.size(); i++)
{
result += valuesCalculatedByMainThread[i];
}
for (long long int i = 0; i < valuesWillBeReturnedByThreads.size(); i++)
{
result += valuesWillBeReturnedByThreads[i].get();
}
result *= 4.00;
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout.precision(15);
cout << "Duration of the computation: " << left << setw(10) << time_span.count() << " seconds for " << setw(2) << numberOfThreads << " threads. Result: " << result << endl;
return result;
}
int main()
{
cout << "Going to calculate PI number with an approximation:" << endl;
cout << "By using Tylor Series Method (PI/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 ...)" << endl;
cout << "With implementation of multi-threaded programming." << endl << endl;
cout << "Type \"1\" for using program with your own inputs." << endl;
cout << "Type \"2\" for executing auto test: Running 1 to 64 threads for 1m operations." << endl;
char input;
cin >> input;
long long int numberOfThreads;
long long int numberOfOperations;
double result;
if (input == '1')
{
cout << "Enter number of operations: ";
cin >> numberOfOperations;
cout << "Enter number of threads: ";
cin >> numberOfThreads;
while (numberOfThreads > numberOfOperations)
{
cout << endl << "Thread number can not be bigger than number of operations. Enter number of threads: ";
cin >> numberOfThreads;
}
result = makeCalculation(numberOfThreads, numberOfOperations);
cout.precision(15);
cout << endl << "Result: " << result;
cout << endl << "PI: " << PI << endl;
cout << endl << "Approximation Error: Result - PI = " << result - PI;
cout << endl << "Approximation Error: PI - Result = " << PI - result << endl;
}
else if (input == '2')
{
int threadNo = 0;
cout.precision(15);
cout << "------------------------------------------------------------------- PI: " << PI << endl;
while (threadNo<64)
{
threadNo++;
makeCalculation(threadNo, 1000000);
}
}
char end;
cin >> end;
return 0;
}