-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (132 loc) · 4.93 KB
/
Copy pathmain.cpp
File metadata and controls
145 lines (132 loc) · 4.93 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
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <chrono>
#include <execution>
#include <thread>
#include <string>
#include <format>
#include <future>
#include <fstream>
void general_info(std::ostream& out)
{
out << std::endl << ':' << std::string(70, '~') << ':' << std::endl;
out << std::format(":{:^70}:", "The author of this program is Yaroslav Kucher K-27") << std::endl;
out << std::format(":{:^70}:", "The program measures the time taken by the std::all_of algorithm.") << std::endl;
out << std::format(":{:^70}:", "It tests different execution policies on vectors of random integers.") << std::endl;
out << std::format(":{:^70}:", "Computer has " + std::to_string(std::thread::hardware_concurrency()) + " cores.") << std::endl;
out << ':' << std::string(70, '~')<< ':' << std::endl << std::endl;
}
void print_policy_time(std::string policy, std::chrono::duration<double, std::milli> duration, std::ostream& out)
{
out << "Policy: " << policy << ", Time taken: " << duration.count() << " ms" << std::endl;
}
bool pred(int x)
{
for (int i = 0; i < 1000; i++)
{
x = (x * x - x + 1)%86;
}
return true;
}
void policy_all_of(const std::vector<int>& vec, std::ostream& out)
{
auto start = std::chrono::high_resolution_clock::now();
std::all_of(vec.begin(), vec.end(), pred);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
print_policy_time("No policy", duration, out);
}
template<typename ExecPolicy>
constexpr const std::string policy_name() {
if constexpr (std::is_same_v<std::decay_t<ExecPolicy>, std::execution::sequenced_policy>)
return "sequential";
else if constexpr (std::is_same_v<std::decay_t<ExecPolicy>, std::execution::parallel_policy>)
return "parallel";
else if constexpr (std::is_same_v<std::decay_t<ExecPolicy>, std::execution::unsequenced_policy>)
return "unsequenced";
else if constexpr (std::is_same_v<std::decay_t<ExecPolicy>, std::execution::parallel_unsequenced_policy>)
return "parallel unsequenced";
}
template<typename ExecPolicy>
void policy_all_of(const std::vector<int>& vec, ExecPolicy&& policy, std::ostream& out)
{
auto start = std::chrono::high_resolution_clock::now();
std::all_of(std::forward<ExecPolicy>(policy), vec.begin(), vec.end(), pred);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
print_policy_time(policy_name<ExecPolicy>(), duration, out);
}
bool parallel_all_of(const std::vector<int>& vec, int k)
{
size_t vecSize = vec.size();
size_t blockSize = vecSize / k;
std::vector<std::future<bool>> futures;
for (int i = 0; i < k; ++i)
{
size_t startIndx = i * blockSize;
size_t endIndx = (i + 1) * blockSize;
if (i == k - 1)
{
endIndx = vecSize;
}
futures.push_back(std::async(std::launch::async, [&vec, startIndx, endIndx]() {
return std::all_of(vec.begin() + startIndx, vec.begin() + endIndx, pred);
}));
}
for (auto& f : futures)
{
if (!f.get()) return false;
}
return true;
}
int main()
{
std::ofstream out("output_Od.txt");
std::mt19937 randomGenerator(std::random_device{}());
std::uniform_int_distribution<int> distribution(0, 100);
std::vector<int> sizes = {100, 1'000, 10'000, 100'000, 1'000'000, 10'000'000};
int n = 1;
general_info(out);
for(auto vecSize: sizes)
{
out << "Experiment #" << n++ << ":" << std::endl
<< "vector size = " << vecSize << std::endl;
std::vector<int> seqOfRandomNumbers(vecSize);
for (auto& el : seqOfRandomNumbers)
{
el = distribution(randomGenerator);
}
policy_all_of(seqOfRandomNumbers, out);
policy_all_of(seqOfRandomNumbers, std::execution::seq, out);
policy_all_of(seqOfRandomNumbers, std::execution::unseq, out);
policy_all_of(seqOfRandomNumbers, std::execution::par, out);
policy_all_of(seqOfRandomNumbers, std::execution::par_unseq, out);
out << std::string(53, '-') << std::endl
<< std::format("|{:^25}|{:^25}|", "K", "Duration, ms") << std::endl;
std::chrono::duration<double, std::milli> best_duration = std::chrono::duration<double, std::milli>::max();
int K = 1;
for (int k = 1; k <= std::thread::hardware_concurrency() * 2; ++k)
{
auto start = std::chrono::high_resolution_clock::now();
parallel_all_of(seqOfRandomNumbers, k);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
if (duration < best_duration)
{
best_duration = duration;
K = k;
}
out << std::string(53, '-') << std::endl
<< std::format("|{:^25}|{:^25}|", k, duration) << std::endl;
}
out << std::string(53, '-') << std::endl;
double ratio = static_cast<double>(K) / std::thread::hardware_concurrency();
out << "The best performance is achieved with "
<< K << " part(s) - this is " << best_duration << std::endl;
out << "K / Threads ratio = " << ratio << std::endl;
out << std::endl << ':' << std::string(70, '~') << ':' << std::endl;
}
return 0;
}