parallelstring.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/random/mersenne_twister.hpp>
  10. #include <boost/random/uniform_int_distribution.hpp>
  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 <iostream>
  19. #include <fstream>
  20. #include <string>
  21. using std::string;
  22. using namespace boost::sort::spreadsort;
  23. #define DATA_TYPE string
  24. static bool is_sorted(const std::vector<DATA_TYPE> &array) {
  25. for (unsigned u = 0; u + 1 < array.size(); ++u) {
  26. if (array[u] > array[u + 1]) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. static void sort_core(std::vector<DATA_TYPE> &array, bool stdSort,
  33. unsigned loopCount) {
  34. if (stdSort)
  35. std::sort(array.begin(), array.end());
  36. else
  37. boost::sort::spreadsort::spreadsort(array.begin(), array.end());
  38. if (!is_sorted(array)) {
  39. fprintf(stderr, "sort failed!\n");
  40. exit(1);
  41. }
  42. }
  43. static void sort_loop(const std::vector<DATA_TYPE> &base_array, bool stdSort,
  44. unsigned loopCount) {
  45. std::vector<DATA_TYPE> array(base_array);
  46. for (unsigned u = 0; u < loopCount; ++u) {
  47. for (unsigned v = 0; v < base_array.size(); ++v) {
  48. array[v] = base_array[v];
  49. }
  50. sort_core(array, stdSort, loopCount);
  51. }
  52. }
  53. //Pass in an argument to test std::sort
  54. int main(int argc, const char ** argv) {
  55. std::ifstream indata;
  56. std::ofstream outfile;
  57. bool stdSort = false;
  58. int constant_to_random_ratio = -1;
  59. int threadCount = -1;
  60. unsigned loopCount = 0;
  61. for (int u = 1; u < argc; ++u) {
  62. if (std::string(argv[u]) == "-std")
  63. stdSort = true;
  64. else if(threadCount < 0)
  65. threadCount = atoi(argv[u]);
  66. else if (!loopCount)
  67. loopCount = atoi(argv[u]);
  68. else
  69. constant_to_random_ratio = atoi(argv[u]);
  70. }
  71. if (!loopCount) {
  72. loopCount = 1;
  73. }
  74. printf("threads: %d loops: %d\n", threadCount, loopCount);
  75. std::vector<DATA_TYPE> base_array;
  76. if (constant_to_random_ratio >= 0) {
  77. //Test for random data with gaps of identical data.
  78. random::mt19937 generator;
  79. random::uniform_int_distribution<int> distribution(0,255);
  80. const int constant_to_random_count = 1000000;
  81. const int string_length = 1000;
  82. for (int i = 0; i < constant_to_random_count; ++i) {
  83. DATA_TYPE temp(string_length, 'x'); // fill with default character.
  84. for (int j = constant_to_random_ratio; j < string_length;) {
  85. int val = distribution(generator);
  86. temp[j] = val;
  87. j += (val * constant_to_random_ratio)/128 + 1;
  88. }
  89. base_array.push_back(temp);
  90. }
  91. } else {
  92. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  93. if (indata.bad()) {
  94. printf("input.txt could not be opened\n");
  95. return 1;
  96. }
  97. DATA_TYPE inval;
  98. while (!indata.eof() ) {
  99. indata >> inval;
  100. base_array.push_back(inval);
  101. }
  102. }
  103. // Sort the input
  104. clock_t start, end;
  105. double elapsed;
  106. std::vector<boost::thread *> workers;
  107. start = clock();
  108. if (threadCount == 0) {
  109. if (loopCount > 1) {
  110. sort_loop(base_array, stdSort, loopCount);
  111. } else {
  112. sort_core(base_array, stdSort, loopCount);
  113. }
  114. threadCount = 1;
  115. } else {
  116. for (int i = 0; i < threadCount; ++i) {
  117. workers.push_back(new boost::thread(sort_loop, base_array, stdSort,
  118. loopCount));
  119. }
  120. for (int i = 0; i < threadCount; ++i) {
  121. workers[i]->join();
  122. delete workers[i];
  123. }
  124. }
  125. end = clock();
  126. elapsed = static_cast<double>(end - start) ;
  127. printf("for %lu strings\n", base_array.size());
  128. if (stdSort)
  129. printf("std::sort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
  130. else
  131. printf("spreadsort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
  132. return 0;
  133. }