Thread parallelizion in STL
Loading...
Searching...
No Matches
gh_main.cpp
Go to the documentation of this file.
1// C++ Vorlesung xxx
2// C++-17: Threads, execution policies
3// Great web pages, great book by Filipek: https://www.bfilipek.com/2018/06/parstl-tests.html
4
5/*
6 g++ -O3 -std=c++17 gh_main.cpp -ltbb
7 g++ -O3 -std=c++17 -pedantic -Weffc++ -Wall -Wextra -pedantic -Wswitch-default -Wfloat-equal -Wundef -Wredundant-decls -Winit-self -Wshadow -Wparentheses -Wshadow -Wunreachable-code -Wuninitialized -Wmaybe-uninitialized gh_main.cpp -ltbb
8 ---
9 cppcheck --enable=all --inconclusive --std=c++11 --std=posix --suppress=missingIncludeSystem gh_main.cpp
10 clang++ -O3 -std=c++17 -ltbb gh_main.cpp
11 clang++ -std=c++17 -fsyntax-only -Wdocumentation -Wconversion -Wshadow -Wfloat-conversion -pedantic gh_main.cpp
12 clang++ -std=c++17 -Weverything -Wno-c++98-compat -Wno-padded -ltbb gh_main.cpp
13 clang++ -cc1 --help
14 ---
15 icpc -O3 -std=c++17 -ltbb -Wall -Wextra -pedantic gh_main.cpp
16*/
17#include <algorithm>
18#include <chrono>
19#include <execution> // execution policy
20#include <iostream>
21#include <numeric> // accumulate
22#include <random>
23#include <vector>
24using namespace std;
25using namespace std::chrono; // timing
26
27
33template <class T>
34ostream& operator<<(ostream &s, const vector<T>& v)
35{
36 for (const auto& it: v) // Reference is required with unique_ptr. No copy constructor for unique_ptr available!
37 {
38 cout << *it << " ";
39 }
40 return s;
41}
42
43
44int main()
45{
46 cout << "Threads C++17" << endl;
47
48 size_t const N = 1<<25;
49 vector<double> v(N);
50 iota(v.begin(), v.end(), 1);
51 std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
52
53 auto const v_bak(v);
54
55 {
56 v = v_bak;
57 cout << "---- sort old ----" << endl;
58 auto t1 = system_clock::now();
59 sort(v.begin(), v.end());
60 auto t2 = system_clock::now();
61 //auto duration = duration_cast<microseconds>(t2 - t1);
62 auto duration = std::chrono::duration <double, std::micro>(t2 - t1);
63 cout << "sort old : " << duration.count() / 1e6 << " sec." << endl;
64 }
65
66 {
67 v = v_bak;
68 cout << "---- sort seq ----" << endl;
69 auto t1 = system_clock::now();
70 sort(std::execution::seq, v.begin(), v.end());
71 auto t2 = system_clock::now();
72 auto duration = std::chrono::duration <double, std::micro>(t2 - t1);
73 cout << "sort seq : " << duration.count() / 1e6 << " sec." << endl;
74 }
75
76 {
77 v = v_bak;
78 cout << "---- sort par ----" << endl;
79 auto t1 = system_clock::now();
80 sort(std::execution::par, v.begin(), v.end());
81 //auto cnt = count(std::execution::par, v.begin(), v.end(), 17);
82 auto t2 = system_clock::now();
83 auto duration = std::chrono::duration <double, std::micro>(t2 - t1);
84 cout << "sort par : " << duration.count() / 1e6 << " sec." << endl;
85 }
86
87 {
88 v = v_bak;
89 cout << "---- sort par_unseq ----" << endl;
90 auto t1 = system_clock::now();
91 sort(std::execution::par_unseq, v.begin(), v.end());
92 auto t2 = system_clock::now();
93 auto duration = std::chrono::duration <double, std::micro>(t2 - t1);
94 cout << "sort par_unseq: " << duration.count() / 1e6 << " sec." << endl;
95 }
96
97 auto ip = max_element(std::execution::par,v.cbegin(), v.cend());
98 cout << "*ip " << *ip << endl;
99
100
101 return 0;
102}
103
ostream & operator<<(ostream &s, const vector< T > &v)
Prints the whole vector of base class pointers.
Definition gh_main.cpp:34
int main()
Definition gh_main.cpp:44