floatsample.cpp 3.1 KB

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