| 1 | // Benchmark for integer sorting speed across parallel threads. |
| 2 | // |
| 3 | // Copyright Steven Ross 2014 |
| 4 | // |
| 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) |
| 8 | |
| 9 | // See http://www.boost.org/libs/sort for library home page. |
| 10 | |
| 11 | #include <boost/sort/spreadsort/spreadsort.hpp> |
| 12 | #include <boost/thread.hpp> |
| 13 | #include <time.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <algorithm> |
| 17 | #include <vector> |
| 18 | #include <string> |
| 19 | #include <fstream> |
| 20 | #include <sstream> |
| 21 | #include <iostream> |
| 22 | using namespace boost::sort::spreadsort; |
| 23 | |
| 24 | #define DATA_TYPE int |
| 25 | |
| 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]) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | void sort_loop(const std::vector<DATA_TYPE> &base_array, bool stdSort, |
| 36 | unsigned loopCount) { |
| 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]; |
| 41 | } |
| 42 | if (stdSort) |
| 43 | std::sort(first: array.begin(), last: array.end()); |
| 44 | else |
| 45 | boost::sort::spreadsort::spreadsort(first: array.begin(), last: array.end()); |
| 46 | if (!is_sorted(array)) { |
| 47 | fprintf(stderr, format: "sort failed!\n" ); |
| 48 | exit(status: 1); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 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); |
| 56 | bool stdSort = false; |
| 57 | int threadCount = -1; |
| 58 | unsigned loopCount = 0; |
| 59 | for (int u = 1; u < argc; ++u) { |
| 60 | if (std::string(argv[u]) == "-std" ) |
| 61 | stdSort = true; |
| 62 | else if(threadCount < 0) |
| 63 | threadCount = atoi(nptr: argv[u]); |
| 64 | else |
| 65 | loopCount = atoi(nptr: argv[u]); |
| 66 | } |
| 67 | if (!loopCount) { |
| 68 | loopCount = 1; |
| 69 | } |
| 70 | printf(format: "threads: %d loops: %d\n" , threadCount, loopCount); |
| 71 | |
| 72 | std::ifstream input("input.txt" , std::ios_base::in | std::ios_base::binary); |
| 73 | if (input.fail()) { |
| 74 | printf(format: "input.txt could not be opened\n" ); |
| 75 | return 1; |
| 76 | } |
| 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(new_size: uCount); |
| 84 | unsigned v = 0; |
| 85 | while (input.good() && v < uCount) |
| 86 | input.read(s: reinterpret_cast<char *>(&(base_array[v++])), n: uSize ); |
| 87 | input.close(); |
| 88 | if (v < uCount) |
| 89 | base_array.resize(new_size: v); |
| 90 | //Run multiple loops, if requested |
| 91 | clock_t start, end; |
| 92 | double elapsed; |
| 93 | std::vector<boost::thread *> workers; |
| 94 | start = clock(); |
| 95 | if (threadCount == 0) { |
| 96 | sort_loop(base_array, stdSort, loopCount); |
| 97 | } else { |
| 98 | for (int i = 0; i < threadCount; ++i) { |
| 99 | workers.push_back(x: new boost::thread(sort_loop, base_array, stdSort, |
| 100 | loopCount)); |
| 101 | } |
| 102 | for (int i = 0; i < threadCount; ++i) { |
| 103 | workers[i]->join(); |
| 104 | delete workers[i]; |
| 105 | } |
| 106 | } |
| 107 | end = clock(); |
| 108 | elapsed = static_cast<double>(end - start) ; |
| 109 | |
| 110 | if (stdSort) |
| 111 | printf(format: "std::sort clock time %lf\n" , elapsed/CLOCKS_PER_SEC/threadCount); |
| 112 | else |
| 113 | printf(format: "spreadsort clock time %lf\n" , elapsed/CLOCKS_PER_SEC/threadCount); |
| 114 | return 0; |
| 115 | } |
| 116 | |