C++ Library Extensions 2022.12.09
To help learn modern C++ programming
001-parallel_sort_msvc.cpp
Go to the documentation of this file.
2#include <tpf_output.hpp>
3#include <execution> // as of today, April 28, 2019
4 // only MSVC version 12.7 implements C++2a Parallel Algorithm
5 // possibly latest version of INTEL C++ compiler too
6 // GNU g++ version 9.0 /clang++ version 9.0 will be
7 // released very soon, then we can try this program too.
8
10auto nl = "\n";
11auto nL = "\n\n";
13
15{
16 // STEP 1. define namespace alias
17 // and some types we will be using
18
19 namespace crd = tpf::chrono_random;
20
21 using element_t = int;
22 using container_t = std::vector<element_t>;
23
24 // STEP 2. create a random number generator
25 // we create a random number generator
26 // that generates type element_t (or double)
27 // range of generated doubles are from 1 to 100
28 auto generator = crd::random_generator<element_t>(1, 20); // from 1 to 20 inclusive
29
30 // STEP 3. create an object of type container_t, std::vector<double>
31 container_t v;
32
33 // STEP 4. fill the container with random numbers
34 // using the random number generator created at STEP 2.
35 // we generate 20 elements of type element_t, or double
36 crd::random_fill(v, generator, 10); // 10 elements of type element_t
37
38 stream <<"Random numbers : " << v << nl;
39
40 // if you don't end stream with flush here
41 // you cannot see your result. The reason is for optimization
42 stream << v.size()
43 << " random numbers are generated" << flush;
44}
45
46template<typename ContainerType, typename StopwatchType>
47void print_constainer(size_t index,
48 const ContainerType& container,
49 const StopwatchType& sw)
50 {
51 stream << index <<"- Minimum: " << container.front()
52 << ", Maximum: " << container.back()
53 << ", Elapsed: " << sw.elapsed_time() << nl;
54 }
55
57{
58 // STEP 1. define namespace alias
59 // and some types we will be using
60
61 namespace crd = tpf::chrono_random;
62
63 using element_t = double;
64 using container_t = std::vector<element_t>;
65
66 // STEP 2. create a random number generator
67 // we create a random number generator
68 // that generates type element_t (or double)
69 // range of generated doubles are from 1 to 100
70 auto generator = crd::random_generator<element_t>(1, 100); // from 1 to 100 inclusive
71
72 // STEP 3. create an object of type container_t, std::vector<double>
73 container_t v;
74
75 size_t size = 1'000'000;
76 size_t test_count = 5;
77
78 // STEP 4. fill the container with random numbers
79 // using the random number generator created at STEP 2.
80 // we generate 20 elements of type element_t, or double
81 crd::random_fill(v, generator, size); // 10 elements of type element_t
82
84
85 stream << "========== Serial Sort ==========" << nL;
86 for(size_t i = 0; i < test_count; ++i)
87 {
88 auto sorted_container = v;
89 sw.reset();
90 std::sort(sorted_container.begin(), sorted_container.end());
91 print_constainer(i, sorted_container, sw);
92 }
93
94 // if you don't flush, you cannot see anything
95 stream << flush;
96}
97
99{
100 // STEP 1. define namespace alias
101 // and some types we will be using
102
103 namespace crd = tpf::chrono_random;
104
105 using element_t = double;
106 using container_t = std::vector<element_t>;
107
108 // STEP 2. create a random number generator
109 // we create a random number generator
110 // that generates type element_t (or double)
111 // range of generated doubles are from 1 to 100
112 auto generator = crd::random_generator<element_t>(1, 100); // from 1 to 100 inclusive
113
114 // STEP 3. create an object of type container_t, std::vector<double>
115 container_t v;
116
117 size_t size = 1'000'000;
118 size_t test_count = 5;
119
120 // STEP 4. fill the container with random numbers
121 // using the random number generator created at STEP 2.
122 // we generate 20 elements of type element_t, or double
123 crd::random_fill(v, generator, size); // 10 elements of type element_t
124
126
127 stream << "========== Parallel Sort ==========" << nL;
128 for(size_t i = 0; i < test_count; ++i)
129 {
130 auto sorted_container = v;
131 sw.reset();
132 std::sort(std::execution::par_unseq, sorted_container.begin(), sorted_container.end());
133 print_constainer(i, sorted_container, sw);
134 }
135
136 // if you don't flush, you cannot see anything
137 stream << flush;
138}
139
140int main()
141{
142 // examples_for_random_stopwatch();
145}
void examples_for_random_stopwatch()
tpf::sstream stream
void examples_for_parallel_sort()
void print_constainer(size_t index, const ContainerType &container, const StopwatchType &sw)
void examples_for_serial_sort()
tpf::chrono_random::stop_watch stop_watch
void random_fill(Type(&container)[N], RandomGeneratorType const &random_generator)
Implements random number generator and stop watch.
constexpr new_line nl()
Definition: tpf_output.hpp:837
ContainerType< EleType > sort(ContainerType< EleType, Types... > container, sort_order order=sort_order::ascending, sort_method method=sort_method::size)
Definition: tpf_set.hpp:438
Stream output operators << are implemented.