Skip to content

Commit 12b5407

Browse files
committed
Refactoring.
1 parent 146bcc3 commit 12b5407

File tree

2 files changed

+44
-80
lines changed

2 files changed

+44
-80
lines changed

ChudnovskyPiBS.cpp

Lines changed: 42 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include <Windows.h> //this should be the last import somehow?
88

9-
//Global variables:
9+
//Global variables for configuration:
1010
const int NUM_THREADS_IN_CPU = std::thread::hardware_concurrency();
11-
const int NUM_MAIN_WORKER_THREADS = NUM_THREADS_IN_CPU; // 2 * NUM_THREADS_IN_CPU;
11+
const int NUM_MAIN_WORKER_THREADS = NUM_THREADS_IN_CPU;
1212
const int TOTAL_THREAD_COUNT = ChudnovskyPiBS::getTotalNumThreadsFromUsefulNumThreads(NUM_MAIN_WORKER_THREADS);
1313
std::barrier ALL_THREADS_SPAWNED(NUM_MAIN_WORKER_THREADS);
1414

@@ -46,39 +46,11 @@ bsReturn ChudnovskyPiBS::bs(mpz_class a, mpz_class b) //clearly thread safe beca
4646
mpz_class m;
4747
if (b - a == 1)
4848
{
49-
//Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
50-
if (a == 0)
51-
Pab = Qab = 1;
52-
else
53-
{
54-
Pab = (6 * a - 5) * (2 * a - 1) * (6 * a - 1);
55-
Qab = a * a * a * intBigC3_OVER_24;
56-
}
57-
58-
Tab = Pab * (13591409 + 545140134 * a); // a(a) * p(a)
59-
//Pab.get_str();
60-
//Tab.get_str();
61-
//a.get_str();
62-
//Qab.get_str();
63-
mpz_class toCheck;
64-
mpz_and(toCheck.get_mpz_t(), a.get_mpz_t(), one);
65-
if (toCheck == 1) //note to self: works as expected
66-
Tab = -Tab;
49+
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
6750
}
6851
else
6952
{
70-
// Recursively compute P(a,b), Q(a,b) and T(a,b)
71-
// m is the midpoint of and b
72-
mpz_class aplusb = a + b;
73-
mpz_div_ui(m.get_mpz_t(), aplusb.get_mpz_t(), 2);//equivalent pseudocode: m=floor((a+b)/2)
74-
// Recursively calculate P(a, m), Q(a, m) and T(a, m)
75-
bsReturn am = bs(a, m);
76-
// Recursively calculate P(m, b), Q(m, b) and T(m, b)
77-
bsReturn mb = bs(m, b);
78-
// Now combine
79-
Pab = am.P * mb.P;
80-
Qab = am.Q * mb.Q;
81-
Tab = (mb.Q * am.T) + (am.P * mb.T);
53+
recursivelyComputePabQabTab_SingleThreaded(a, b, m, Pab, Qab, Tab);
8254
}
8355
result.P = Pab;
8456
result.Q = Qab;
@@ -88,6 +60,42 @@ bsReturn ChudnovskyPiBS::bs(mpz_class a, mpz_class b) //clearly thread safe beca
8860
//std::string tabStr = result.T.get_str();
8961
return result;
9062
}
63+
void ChudnovskyPiBS::directlyCompute__P_Q_T__from_A_to_AplusOne(mpz_class& a, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab)
64+
{
65+
//Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
66+
if (a == 0)
67+
Pab = Qab = 1;
68+
else
69+
{
70+
Pab = (6 * a - 5) * (2 * a - 1) * (6 * a - 1);
71+
Qab = a * a * a * intBigC3_OVER_24;
72+
}
73+
74+
Tab = Pab * (13591409 + 545140134 * a); // a(a) * p(a)
75+
//Pab.get_str();
76+
//Tab.get_str();
77+
//a.get_str();
78+
//Qab.get_str();
79+
mpz_class toCheck;
80+
mpz_and(toCheck.get_mpz_t(), a.get_mpz_t(), one);
81+
if (toCheck == 1) //note to self: works as expected
82+
Tab = -Tab;
83+
}
84+
void ChudnovskyPiBS::recursivelyComputePabQabTab_SingleThreaded(mpz_class& a, mpz_class& b, mpz_class& m, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab)
85+
{
86+
// Recursively compute P(a,b), Q(a,b) and T(a,b)
87+
// m is the midpoint of and b
88+
mpz_class aplusb = a + b;
89+
mpz_div_ui(m.get_mpz_t(), aplusb.get_mpz_t(), 2);//equivalent pseudocode: m=floor((a+b)/2)
90+
// Recursively calculate P(a, m), Q(a, m) and T(a, m)
91+
bsReturn am = bs(a, m);
92+
// Recursively calculate P(m, b), Q(m, b) and T(m, b)
93+
bsReturn mb = bs(m, b);
94+
// Now combine
95+
Pab = am.P * mb.P;
96+
Qab = am.Q * mb.Q;
97+
Tab = (mb.Q * am.T) + (am.P * mb.T);
98+
}
9199

