C++ Library Extensions 2022.12.09
To help learn modern C++ programming
002-parallel_gnu.cpp
Go to the documentation of this file.
2#include <tpf_output.hpp>
3
4// MSVC : cl /EHsc /std:c++latest /O2 002-parallel_gnu.cpp /Femo.exe
5// Intel C++ : icl /EHsc /std:c++latest /O2 002-parallel_gnu.cpp /Feio.exe
6// GNU g++ : g++ -std=c++2a -fopenmp -D_GLIBCXX_PARALLEL -O3 002-parallel_gnu.cpp -o go.exe
7// clang++ : clang++ -std=c++2a -O3 002-parallel_gnu.cpp -o co.exe
8
9tpf::sstream stream; // string stream, so we have to flush to the console
10 // otherwise, we cannot see the result.
11
12auto nl = "\n";
13auto nL = "\n\n";
15
16/*
17 In future sessions, we will be using random number generators and
18 stop_watch all the time. So, please make yourself familiar with random number
19 generator and stop_watch.
20
21*/
23{
24 // STEP 1. define namespace alias and some types for our use
25 namespace crd = tpf::chrono_random;
26 // don't ever do this if you like my code or programming tutorials
27 // using namespace tpf::chrono_random;
28 // never do this, using namespace std;
29
30 // use type alias all the time
31 using element_t = int;
32 using container_t = std::vector<element_t>;
33
34 // sometime later, if you want change your container type
35
36 // STEP 2. we create a random number generator
37
38 // we are creating a random number generator
39 // that generates type element_t (or float)
40 // ranging from 1 to 100 inclusive. By "inclusive,"
41 // I mean that 1 and 100 can also be generated
42 auto generator = crd::random_generator<element_t>(1, 100);
43
44 size_t count = 10; // we will create count of elements of type element_t
45 size_t test_count = 5; // we will test test_count times
46
48
49 for(size_t i = 0; i < test_count; ++ i)
50 {
51 container_t v;
52 sw.reset();
53
54 crd::random_fill(v, generator, count);
55
56 stream <<"Generated randoms: " << v << ", Elapsed: "
57 << sw.elapsed_time<crd::nano_t>() << nl;
58 }
59
60 stream << flush;
61}
62
63int main()
64{
66
67}
void examples_for_random_stop_watch()
tpf::sstream stream
auto nL
auto flush
auto nl
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
Stream output operators << are implemented.