]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/parallelstring.cpp
1 // Benchmark for integer sorting speed across parallel threads.
3 // Copyright Steven Ross 2014
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/sort for library home page.
11 #include <boost/random/mersenne_twister.hpp>
12 #include <boost/random/uniform_int_distribution.hpp>
13 #include <boost/sort/spreadsort/spreadsort.hpp>
14 #include <boost/thread.hpp>
24 using namespace boost::sort::spreadsort
;
26 #define DATA_TYPE string
28 static bool is_sorted(const std::vector
<DATA_TYPE
> &array
) {
29 for (unsigned u
= 0; u
+ 1 < array
.size(); ++u
) {
30 if (array
[u
] > array
[u
+ 1]) {
37 static void sort_core(std::vector
<DATA_TYPE
> &array
, bool stdSort
,
40 std::sort(array
.begin(), array
.end());
42 boost::sort::spreadsort::spreadsort(array
.begin(), array
.end());
43 if (!is_sorted(array
)) {
44 fprintf(stderr
, "sort failed!\n");
49 static void sort_loop(const std::vector
<DATA_TYPE
> &base_array
, bool stdSort
,
51 std::vector
<DATA_TYPE
> array(base_array
);
52 for (unsigned u
= 0; u
< loopCount
; ++u
) {
53 for (unsigned v
= 0; v
< base_array
.size(); ++v
) {
54 array
[v
] = base_array
[v
];
56 sort_core(array
, stdSort
, loopCount
);
60 //Pass in an argument to test std::sort
61 int main(int argc
, const char ** argv
) {
63 std::ofstream outfile
;
65 int constant_to_random_ratio
= -1;
67 unsigned loopCount
= 0;
68 for (int u
= 1; u
< argc
; ++u
) {
69 if (std::string(argv
[u
]) == "-std")
71 else if(threadCount
< 0)
72 threadCount
= atoi(argv
[u
]);
74 loopCount
= atoi(argv
[u
]);
76 constant_to_random_ratio
= atoi(argv
[u
]);
81 printf("threads: %d loops: %d\n", threadCount
, loopCount
);
83 std::vector
<DATA_TYPE
> base_array
;
84 if (constant_to_random_ratio
>= 0) {
85 //Test for random data with gaps of identical data.
86 random::mt19937 generator
;
87 random::uniform_int_distribution
<int> distribution(0,255);
88 const int constant_to_random_count
= 1000000;
89 const int string_length
= 1000;
90 for (int i
= 0; i
< constant_to_random_count
; ++i
) {
91 DATA_TYPE
temp(string_length
, 'x'); // fill with default character.
92 for (int j
= constant_to_random_ratio
; j
< string_length
;) {
93 int val
= distribution(generator
);
95 j
+= (val
* constant_to_random_ratio
)/128 + 1;
97 base_array
.push_back(temp
);
100 indata
.open("input.txt", std::ios_base::in
| std::ios_base::binary
);
102 printf("input.txt could not be opened\n");
106 while (!indata
.eof() ) {
108 base_array
.push_back(inval
);
115 std::vector
<boost::thread
*> workers
;
117 if (threadCount
== 0) {
119 sort_loop(base_array
, stdSort
, loopCount
);
121 sort_core(base_array
, stdSort
, loopCount
);
125 for (int i
= 0; i
< threadCount
; ++i
) {
126 workers
.push_back(new boost::thread(sort_loop
, base_array
, stdSort
,
129 for (int i
= 0; i
< threadCount
; ++i
) {
135 elapsed
= static_cast<double>(end
- start
) ;
137 printf("for %lu strings\n", base_array
.size());
139 printf("std::sort clock time %lf\n", elapsed
/CLOCKS_PER_SEC
/threadCount
);
141 printf("spreadsort clock time %lf\n", elapsed
/CLOCKS_PER_SEC
/threadCount
);