-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathparallel_sort.cpp
More file actions
83 lines (66 loc) · 2.1 KB
/
parallel_sort.cpp
File metadata and controls
83 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// This program demonstrates how to sort a vector of strings
// in parallel using tf::Taskflow::sort and compares it against
// the sequential sort std::sort.
#include <taskflow/taskflow.hpp>
#include <taskflow/algorithm/sort.hpp>
// generate a random string
std::string random_string(size_t len) {
std::string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::default_random_engine eng{std::random_device{}()};
std::uniform_int_distribution<int> dist(1, 100000);
tmp_s.reserve(len);
for (size_t i = 0; i < len; ++i) {
tmp_s += alphanum[dist(eng) % (sizeof(alphanum) - 1)];
}
return tmp_s;
}
// generate a vector of random strings
std::vector<std::string> random_strings() {
std::vector<std::string> strings(1000000);
std::cout << "generating random strings ...\n";
for(auto& str : strings) {
str = random_string(32);
}
return strings;
}
// Function: main
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cerr << "usage: ./parallel_sort s|p" << std::endl;
std::exit(EXIT_FAILURE);
}
// sequential sort
if(argv[1][0] == 's') {
auto strings = random_strings();
std::cout << "std::sort ... ";
auto beg = std::chrono::steady_clock::now();
std::sort(strings.begin(), strings.end());
auto end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end-beg).count()
<< " ms\n";
}
// parallel sort
else if(argv[1][0] == 'p') {
auto strings = random_strings();
std::cout << "Taskflow Parallel Sort ... ";
auto beg = std::chrono::steady_clock::now();
{
tf::Taskflow taskflow;
tf::Executor executor;
taskflow.sort(strings.begin(), strings.end());
executor.run(taskflow).wait();
}
auto end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end-beg).count()
<< " ms\n";
}
else {
std::cerr << "uncognized method character '" << argv[1][0] << "'\n";
std::exit(EXIT_FAILURE);
}
return 0;
}