]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/parallelint.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/sort/spreadsort/spreadsort.hpp>
12 #include <boost/thread.hpp>
22 using namespace boost::sort::spreadsort
;
26 bool is_sorted(const std::vector
<DATA_TYPE
> &array
) {
27 for (unsigned u
= 0; u
+ 1 < array
.size(); ++u
) {
28 if (array
[u
] > array
[u
+ 1]) {
35 void sort_loop(const std::vector
<DATA_TYPE
> &base_array
, bool stdSort
,
37 std::vector
<DATA_TYPE
> array(base_array
);
38 for (unsigned u
= 0; u
< loopCount
; ++u
) {
39 for (unsigned v
= 0; v
< base_array
.size(); ++v
) {
40 array
[v
] = base_array
[v
];
43 std::sort(array
.begin(), array
.end());
45 boost::sort::spreadsort::spreadsort(array
.begin(), array
.end());
46 if (!is_sorted(array
)) {
47 fprintf(stderr
, "sort failed!\n");
53 //Pass in an argument to test std::sort
54 int main(int argc
, const char ** argv
) {
55 size_t uCount
,uSize
=sizeof(DATA_TYPE
);
58 unsigned loopCount
= 0;
59 for (int u
= 1; u
< argc
; ++u
) {
60 if (std::string(argv
[u
]) == "-std")
62 else if(threadCount
< 0)
63 threadCount
= atoi(argv
[u
]);
65 loopCount
= atoi(argv
[u
]);
70 printf("threads: %d loops: %d\n", threadCount
, loopCount
);
72 std::ifstream
input("input.txt", std::ios_base::in
| std::ios_base::binary
);
74 printf("input.txt could not be opened\n");
77 std::vector
<DATA_TYPE
> base_array
;
78 input
.seekg (0, std::ios_base::end
);
79 size_t length
= input
.tellg();
80 uCount
= length
/uSize
;
81 input
.seekg (0, std::ios_base::beg
);
82 //Conversion to a vector
83 base_array
.resize(uCount
);
85 while (input
.good() && v
< uCount
)
86 input
.read(reinterpret_cast<char *>(&(base_array
[v
++])), uSize
);
90 //Run multiple loops, if requested
93 std::vector
<boost::thread
*> workers
;
95 if (threadCount
== 0) {
96 sort_loop(base_array
, stdSort
, loopCount
);
98 for (int i
= 0; i
< threadCount
; ++i
) {
99 workers
.push_back(new boost::thread(sort_loop
, base_array
, stdSort
,
102 for (int i
= 0; i
< threadCount
; ++i
) {
108 elapsed
= static_cast<double>(end
- start
) ;
111 printf("std::sort clock time %lf\n", elapsed
/CLOCKS_PER_SEC
/threadCount
);
113 printf("spreadsort clock time %lf\n", elapsed
/CLOCKS_PER_SEC
/threadCount
);