Thread parallelizion in STL
Loading...
Searching...
No Matches
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 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 main.cpp -ltbb
8 ---
9 cppcheck --enable=all --inconclusive --std=c++11 --std=posix --suppress=missingIncludeSystem main.cpp
10 clang++ -O3 -std=c++17 -ltbb main.cpp
11 clang++ -std=c++17 -fsyntax-only -Wdocumentation -Wconversion -Wshadow -Wfloat-conversion -pedantic main.cpp
12 clang++ -std=c++17 -Weverything -Wno-c++98-compat -Wno-padded -ltbb main.cpp
13 clang++ -cc1 --help
14 ---
15 icpc -O3 -std=c++17 -ltbb -Wall -Wextra -pedantic main.cpp
16*/
17
18#include <algorithm>
19#include <chrono>
20#include <execution> // execution policy
21#include <iostream>
22#include <numeric> // accumulate
23#include <random>
24#include <vector>
25using namespace std;
26using namespace std::chrono; // timing
27
28// Great web pages, great book by Filipek
29// https://www.bfilipek.com/2018/06/parstl-tests.html
30template <typename TFunc> void RunAndMeasure(const char *title, TFunc func)
31{
32 const auto start = std::chrono::steady_clock::now();
33 auto ret = func();
34 const auto end = std::chrono::steady_clock::now();
35 std::cout << title << ": " <<
36 std::chrono::duration <double, std::milli>(end - start).count()
37 << " ms, res " << ret << "\n";
38}
39
40int main()
41{
42 //std::vector<double> v(6000000, 0.5);
43 std::vector<double> v(1<<30, 0.5);
44
45 RunAndMeasure("std::warm up", [&v]
46 {
47 return std::reduce(std::execution::seq, v.begin(), v.end(), 0.0);
48 });
49
50 RunAndMeasure("std::accumulate", [&v]
51 {
52 return std::accumulate(v.begin(), v.end(), 0.0);
53 });
54
55 RunAndMeasure("std::reduce, seq", [&v]
56 {
57 return std::reduce(std::execution::seq, v.begin(), v.end(), 0.0);
58 });
59
60 RunAndMeasure("std::reduce, par", [&v]
61 {
62 return std::reduce(std::execution::par, v.begin(), v.end(), 0.0);
63 });
64
65 RunAndMeasure("std::reduce, par_unseq", [&v]
66 {
67 return std::reduce(std::execution::par_unseq, v.begin(), v.end(), 0.0);
68 });
69
70 RunAndMeasure("std::find, seq", [&v]
71 {
72 auto res = std::find(std::execution::seq, std::begin(v), std::end(v), 0.6);
73 return res == std::end(v) ? 0.0 : 1.0;
74 });
75
76 RunAndMeasure("std::find, par", [&v]
77 {
78 auto res = std::find(std::execution::par, std::begin(v), std::end(v), 0.6);
79 return res == std::end(v) ? 0.0 : 1.0;
80 });
81
82 cout << "------------------------------------------------\n";
83 const size_t VecSize=10*20000000;
84 cout << "N = " << VecSize << endl;
85 vector<double> vec(VecSize);
86 iota(begin(vec),end(vec),0.1);
87
88 vector<double> out(VecSize);
89
90 auto heavy_fkt = [](double a){return std::sin(a)*std::cos(a);};
91 auto light_fkt = [](double a){return 1.0/a;};
92 //auto light_fkt = [](double a){return std::sqrt(1.0/a);};
93
94 RunAndMeasure("heavy std::transform seq", [&vec, &out, heavy_fkt]
95 {
96 auto res = std::transform(std::execution::seq, cbegin(vec), cend(vec), begin(out),
97 //[](double a){return std::sin(a)*std::cos(a);}
98 heavy_fkt
99 );
100 return res == std::end(vec) ? 0.0 : 1.0;
101 });
102
103 RunAndMeasure("heavy std::transform par", [&vec, &out, heavy_fkt]
104 {
105 auto res = std::transform(std::execution::par, cbegin(vec), cend(vec), begin(out),
106 heavy_fkt
107 );
108 return res == std::end(vec) ? 0.0 : 1.0;
109 });
110
111
112 RunAndMeasure("light std::transform seq", [&vec, &out, light_fkt]
113 {
114 auto res = std::transform(std::execution::seq, cbegin(vec), cend(vec), begin(out),
115 light_fkt
116 );
117 return res == std::end(vec) ? 0.0 : 1.0;
118 });
119
120 RunAndMeasure("light std::transform par", [&vec, &out, light_fkt]
121 {
122 auto res = std::transform(std::execution::par, cbegin(vec), cend(vec), begin(out),
123 light_fkt
124 );
125 return res == std::end(vec) ? 0.0 : 1.0;
126 });
127
128 return 0;
129}
130
void RunAndMeasure(const char *title, TFunc func)
Definition main.cpp:30
int main()
Definition main.cpp:40