C++ Library Extensions 2022.12.09
To help learn modern C++ programming
020-random.cpp
Go to the documentation of this file.
2#include <tpf_output.hpp>
3
4// cl /EHsc /std:c++latest /O2 020-random.cpp /Fem.exe
5// icl /EHsc /std:c++latest /O2 020-random.cpp /Fem.exe
6// g++ -std=c++2a -fopenmp -D_GLIBCXX_PARALLEL -O3 020-random.cpp -o g.exe
7// clang++ -std=c++2a -O3 020-random.cpp -o c.exe
8
9#if defined(_MSC_VER) || defined(__ICL)
10 #include <execution> // as of today, April 28, 2019
11 // only MSVC version 12.7 implements C++2a Parallel Algorithm
12 // possibly latest version of INTEL C++ compiler too
13 // GNU g++ version 9.0 /clang++ version 9.0 will be
14 // released very soon, then we can try this program too.
15#else
16 #include <parallel/algorithm>
17#endif
18
20auto nl = "\n";
21auto nL = "\n\n";
23
24template<typename ContainerType, typename StopwatchType>
25void print_container(size_t index,
26 const ContainerType& container,
27 const StopwatchType& sw, bool bShow=true)
28 {
29 stream << index;
30
31 if(bShow)
32 stream <<" : " << container << nl;
33
34 stream << "- Minimum: " << container.front()
35 << ", Maximum: " << container.back()
36 << ", Elapsed: " << sw.elapsed_time() << nL;
37 }
38
40{
41 // STEP 1. define namespace alias
42 // and some types we will be using
43
44 namespace crd = tpf::chrono_random;
45
46 using element_t = double;
47 using container_t = std::vector<element_t>;
48
49 // STEP 2. create a random number generator
50 // we create a random number generator
51 // that generates type element_t (or double)
52 // range of generated doubles are from 1 to 100
53 auto generator = crd::random_generator<element_t>(1, 100); // from 1 to 100 inclusive
54
55 // STEP 3. create an object of type container_t, std::vector<double>
56 container_t v;
57
58 size_t size = 1'000'000;
59 size_t test_count = 5;
60
61 // STEP 4. fill the container with random numbers
62 // using the random number generator created at STEP 2.
63 // we generate 20 elements of type element_t, or double
64 crd::random_fill(v, generator, size); // 10 elements of type element_t
65
67
68 auto generators = generator.clone(test_count);
69
70 stream << "========== Parallel Sort ==========" << nL;
71 for(size_t i = 0; i < test_count; ++i)
72 {
73 container_t sorted_container;
74 crd::random_fill(sorted_container, generators[i], size);
75
76 if(sorted_container.size() < 11)
77 stream <<i<<" : " << sorted_container << nl;
78
79 sw.reset();
80
81 #if defined(_MSC_VER) || defined(__ICL)
82 std::sort(std::execution::par_unseq, sorted_container.begin(), sorted_container.end());
83 #elif defined(_GLIBCXX_PARALLEL)
84 __gnu_parallel::sort(sorted_container.begin(), sorted_container.end());
85 #else
86 std::sort(sorted_container.begin(), sorted_container.end());
87 #endif
88 print_container(i, sorted_container, sw, sorted_container.size() < 11);
89 }
90
91 // if you don't flush, you cannot see anything
92 stream << flush;
93}
94
95int main()
96{
98}
tpf::sstream stream
Definition: 020-random.cpp:19
auto nL
Definition: 020-random.cpp:21
void examples_for_parallel_sort()
Definition: 020-random.cpp:39
auto flush
Definition: 020-random.cpp:22
void print_container(size_t index, const ContainerType &container, const StopwatchType &sw, bool bShow=true)
Definition: 020-random.cpp:25
auto nl
Definition: 020-random.cpp:20
int main()
Definition: 020-random.cpp:95
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.