-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParallelStdCppExample.cpp
211 lines (191 loc) · 7.59 KB
/
ParallelStdCppExample.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
// From https://blogs.msdn.microsoft.com/vcblog/2018/09/11/using-c17-parallel-algorithms-for-better-performance/
// compile with:
// debug: cl /EHsc /W4 /WX /std:c++latest /Fedebug /MDd .\program.cpp
// release: cl /EHsc /W4 /WX /std:c++latest /Ferelease /MD /O2 .\program.cpp
//#include <oneapi/dpl/execution>
//#include <oneapi/dpl/algorithm>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
#include <execution>
//#include <pstl/execution> // TBB
//#include <pstl/algorithm> // TBB
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::milli;
using std::random_device;
using std::sort;
using std::vector;
const int iterationCount = 5;
void print_results(const char *const tag, const vector<double>& sorted,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime)
{
printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(), sorted.back(),
duration_cast<duration<double, milli>>(endTime - startTime).count());
}
void print_results(const char* const tag, const vector<unsigned long>& sorted,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime)
{
printf("%s: Lowest: %lu Highest: %lu Time: %fms\n", tag, sorted.front(), sorted.back(),
duration_cast<duration<double, milli>>(endTime - startTime).count());
std::cout << tag << ": Lowest: " << sorted.front() << " " << sizeof(sorted.front()) << std::endl;
}
void print_results(const char* const tag, const vector<unsigned>& sorted,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime)
{
printf("%s: Lowest: %u Highest: %u Time: %fms\n", tag, sorted.front(), sorted.back(),
duration_cast<duration<double, milli>>(endTime - startTime).count());
}
void print_results(const char *const tag, double first, double last,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime)
{
printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, first, last,
duration_cast<duration<double, milli>>(endTime - startTime).count());
}
int ParallelStdCppExample(vector<double>& doubles)
{
// time how long it takes to sort them:
for (int i = 0; i < iterationCount; ++i)
{
vector<double> sorted(doubles);
const auto startTime = high_resolution_clock::now();
sort(sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
print_results("Serial", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
vector<double> sorted(doubles);
const auto startTime = high_resolution_clock::now();
// same sort call as above, but with par_unseq:
sort(std::execution::par_unseq, sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
// in our output, note that these are the parallel results:
print_results("Parallel", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
double * s = new double[doubles.size()];
for (size_t j = 0; j < doubles.size(); j++) { // copy the original random array into the source array each time, since ParallelMergeSort modifies the source array while sorting
s[j] = doubles[j];
}
const auto startTime = high_resolution_clock::now();
sort(std::execution::par_unseq, s, s+doubles.size());
const auto endTime = high_resolution_clock::now();
print_results("Parallel Array", s[0], s[doubles.size() - 1], startTime, endTime);
delete[] s;
}
return 0;
}
int ParallelStdCppExample(vector<unsigned>& uints, bool stable = false)
{
// time how long it takes to sort them:
for (int i = 0; i < iterationCount; ++i)
{
vector<unsigned> sorted(uints);
const auto startTime = high_resolution_clock::now();
if (!stable)
sort(sorted.begin(), sorted.end());
else
stable_sort(sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
print_results("Serial", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
vector<unsigned> sorted(uints);
const auto startTime = high_resolution_clock::now();
// same sort call as above, but with par_unseq:
if (!stable)
sort(std::execution::par_unseq, sorted.begin(), sorted.end());
else
stable_sort(std::execution::par_unseq, sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
// in our output, note that these are the parallel results:
print_results("Parallel", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
unsigned* s = new unsigned[uints.size()];
for (size_t j = 0; j < uints.size(); j++) { // copy the original random array into the source array each time, since ParallelMergeSort modifies the source array while sorting
s[j] = uints[j];
}
const auto startTime = high_resolution_clock::now();
if (!stable)
sort(std::execution::par_unseq, s, s + uints.size());
else
std::stable_sort(std::execution::par_unseq, s, s + uints.size());
const auto endTime = high_resolution_clock::now();
print_results("Parallel Array", s[0], s[uints.size() - 1], startTime, endTime);
delete[] s;
}
return 0;
}
int ParallelStdCppExample(vector<unsigned>& uints)
{
// time how long it takes to sort them:
for (int i = 0; i < iterationCount; ++i)
{
vector<unsigned> sorted(uints);
const auto startTime = high_resolution_clock::now();
sort(sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
print_results("Serial", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
vector<unsigned> sorted(uints);
const auto startTime = high_resolution_clock::now();
// same sort call as above, but with par_unseq:
sort(std::execution::par_unseq, sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
// in our output, note that these are the parallel results:
print_results("Parallel", sorted, startTime, endTime);
}
for (int i = 0; i < iterationCount; ++i)
{
unsigned *s = new unsigned[uints.size()];
for (size_t j = 0; j < uints.size(); j++) { // copy the original random array into the source array each time, since ParallelMergeSort modifies the source array while sorting
s[j] = uints[j];
}
const auto startTime = high_resolution_clock::now();
sort(std::execution::par_unseq, s, s + uints.size());
const auto endTime = high_resolution_clock::now();
print_results("Parallel Array", s[0], s[uints.size() - 1], startTime, endTime);
delete[] s;
}
printf("\nAccumulate/Sum Benchmark:\n");
for (int i = 0; i < iterationCount; ++i)
{
unsigned* s = new unsigned[uints.size()];
for (size_t j = 0; j < uints.size(); j++) { // copy the original random array into the source array each time, since ParallelMergeSort modifies the source array while sorting
s[j] = uints[j];
}
const auto startTime = high_resolution_clock::now();
unsigned result_serial = std::accumulate(s, s + uints.size(), 0);
const auto endTime = high_resolution_clock::now();
print_results("Serial Array Sum", result_serial, result_serial, startTime, endTime);
delete[] s;
}
for (int i = 0; i < iterationCount; ++i)
{
unsigned* s = new unsigned[uints.size()];
for (size_t j = 0; j < uints.size(); j++) { // copy the original random array into the source array each time, since ParallelMergeSort modifies the source array while sorting
s[j] = uints[j];
}
const auto startTime = high_resolution_clock::now();
unsigned result_parallel = std::reduce(std::execution::par, s, s + uints.size(), 0); //Faster
const auto endTime = high_resolution_clock::now();
print_results("Parallel Array Sum", result_parallel, result_parallel, startTime, endTime);
delete[] s;
}
return 0;
}