Commit 1b0b1f672c309f4d54748c0f549b2e3236e9b1ad
1 parent
d4e88d17
Added parallel algorithm example
Showing
7 changed files
with
85 additions
and
31 deletions
examples12/01-future/makefile deleted
100644 → 0
| 1 | -all: async1 async2 async3 promise | |
| 2 | - | |
| 3 | -async1: async1.o | |
| 4 | - g++ -g -Wall -pedantic $^ -o $@ -lpthread | |
| 5 | - | |
| 6 | -async1.o: async1.cpp | |
| 7 | - g++ -g -c -Wall -pedantic $< -o $@ | |
| 8 | - | |
| 9 | -async2: async2.o | |
| 10 | - g++ -g -Wall -pedantic $^ -o $@ -lpthread | |
| 11 | - | |
| 12 | -async2.o: async2.cpp | |
| 13 | - g++ -g -c -Wall -pedantic $< -o $@ | |
| 14 | - | |
| 15 | -async3: async3.o | |
| 16 | - g++ -g -Wall -pedantic $^ -o $@ -lpthread | |
| 17 | - | |
| 18 | -async3.o: async3.cpp | |
| 19 | - g++ -g -c -Wall -pedantic $< -o $@ | |
| 20 | - | |
| 21 | -promise: promise.o | |
| 22 | - g++ -g -Wall -pedantic $^ -o $@ -lpthread | |
| 23 | - | |
| 24 | -promise.o: promise.cpp | |
| 25 | - g++ -g -c -Wall -pedantic $< -o $@ | |
| 26 | - | |
| 27 | - | |
| 28 | -.PHONY: clean | |
| 29 | - | |
| 30 | -clean: | |
| 31 | - -rm async1.o async1 async2.o async2 async3.o async3 promise.o promise | |
| 32 | 0 | \ No newline at end of file |
examples12/01-future/async1.cpp renamed to examples12/01-threading/async1.cpp
examples12/01-future/async2.cpp renamed to examples12/01-threading/async2.cpp
examples12/01-future/async3.cpp renamed to examples12/01-threading/async3.cpp
examples12/01-threading/makefile
0 → 100644
| 1 | +all: async1 async2 async3 promise parallel_algorithm | |
| 2 | + | |
| 3 | +async1: async1.cpp | |
| 4 | + g++ -g -Wall -pedantic $< -o $@ -lpthread | |
| 5 | + | |
| 6 | +async2: async2.cpp | |
| 7 | + g++ -g -Wall -pedantic $< -o $@ -lpthread | |
| 8 | + | |
| 9 | +async3: async3.cpp | |
| 10 | + g++ -g -Wall -pedantic $< -o $@ -lpthread | |
| 11 | + | |
| 12 | +promise: promise.cpp | |
| 13 | + g++ -g -Wall -pedantic $^ -o $@ -lpthread | |
| 14 | + | |
| 15 | +parallel_algorithm: parallel_algorithm.cpp | |
| 16 | + g++ -std=c++17 -g -Wall -pedantic $< -o $@ -ltbb | |
| 17 | + | |
| 18 | + | |
| 19 | +.PHONY: clean | |
| 20 | + | |
| 21 | +clean: | |
| 22 | + -rm async1 async2 async3 promise parallel_algorithm | |
| 0 | 23 | \ No newline at end of file | ... | ... |
examples12/01-threading/parallel_algorithm.cpp
0 → 100644
| 1 | +#include <stddef.h> | |
| 2 | +#include <stdio.h> | |
| 3 | +#include <algorithm> | |
| 4 | +#include <chrono> | |
| 5 | +#include <random> | |
| 6 | +#include <ratio> | |
| 7 | +#include <vector> | |
| 8 | +#include <execution> | |
| 9 | + | |
| 10 | +using std::chrono::duration; | |
| 11 | +using std::chrono::duration_cast; | |
| 12 | +using std::chrono::high_resolution_clock; | |
| 13 | +using std::milli; | |
| 14 | +using std::random_device; | |
| 15 | +using std::sort; | |
| 16 | +using std::vector; | |
| 17 | + | |
| 18 | +const size_t testSize = 1'000'000; | |
| 19 | + | |
| 20 | +const int iterationCount = 5; | |
| 21 | + | |
| 22 | +void print_results(const char *const tag, const vector<double>& sorted, | |
| 23 | + high_resolution_clock::time_point startTime, | |
| 24 | + high_resolution_clock::time_point endTime) { | |
| 25 | + printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(), | |
| 26 | + sorted.back(), | |
| 27 | + duration_cast<duration<double, milli>>(endTime - startTime).count()); | |
| 28 | +} | |
| 29 | + | |
| 30 | +int main() { | |
| 31 | + random_device rd; | |
| 32 | + | |
| 33 | + // generate some random doubles: | |
| 34 | + printf("Testing with %zu doubles...\n", testSize); | |
| 35 | + vector<double> doubles(testSize); | |
| 36 | + for (auto& d : doubles) { | |
| 37 | + d = static_cast<double>(rd()); | |
| 38 | + } | |
| 39 | + | |
| 40 | + // time how long it takes to sort them: | |
| 41 | + for (int i = 0; i < iterationCount; ++i) | |
| 42 | + { | |
| 43 | + vector<double> sorted(doubles); | |
| 44 | + const auto startTime = high_resolution_clock::now(); | |
| 45 | + sort( sorted.begin(), sorted.end()); | |
| 46 | + const auto endTime = high_resolution_clock::now(); | |
| 47 | + print_results("Serial", sorted, startTime, endTime); | |
| 48 | + } | |
| 49 | + | |
| 50 | + | |
| 51 | + for (int i = 0; i < iterationCount; ++i) | |
| 52 | + { | |
| 53 | + vector<double> sorted(doubles); | |
| 54 | + const auto startTime = high_resolution_clock::now(); | |
| 55 | + sort(std::execution::par_unseq, sorted.begin(), sorted.end()); | |
| 56 | + const auto endTime = high_resolution_clock::now(); | |
| 57 | + print_results("Parallel", sorted, startTime, endTime); | |
| 58 | + } | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | +} | |
| 63 | + | ... | ... |
examples12/01-future/promise.cpp renamed to examples12/01-threading/promise.cpp