92100
bsReturn ChudnovskyPiBS::bs_multithreaded(mpz_class a, mpz_class b, int threadCount) //clearly thread safe because nothing from outside the function is written to.
93101
{
@@ -98,24 +106,7 @@ bsReturn ChudnovskyPiBS::bs_multithreaded(mpz_class a, mpz_class b, int threadCo
98106
mpz_class m;
99107
if (b - a == 1)
100108
{
101-
//Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
102-
if (a == 0)
103-
Pab = Qab = 1;
104-
else
105-
{
106-
Pab = (6 * a - 5) * (2 * a - 1) * (6 * a - 1);
107-
Qab = a * a * a * intBigC3_OVER_24;
108-
}
109-
110-
Tab = Pab * (13591409 + 545140134 * a); // a(a) * p(a)
111-
//Pab.get_str();
112-
//Tab.get_str();
113-
//a.get_str();
114-
//Qab.get_str();
115-
mpz_class toCheck;
116-
mpz_and(toCheck.get_mpz_t(), a.get_mpz_t(), one);
117-
if (toCheck == 1) //note to self: works as expected
118-
Tab = -Tab;
109+
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
119110
}
120111
else
121112
{
@@ -168,7 +159,6 @@ bsReturn ChudnovskyPiBS::bs_multithreaded(mpz_class a, mpz_class b, int threadCo
168159
return result;
169160
}
170161

171-
172162
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.
173163
{
174164
if (threadCount == 0)
@@ -184,24 +174,7 @@ bsReturn ChudnovskyPiBS::bs_multithreaded_barrier(mpz_class a, mpz_class b, int
184174
mpz_class m;
185175
if (b - a == 1)
186176
{
187-
//Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
188-
if (a == 0)
189-
Pab = Qab = 1;
190-
else
191-
{
192-
Pab = (6 * a - 5) * (2 * a - 1) * (6 * a - 1);
193-
Qab = a * a * a * intBigC3_OVER_24;
194-
}
195-
196-
Tab = Pab * (13591409 + 545140134 * a); // a(a) * p(a)
197-
//Pab.get_str();
198-
//Tab.get_str();
199-
//a.get_str();
200-
//Qab.get_str();
201-
mpz_class toCheck;
202-
mpz_and(toCheck.get_mpz_t(), a.get_mpz_t(), one);
203-
if (toCheck == 1) //note to self: works as expected
204-
Tab = -Tab;
177+
directlyCompute__P_Q_T__from_A_to_AplusOne(a, Pab, Qab, Tab);
205178
}
206179
else
207180
{
@@ -275,12 +248,6 @@ int ChudnovskyPiBS::getTotalNumThreadsFromUsefulNumThreads(int usefulThreadCount
275248
return totalThreadCountNeeded;
276249
}
277250

278-
//int ChudnovskyPiBS::
279-
//{
280-
//
281-
//}
282-
283-
284251
mpz_class ChudnovskyPiBS::calculatePi()
285252
{
286253
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
@@ -300,11 +267,6 @@ mpz_class ChudnovskyPiBS::calculatePi()
300267

301268

302269
/* DEPRECATED/OLD MULTITHREADING SOLUTION
303-
* //The following definition made 16 main-worker-threads instead of 8 when asked to make 8.
304-
305-
306-
307-
308270
mpz_class ChudnovskyPiBS::calculatePi()
309271
{
310272
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);

ChudnovskyPiBS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ChudnovskyPiBS //Calculates Pi using Chudnovsky algorithm with Binary Spli
4444
/// <param name="b"></param>
4545
/// <returns></returns>
4646
bsReturn bs(mpz_class a, mpz_class b);
47+
void recursivelyComputePabQabTab_SingleThreaded(mpz_class& a, mpz_class& b, mpz_class& m, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab);
48+
void directlyCompute__P_Q_T__from_A_to_AplusOne(mpz_class& a, mpz_class& Pab, mpz_class& Qab, mpz_class& Tab);
4749
bsReturn bs_multithreaded(mpz_class a, mpz_class b, int threadCount);
4850
bsReturn bs_multithreaded_barrier(mpz_class a, mpz_class b, int threadCount, int depth); //uses a barrier to wait for all main worker threads to spawn.
4951

0 commit comments

Comments
 (0)