parallelint.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // See http://www.boost.org/libs/sort for library home page.
  9. #include <boost/sort/spreadsort/spreadsort.hpp>
  10. #include <boost/thread.hpp>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <algorithm>
  15. #include <vector>
  16. #include <string>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <iostream>
  20. using namespace boost::sort::spreadsort;
  21. #define DATA_TYPE int
  22. bool is_sorted(const std::vector<DATA_TYPE> &array) {
  23. for (unsigned u = 0; u + 1 < array.size(); ++u) {
  24. if (array[u] > array[u + 1]) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. void sort_loop(const std::vector<DATA_TYPE> &base_array, bool stdSort,
  31. unsigned loopCount) {
  32. std::vector<DATA_TYPE> array(base_array);
  33. for (unsigned u = 0; u < loopCount; ++u) {
  34. for (unsigned v = 0; v < base_array.size(); ++v) {
  35. array[v] = base_array[v];
  36. }
  37. if (stdSort)
  38. std::sort(array.begin(), array.end());
  39. else
  40. boost::sort::spreadsort::spreadsort(array.begin(), array.end());
  41. if (!is_sorted(array)) {
  42. fprintf(stderr, "sort failed!\n");
  43. exit(1);
  44. }
  45. }
  46. }
  47. //Pass in an argument to test std::sort
  48. int main(int argc, const char ** argv) {
  49. size_t uCount,uSize=sizeof(DATA_TYPE);
  50. bool stdSort = false;
  51. int threadCount = -1;
  52. unsigned loopCount = 0;
  53. for (int u = 1; u < argc; ++u) {
  54. if (std::string(argv[u]) == "-std")
  55. stdSort = true;
  56. else if(threadCount < 0)
  57. threadCount = atoi(argv[u]);
  58. else
  59. loopCount = atoi(argv[u]);
  60. }
  61. if (!loopCount) {
  62. loopCount = 1;
  63. }
  64. printf("threads: %d loops: %d\n", threadCount, loopCount);
  65. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  66. if (input.fail()) {
  67. printf("input.txt could not be opened\n");
  68. return 1;
  69. }
  70. std::vector<DATA_TYPE> base_array;
  71. input.seekg (0, std::ios_base::end);
  72. size_t length = input.tellg();
  73. uCount = length/uSize;
  74. input.seekg (0, std::ios_base::beg);
  75. //Conversion to a vector
  76. base_array.resize(uCount);
  77. unsigned v = 0;
  78. while (input.good() && v < uCount)
  79. input.read(reinterpret_cast<char *>(&(base_array[v++])), uSize );
  80. input.close();
  81. if (v < uCount)
  82. base_array.resize(v);
  83. //Run multiple loops, if requested
  84. clock_t start, end;
  85. double elapsed;
  86. std::vector<boost::thread *> workers;
  87. start = clock();
  88. if (threadCount == 0) {
  89. sort_loop(base_array, stdSort, loopCount);
  90. } else {
  91. for (int i = 0; i < threadCount; ++i) {
  92. workers.push_back(new boost::thread(sort_loop, base_array, stdSort,
  93. loopCount));
  94. }
  95. for (int i = 0; i < threadCount; ++i) {
  96. workers[i]->join();
  97. delete workers[i];
  98. }
  99. }
  100. end = clock();
  101. elapsed = static_cast<double>(end - start) ;
  102. if (stdSort)
  103. printf("std::sort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
  104. else
  105. printf("spreadsort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
  106. return 0;
  107. }