C++ Library Extensions 2022.12.09
To help learn modern C++ programming
003-parallel_sort_gnu.cpp
Go to the documentation of this file.
2#include <tpf_output.hpp>
3
4#if defined(_GLIBCXX_PARALLEL)
5 #include <parallel/algorithm>
6
7#elif defined (_MSC_VER) || defined (__ICL)
8 // C++2a Standard header file for Parallel Algorithm
9 #include <execution>
10#else
11 #include <algorithm>
12#endif
13
14
15// MSVC : cl /EHsc /std:c++latest /O2 003-parallel_sort_gnu.cpp /Femo.exe
16// Intel C++ : icl /EHsc /std:c++latest /O2 003-parallel_sort_gnu.cpp /Feio.exe
17// GNU g++ : g++ -std=c++2a -fopenmp -D_GLIBCXX_PARALLEL -O3 003-parallel_sort_gnu.cpp -o go.exe
18// clang++ : clang++ -std=c++2a -O3 003-parallel_sort_gnu.cpp -o co.exe
19
20tpf::sstream stream; // string stream, so we have to flush to the console
21 // otherwise, we cannot see the result.
22
23auto nl = "\n";
24auto nL = "\n\n";
26
27/*
28 In future sessions, we will be using random number generators and
29 stop_watch all the time. So, please make yourself familiar with random number
30 generator and stop_watch.
31
32*/
34{
35 #if defined(_MSC_VER) || defined(__ICL)
36 stream <<"Testing either MSVC or Intel C++ compiler"<<nL;
37 #else
38 stream <<"Testing either GNU g++ or clang++"<<nL;
39 #endif
40
41 // STEP 1. define namespace alias and some types for our use
42 namespace crd = tpf::chrono_random;
43 // don't ever do this if you like my code or programming tutorials
44 // using namespace tpf::chrono_random;
45 // never do this, using namespace std;
46
47 // use type alias all the time
48 using element_t = double;
49 using container_t = std::vector<element_t>;
50
51 // sometime later, if you want change your container type
52
53 // STEP 2. we create a random number generator
54
55 // we are creating a random number generator
56 // that generates type element_t (or float)
57 // ranging from 1 to 100 inclusive. By "inclusive,"
58 // I mean that 1 and 100 can also be generated
59 auto generator = crd::random_generator<element_t>(1, 100);
60
61 size_t count = 1'000'000; // we will create count of elements of type element_t
62 size_t test_count = 5; // we will test test_count times
63
65
66 for(size_t i = 0; i < test_count; ++ i)
67 {
68 stream <<i << " - Filling the container with random numbers..."<<nl;
69 container_t v;
70 crd::random_fill(v, generator, count);
71
72 stream <<"Before sort: front - " << v.front() << ", back - " << v.back() << nl;
73
74 // reset stopwatch
75 sw.reset();
76
77 #if defined(_MSC_VER) || defined(__ICL)
78 std::sort(std::execution::par_unseq, v.begin(), v.end());
79 #else
80 std::sort(v.begin(), v.end());
81 #endif
82
83 stream <<"After sort: front - " << v.front() << ", back - " << v.back()
84 << ", Elapsed: " << sw.elapsed_time() << nL;
85 }
86
87 stream << flush;
88}
89
90int main()
91{
93}
tpf::sstream stream
auto flush
void examples_for_parallel_sort_algorithm()
int main()
std::atomic< int > count
Definition: 022-mutex.cpp:10
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.