-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChudnovskyPiBS.cpp
307 lines (281 loc) · 10.3 KB
/
ChudnovskyPiBS.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
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
#include <thread>
#include <future>
#include <barrier>
#include "ChudnovskyPiBS.h"
#include <sstream>
#include <Windows.h> //this should be the last import somehow?
//Global variables for configuration:
const int NUM_THREADS_IN_CPU = std::thread::hardware_concurrency();
const int NUM_MAIN_WORKER_THREADS = NUM_THREADS_IN_CPU;
const int TOTAL_THREAD_COUNT = ChudnovskyPiBS::getTotalNumThreadsFromUsefulNumThreads(NUM_MAIN_WORKER_THREADS);
std::barrier ALL_THREADS_SPAWNED(NUM_MAIN_WORKER_THREADS);
//Actual definitions:
ChudnovskyPiBS::ChudnovskyPiBS(unsigned long _digits)
{
digits = _digits;
if (_digits != (unsigned long)((long double)_digits))
throw _EXCEPTION_; //precision can't be handled by long double.
N = (unsigned long)(digits / DIGITS_PER_TERM + 1);
futSqrtC = std::async(std::launch::async, &ChudnovskyPiBS::getSqrtC, this, digits);
mpz_ui_pow_ui(intBigC3_OVER_24.get_mpz_t(), C, 3);
mpz_class twentyfour = 24;
mpz_fdiv_q(intBigC3_OVER_24.get_mpz_t(), intBigC3_OVER_24.get_mpz_t(), twentyfour.get_mpz_t());
//intBigC3_OVER_24.get_str();
}
bsReturn ChudnovskyPiBS::bs(mpz_class a, mpz_class b) //clearly thread safe because nothing from outside the function is written to.
{
bsReturn result;
mpz_class Pab;
mpz_class Qab;
mpz_class Tab;
mpz_class m;
if (b - a == 1)
{
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
}
else
{
recursivelyComputePabQabTab_SingleThreaded(a, b, m, Pab, Qab, Tab);
}
result.P = Pab;
result.Q = Qab;
result.T = Tab;
//std::string pabStr = result.P.get_str();
//std::string qabStr = result.Q.get_str();
//std::string tabStr = result.T.get_str();
return result;
}
void ChudnovskyPiBS::directlyCompute__P_Q_T__from_A_to_AplusOne(mpz_class& a, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab)
{
//Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
if (a == 0)
Pab = Qab = 1;
else
{
Pab = (6 * a - 5) * (2 * a - 1) * (6 * a - 1);
Qab = a * a * a * intBigC3_OVER_24;
}
Tab = Pab * (13591409 + 545140134 * a); // a(a) * p(a)
//Pab.get_str();
//Tab.get_str();
//a.get_str();
//Qab.get_str();
mpz_class toCheck;
mpz_and(toCheck.get_mpz_t(), a.get_mpz_t(), one);
if (toCheck == 1) //note to self: works as expected
Tab = -Tab;
}
void ChudnovskyPiBS::recursivelyComputePabQabTab_SingleThreaded(mpz_class& a, mpz_class& b, mpz_class& m, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab)
{
// Recursively compute P(a,b), Q(a,b) and T(a,b)
// m is the midpoint of and b
mpz_class aplusb = a + b;
mpz_div_ui(m.get_mpz_t(), aplusb.get_mpz_t(), 2);//equivalent pseudocode: m=floor((a+b)/2)
// Recursively calculate P(a, m), Q(a, m) and T(a, m)
bsReturn am = bs(a, m);
// Recursively calculate P(m, b), Q(m, b) and T(m, b)
bsReturn mb = bs(m, b);
// Now combine
Pab = am.P * mb.P;
Qab = am.Q * mb.Q;
Tab = (mb.Q * am.T) + (am.P * mb.T);
}
bsReturn ChudnovskyPiBS::bs_multithreaded(mpz_class a, mpz_class b, int threadCount) //clearly thread safe because nothing from outside the function is written to.
{
bsReturn result;
mpz_class Pab;
mpz_class Qab;
mpz_class Tab;
mpz_class m;
if (b - a == 1)
{
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
}
else
{
// Recursively compute P(a,b), Q(a,b) and T(a,b)
// m is the midpoint of and b
mpz_class aplusb = a + b;
mpz_div_ui(m.get_mpz_t(), aplusb.get_mpz_t(), 2);//equivalent pseudocode: m=floor((a+b)/2)
// Recursively calculate P(a, m), Q(a, m) and T(a, m)
bsReturn am; am.Q = NULL;
std::future<bsReturn> futAm;
if (threadCount > 0)
{
futAm = std::async(std::launch::async, &ChudnovskyPiBS::bs_multithreaded, this, a, m, (--threadCount - 1) / 2);
}
else
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
am = bs(a, m);
}
// Recursively calculate P(m, b), Q(m, b) and T(m, b)
bsReturn mb; mb.Q = NULL;
std::future<bsReturn> futMb;
if (threadCount > 0)
{
futMb = std::async(std::launch::async, &ChudnovskyPiBS::bs_multithreaded, this, m, b, (--threadCount) / 2);
}
else
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
mb = bs(m, b);
}
//Wait for threads:
threadCount -= 2;
if (am.Q == NULL)
am = futAm.get();
if (mb.Q == NULL)
mb = futMb.get();
// Now combine
Pab = am.P * mb.P;
Qab = am.Q * mb.Q;
Tab = (mb.Q * am.T) + (am.P * mb.T);
}
result.P = Pab;
result.Q = Qab;
result.T = Tab;
//std::string pabStr = result.P.get_str();
//std::string qabStr = result.Q.get_str();
//std::string tabStr = result.T.get_str();
return result;
}
bsReturn ChudnovskyPiBS::bs_multithreaded_barrier(mpz_class a, mpz_class b, int threadCount, int depth) //clearly thread safe because nothing from outside the function is written to.
{
if (threadCount == 0)
{
ALL_THREADS_SPAWNED.arrive_and_wait();
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
}
depth++;
bsReturn result;
mpz_class Pab;
mpz_class Qab;
mpz_class Tab;
mpz_class m;
if (b - a == 1)
{
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
}
else
{
// Recursively compute P(a,b), Q(a,b) and T(a,b)
// m is the midpoint of and b
mpz_class aplusb = a + b;
mpz_div_ui(m.get_mpz_t(), aplusb.get_mpz_t(), 2);//equivalent pseudocode: m=floor((a+b)/2)
// Recursively calculate P(a, m), Q(a, m) and T(a, m)
bsReturn am; am.Q = NULL;
std::future<bsReturn> futAm;
if (threadCount > 0)
{
int subThreadCountLeft = (--threadCount - 1) / 2; //(int)powl(2, depth); //About the following comment, I might have been wrong. Can't blame me, I'm sleep deprived. //you divide threadCount by powl(2,depth) because 2^depth tells you the number of parallel threads of the binary recursion (or the nodes of a binary tree) at this depth and so you can calculate how to divide number of threads to spawn.
futAm = std::async(std::launch::async, &ChudnovskyPiBS::bs_multithreaded_barrier, this, a, m, subThreadCountLeft, depth);
}
else
{
//std::ostringstream stream;
//stream << "Depth "<<depth<<":Main worker thread (Thread ID: " << GetThreadId(GetCurrentThread()) << ") ready for duty! (Working on Left now)" << std::endl;
//std::cout << stream.str();
am = bs(a, m);
}
// Recursively calculate P(m, b), Q(m, b) and T(m, b)
bsReturn mb; mb.Q = NULL;
std::future<bsReturn> futMb;
if (threadCount > 0)
{
int subThreadCountRight = (--threadCount) / 2;//(int)powl(2, depth); //About the following comment, I might have been wrong. Can't blame me, I'm sleep deprived. //you divide threadCount by powl(2,depth) because 2^depth tells you the number of parallel threads of the binary recursion (or the nodes of a binary tree) at this depth and so you can calculate how to divide number of threads to spawn.
futMb = std::async(std::launch::async, &ChudnovskyPiBS::bs_multithreaded_barrier, this, m, b, subThreadCountRight, depth);
}
else
{
//std::ostringstream stream;
//stream << "Depth " << depth << ":Main worker thread (Thread ID: " << GetThreadId(GetCurrentThread()) << ") ready for duty! (Working on Right now.)" << std::endl;
//std::cout << stream.str();
mb = bs(m, b);
}
//Wait for threads:
threadCount -= 2;
if (am.Q == NULL)
am = futAm.get();
if (mb.Q == NULL)
mb = futMb.get();
// Now combine
Pab = am.P * mb.P;
Qab = am.Q * mb.Q;
Tab = (mb.Q * am.T) + (am.P * mb.T);
}
result.P = Pab;
result.Q = Qab;
result.T = Tab;
//std::string pabStr = result.P.get_str();
//std::string qabStr = result.Q.get_str();
//std::string tabStr = result.T.get_str();
return result;
}
int ChudnovskyPiBS::getTotalNumThreadsFromUsefulNumThreads(int _usefulThreadCountWanted)
{
int usefulThreadCountWanted = _usefulThreadCountWanted;
if (usefulThreadCountWanted == 2)
{
return 2;
}
else if (floor(log2(usefulThreadCountWanted)) != log2(usefulThreadCountWanted))
{
usefulThreadCountWanted = pow(2, floor(log2(usefulThreadCountWanted)));
}
if (usefulThreadCountWanted <= 1)
{
std::cout << "Invalid thread count. Needs to be some power of 2 and greater than 1." << std::endl;
throw _EXCEPTION_;
}
//usefulThreadCount is basically the same as the maximum leaves of a binary tree at some level, meanwhile total includes useless (those blocking/waititng for the computation threads).
int depthOfBinaryTree = log2(usefulThreadCountWanted);//num leaves = 2^depth
int totalThreadCountNeeded = (int)pow(2, 1 + depthOfBinaryTree) - 2; //total nodes = 2^(k+1) -1, if without root, -1 becomes -2
return totalThreadCountNeeded;
}
mpz_class ChudnovskyPiBS::calculatePi()
{
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
//std::cout << NUM_THREADS_IN_CPU << std::endl;
//std::cout << TOTAL_THREAD_COUNT << std::endl;
bsReturn BSResult = bs_multithreaded(0, N, TOTAL_THREAD_COUNT); //bs_multithreaded(0, N, TOTAL_THREAD_COUNT); //bs_multithreaded_barrier(0, N, TOTAL_THREAD_COUNT, 0); //bs(0, N); //apparently Q and T gotten are wrong.
//BSResult.Q.get_str();
//BSResult.T.get_str();
//return mpz_fdiv_q((Q * 426880 * sqrtC) / T
mpz_class result = (BSResult.Q * 426880 * futSqrtC.get());
mpz_fdiv_q(result.get_mpz_t(), result.get_mpz_t(), BSResult.T.get_mpz_t());
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
return result;
}
mpz_class ChudnovskyPiBS::getSqrtC(unsigned long digits)
{
mpz_class one_squared;
mpz_ui_pow_ui(one_squared.get_mpz_t(), 10, 2 * digits);
//one_squared.get_str();
mpz_class thousandandfive__times__one_squared = 10005 * one_squared;
mpz_class sqrtC;
mpz_sqrt(sqrtC.get_mpz_t(), thousandandfive__times__one_squared.get_mpz_t());
//sqrtC.get_str();
return sqrtC;
}
/* DEPRECATED/OLD MULTITHREADING SOLUTION
mpz_class ChudnovskyPiBS::calculatePi()
{
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
//for (int i = 1; i < 5; i++)
//{
// std::cout << (int)pow(2, i) <<" - " << getTotalNumThreadsFromUsefulNumThreads((int)pow(2,i)) << std::endl;
//}
int numberOfThreadsInCPU = std::thread::hardware_concurrency();
int totalThreadCount = getTotalNumThreadsFromUsefulNumThreads(numberOfThreadsInCPU);
bsReturn BSResult = bs_multithreaded(0, N, totalThreadCount, 0);//bs(0, N);
//bsReturn BSResult = bs(0, N); //apparently Q and T gotten are wrong.
//BSResult.Q.get_str();
//BSResult.T.get_str();
//return mpz_fdiv_q((Q * 426880 * sqrtC) / T
mpz_class result = (BSResult.Q * 426880 * sqrtC);
mpz_fdiv_q(result.get_mpz_t(), result.get_mpz_t(), BSResult.T.get_mpz_t());
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
return result;
}
*/