| 1 | // spreadsort double sorting example. |
| 2 | // |
| 3 | // Copyright Steven Ross 2009-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 <time.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <algorithm> |
| 16 | #include <vector> |
| 17 | #include <string> |
| 18 | #include <fstream> |
| 19 | #include <iostream> |
| 20 | using namespace boost::sort::spreadsort; |
| 21 | |
| 22 | #define DATA_TYPE double |
| 23 | #define CAST_TYPE boost::int64_t |
| 24 | |
| 25 | //Pass in an argument to test std::sort |
| 26 | //Note that this converts NaNs and -0.0 to 0.0, so that sorting results are |
| 27 | //identical every time |
| 28 | int main(int argc, const char ** argv) { |
| 29 | size_t uCount,uSize=sizeof(DATA_TYPE); |
| 30 | bool stdSort = false; |
| 31 | unsigned loopCount = 1; |
| 32 | for (int u = 1; u < argc; ++u) { |
| 33 | if (std::string(argv[u]) == "-std" ) |
| 34 | stdSort = true; |
| 35 | else |
| 36 | loopCount = atoi(nptr: argv[u]); |
| 37 | } |
| 38 | std::ifstream input("input.txt" , std::ios_base::in | std::ios_base::binary); |
| 39 | if (input.fail()) { |
| 40 | printf(format: "input.txt could not be opened\n" ); |
| 41 | return 1; |
| 42 | } |
| 43 | double total = 0.0; |
| 44 | std::vector<DATA_TYPE> array; |
| 45 | input.seekg (0, std::ios_base::end); |
| 46 | size_t length = input.tellg(); |
| 47 | uCount = length/uSize; |
| 48 | //Using this to support compilers that don't support 64-bit constants |
| 49 | CAST_TYPE exponent_mask = 0x7ff00000; |
| 50 | exponent_mask <<= 32; |
| 51 | CAST_TYPE top_exponent_bit = 0x40000000; |
| 52 | top_exponent_bit <<= 32; |
| 53 | //Run multiple loops, if requested |
| 54 | for (unsigned u = 0; u < loopCount; ++u) { |
| 55 | input.seekg (0, std::ios_base::beg); |
| 56 | //Conversion to a vector |
| 57 | array.resize(new_size: uCount); |
| 58 | unsigned v = 0; |
| 59 | while (input.good() && v < uCount) { |
| 60 | input.read(s: reinterpret_cast<char *>(&(array[v])), n: uSize ); |
| 61 | //Checking for denormalized numbers |
| 62 | if (!(float_mem_cast<DATA_TYPE, CAST_TYPE>(data: array[v]) & exponent_mask)) { |
| 63 | //Make the top exponent bit high |
| 64 | CAST_TYPE temp = top_exponent_bit | |
| 65 | float_mem_cast<DATA_TYPE, CAST_TYPE>(data: array[v]); |
| 66 | memcpy(dest: &(array[v]), src: &temp, n: sizeof(DATA_TYPE)); |
| 67 | } |
| 68 | //Testcase doesn't sort NaNs; they just cause confusion |
| 69 | if (!(array[v] < 0.0) && !(0.0 < array[v])) |
| 70 | array[v] = 0.0; |
| 71 | ++v; |
| 72 | } |
| 73 | clock_t start, end; |
| 74 | double elapsed; |
| 75 | start = clock(); |
| 76 | if (stdSort) |
| 77 | //std::sort(&(array[0]), &(array[0]) + uCount); |
| 78 | std::sort(first: array.begin(), last: array.end()); |
| 79 | else |
| 80 | //float_sort(&(array[0]), &(array[0]) + uCount); |
| 81 | float_sort(first: array.begin(), last: array.end()); |
| 82 | end = clock(); |
| 83 | elapsed = static_cast<double>(end - start) ; |
| 84 | std::ofstream ofile; |
| 85 | if (stdSort) |
| 86 | ofile.open(s: "standard_sort_out.txt" , mode: std::ios_base::out | |
| 87 | std::ios_base::binary | std::ios_base::trunc); |
| 88 | else |
| 89 | ofile.open(s: "boost_sort_out.txt" , mode: std::ios_base::out | |
| 90 | std::ios_base::binary | std::ios_base::trunc); |
| 91 | if (ofile.good()) { |
| 92 | for (unsigned v = 0; v < array.size(); ++v) { |
| 93 | ofile.write(s: reinterpret_cast<char *>(&(array[v])), n: sizeof(array[v]) ); |
| 94 | } |
| 95 | ofile.close(); |
| 96 | } |
| 97 | total += elapsed; |
| 98 | array.clear(); |
| 99 | } |
| 100 | if (stdSort) |
| 101 | printf(format: "std::sort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 102 | else |
| 103 | printf(format: "spreadsort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 104 | return 0; |
| 105 | } |
| 106 | |