-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (128 loc) · 4.33 KB
/
main.cpp
File metadata and controls
153 lines (128 loc) · 4.33 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
// David Huynh
// COP 4520 - Spring, 2024
#include <iostream>
#include <atomic>
#include <chrono>
#include <fstream>
#include <thread>
#include <vector>
#include <mutex>
#include <shared_mutex>
// Counter class to track information on the Sieve of Eratosthenes.
class Counter
{
private:
std::shared_mutex myMutex;
std::vector<std::atomic<bool>> primes;
int num;
public:
// Constructs a counter with a given range. Sets num to 2 for the sieve.
Counter(int range) : primes(range)
{
num = 2;
}
// Returns the bool at the specified position.
char checkPosition(int position)
{
// Multiple threads/readers can read the counter's value at the same time.
// https://stackoverflow.com/questions/46049803/difference-between-shared-mutex-and-mutex-why-do-both-exist-in-c-17
// https://en.cppreference.com/w/cpp/thread/shared_mutex
std::shared_lock lock(myMutex);
return primes[position];
}
// Grabs from the counter and increments it.
int getAndIncrement()
{
// Only one thread/writer can increment/write the counter's value.
// https://stackoverflow.com/questions/46049803/difference-between-shared-mutex-and-mutex-why-do-both-exist-in-c-17
// https://en.cppreference.com/w/cpp/thread/shared_mutex
std::unique_lock lock(myMutex);
return num++;
}
// Sets a position to true. Each bool is atomic, preventing multiple threads from writing at the same time.
void setTrue(int position)
{
primes[position] = true;
}
// Returns total primes found, sum primes found, and top ten max primes.
std::vector<unsigned long long int> getInformation()
{
int n = primes.size();
std::vector<unsigned long long int> result;
unsigned long long int numOfPrimes = 0;
unsigned long long int sumOfPrimes = 0;
// Calculates the number of all primes found and their sum.
for (int i = 2; i < n; i++)
{
if (primes[i] == 0)
{
numOfPrimes++;
sumOfPrimes += i;
}
}
result.push_back(numOfPrimes);
result.push_back(sumOfPrimes);
// Finds the top 10 max primes.
for (int i = n - 1; i >= 2; i--)
{
if (result.size() < 12 && primes[i] == 0)
{
result.push_back(i);
}
}
return result;
}
};
// Each thread takes a number from the counter, and generates the multiples for it.
void findMultiples(int range, Counter &sharedCounter)
{
int curr = 0;
// Loop through the sieve until i^2 < range.
while (((curr = sharedCounter.getAndIncrement()) * curr) < range)
{
// Check if we should generate primes for this number.
if (sharedCounter.checkPosition(curr) == false)
{
// Set all multiples for this number to true.
for (int i = curr * curr; i < range; i += curr)
{
sharedCounter.setTrue(i);
}
}
}
}
// Main function that starts a timer, spawns 8 threads, and prints output to primes.txt.
int main(void)
{
auto start = std::chrono::high_resolution_clock::now();
// Calculating 10^8 numbers = 100000000.
int range = 100000000;
Counter sharedCounter(range);
std::vector<std::thread> pool(8);
// Spawn 8 threads for the Sieve of Eratosthenes.
for (auto &t : pool)
{
t = std::thread(findMultiples, range, std::ref(sharedCounter));
}
for (auto &t : pool)
{
t.join();
}
// Returns necessary information for the output file.
std::vector<unsigned long long int> res = sharedCounter.getInformation();
// Ending execution timer after threads complete.
auto end = std::chrono::high_resolution_clock::now();
auto executionTime = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
// Outputting results to primes.txt.
std::ofstream output("primes.txt");
output << "Execution Time: " << executionTime.count() << " Seconds, ";
output << "Total Primes Found: " << res[0] << ", ";
output << "Sum of Primes Found: " << res[1] << "\n\n";
output << "Top 10 Max Primes:" << "\n";
for (int i = res.size() - 1; i >= 2; i--)
{
output << res[i] << "\n";
}
output.close();
return 0;
}