]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/sort/example/parallelstring.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / parallelstring.cpp
index fb86604aae98a3b7b39a490bab7f61e0166f742d..f710802bf9026f1bd9e851dcaf5b9fb92450f476 100644 (file)
-//  Benchmark for integer sorting speed across parallel threads.
-//
-//  Copyright Steven Ross 2014
-//
-// Distributed under the Boost Software License, Version 1.0.
-//    (See accompanying file LICENSE_1_0.txt or copy at
-//          http://www.boost.org/LICENSE_1_0.txt)
-
-//  See http://www.boost.org/libs/sort for library home page.
-
-#include <boost/random/mersenne_twister.hpp>
-#include <boost/random/uniform_int_distribution.hpp>
-#include <boost/sort/spreadsort/spreadsort.hpp>
-#include <boost/thread.hpp>
-#include <time.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <algorithm>
-#include <vector>
-#include <iostream>
-#include <fstream>
-#include <string>
-using std::string;
-using namespace boost::sort::spreadsort;
-
-#define DATA_TYPE string
-
-static bool is_sorted(const std::vector<DATA_TYPE> &array) {
-  for (unsigned u = 0; u + 1 < array.size(); ++u) {
-    if (array[u] > array[u + 1]) {
-      return false;
-    }
-  }
-  return true;
-}
-
-static void sort_core(std::vector<DATA_TYPE> &array, bool stdSort,
-               unsigned loopCount) {
-  if (stdSort)
-    std::sort(array.begin(), array.end());
-  else
-    boost::sort::spreadsort::spreadsort(array.begin(), array.end());
-  if (!is_sorted(array)) {
-    fprintf(stderr, "sort failed!\n");
-    exit(1);
-  }
-}
-
-static void sort_loop(const std::vector<DATA_TYPE> &base_array, bool stdSort,
-               unsigned loopCount) {
-  std::vector<DATA_TYPE> array(base_array);
-  for (unsigned u = 0; u < loopCount; ++u) {
-    for (unsigned v = 0; v < base_array.size(); ++v) {
-      array[v] = base_array[v];
-    }
-    sort_core(array, stdSort, loopCount);
-  }
-}
-
-//Pass in an argument to test std::sort
-int main(int argc, const char ** argv) {
-  std::ifstream indata;
-  std::ofstream outfile;
-  bool stdSort = false;
-  int constant_to_random_ratio = -1;
-  int threadCount = -1;
-  unsigned loopCount = 0;
-  for (int u = 1; u < argc; ++u) {
-    if (std::string(argv[u]) == "-std")
-      stdSort = true;
-    else if(threadCount < 0)
-      threadCount = atoi(argv[u]);
-    else if (!loopCount)
-      loopCount = atoi(argv[u]);
-    else
-      constant_to_random_ratio = atoi(argv[u]);
-  }
-  if (!loopCount) {
-    loopCount = 1;
-  }
-  printf("threads: %d loops: %d\n", threadCount, loopCount);
-
-  std::vector<DATA_TYPE> base_array;
-  if (constant_to_random_ratio >= 0) {
-    //Test for random data with gaps of identical data.
-    random::mt19937 generator;
-    random::uniform_int_distribution<int> distribution(0,255);
-    const int constant_to_random_count = 1000000;
-    const int string_length = 1000;
-    for (int i = 0; i < constant_to_random_count; ++i) {
-      DATA_TYPE temp(string_length, 'x');  // fill with default character.
-      for (int j = constant_to_random_ratio; j < string_length;) {
-        int val = distribution(generator);
-        temp[j] = val;
-        j += (val * constant_to_random_ratio)/128 + 1;
-      }
-      base_array.push_back(temp);
-    }
-  } else {
-    indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
-    if (indata.bad()) {
-      printf("input.txt could not be opened\n");
-      return 1;
-    }
-    DATA_TYPE inval;
-    while (!indata.eof() ) {
-      indata >> inval;
-      base_array.push_back(inval);
-    }
-  }
-
-  // Sort the input
-  clock_t start, end;
-  double elapsed;
-  std::vector<boost::thread *> workers;
-  start = clock();
-  if (threadCount == 0) {
-    if (loopCount > 1) {
-      sort_loop(base_array, stdSort, loopCount);
-    } else {
-      sort_core(base_array, stdSort, loopCount);
-    }
-    threadCount = 1;
-  } else {
-    for (int i = 0; i < threadCount; ++i) {
-      workers.push_back(new boost::thread(sort_loop, base_array, stdSort,
-                                          loopCount));
-    }
-    for (int i = 0; i < threadCount; ++i) {
-      workers[i]->join();
-      delete workers[i];
-    }
-  }
-  end = clock();
-  elapsed = static_cast<double>(end - start) ;
-
-  printf("for %lu strings\n", base_array.size());
-  if (stdSort)
-    printf("std::sort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
-  else
-    printf("spreadsort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);
-  return 0;
-}
+//  Benchmark for integer sorting speed across parallel threads.\r
+//\r
+//  Copyright Steven Ross 2014\r
+//\r
+// Distributed under the Boost Software License, Version 1.0.\r
+//    (See accompanying file LICENSE_1_0.txt or copy at\r
+//          http://www.boost.org/LICENSE_1_0.txt)\r
+\r
+//  See http://www.boost.org/libs/sort for library home page.\r
+\r
+#include <boost/random/mersenne_twister.hpp>\r
+#include <boost/random/uniform_int_distribution.hpp>\r
+#include <boost/sort/spreadsort/spreadsort.hpp>\r
+#include <boost/thread.hpp>\r
+#include <time.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <algorithm>\r
+#include <vector>\r
+#include <iostream>\r
+#include <fstream>\r
+#include <string>\r
+using std::string;\r
+using namespace boost::sort::spreadsort;\r
+\r
+#define DATA_TYPE string\r
+\r
+static bool is_sorted(const std::vector<DATA_TYPE> &array) {\r
+  for (unsigned u = 0; u + 1 < array.size(); ++u) {\r
+    if (array[u] > array[u + 1]) {\r
+      return false;\r
+    }\r
+  }\r
+  return true;\r
+}\r
+\r
+static void sort_core(std::vector<DATA_TYPE> &array, bool stdSort,\r
+               unsigned loopCount) {\r
+  if (stdSort)\r
+    std::sort(array.begin(), array.end());\r
+  else\r
+    boost::sort::spreadsort::spreadsort(array.begin(), array.end());\r
+  if (!is_sorted(array)) {\r
+    fprintf(stderr, "sort failed!\n");\r
+    exit(1);\r
+  }\r
+}\r
+\r
+static void sort_loop(const std::vector<DATA_TYPE> &base_array, bool stdSort,\r
+               unsigned loopCount) {\r
+  std::vector<DATA_TYPE> array(base_array);\r
+  for (unsigned u = 0; u < loopCount; ++u) {\r
+    for (unsigned v = 0; v < base_array.size(); ++v) {\r
+      array[v] = base_array[v];\r
+    }\r
+    sort_core(array, stdSort, loopCount);\r
+  }\r
+}\r
+\r
+//Pass in an argument to test std::sort\r
+int main(int argc, const char ** argv) {\r
+  std::ifstream indata;\r
+  std::ofstream outfile;\r
+  bool stdSort = false;\r
+  int constant_to_random_ratio = -1;\r
+  int threadCount = -1;\r
+  unsigned loopCount = 0;\r
+  for (int u = 1; u < argc; ++u) {\r
+    if (std::string(argv[u]) == "-std")\r
+      stdSort = true;\r
+    else if(threadCount < 0)\r
+      threadCount = atoi(argv[u]);\r
+    else if (!loopCount)\r
+      loopCount = atoi(argv[u]);\r
+    else\r
+      constant_to_random_ratio = atoi(argv[u]);\r
+  }\r
+  if (!loopCount) {\r
+    loopCount = 1;\r
+  }\r
+  printf("threads: %d loops: %d\n", threadCount, loopCount);\r
+\r
+  std::vector<DATA_TYPE> base_array;\r
+  if (constant_to_random_ratio >= 0) {\r
+    //Test for random data with gaps of identical data.\r
+    random::mt19937 generator;\r
+    random::uniform_int_distribution<int> distribution(0,255);\r
+    const int constant_to_random_count = 1000000;\r
+    const int string_length = 1000;\r
+    for (int i = 0; i < constant_to_random_count; ++i) {\r
+      DATA_TYPE temp(string_length, 'x');  // fill with default character.\r
+      for (int j = constant_to_random_ratio; j < string_length;) {\r
+        int val = distribution(generator);\r
+        temp[j] = val;\r
+        j += (val * constant_to_random_ratio)/128 + 1;\r
+      }\r
+      base_array.push_back(temp);\r
+    }\r
+  } else {\r
+    indata.open("input.txt", std::ios_base::in | std::ios_base::binary);\r
+    if (indata.bad()) {\r
+      printf("input.txt could not be opened\n");\r
+      return 1;\r
+    }\r
+    DATA_TYPE inval;\r
+    while (!indata.eof() ) {\r
+      indata >> inval;\r
+      base_array.push_back(inval);\r
+    }\r
+  }\r
+\r
+  // Sort the input\r
+  clock_t start, end;\r
+  double elapsed;\r
+  std::vector<boost::thread *> workers;\r
+  start = clock();\r
+  if (threadCount == 0) {\r
+    if (loopCount > 1) {\r
+      sort_loop(base_array, stdSort, loopCount);\r
+    } else {\r
+      sort_core(base_array, stdSort, loopCount);\r
+    }\r
+    threadCount = 1;\r
+  } else {\r
+    for (int i = 0; i < threadCount; ++i) {\r
+      workers.push_back(new boost::thread(sort_loop, base_array, stdSort,\r
+                                          loopCount));\r
+    }\r
+    for (int i = 0; i < threadCount; ++i) {\r
+      workers[i]->join();\r
+      delete workers[i];\r
+    }\r
+  }\r
+  end = clock();\r
+  elapsed = static_cast<double>(end - start) ;\r
+\r
+  printf("for %lu strings\n", base_array.size());\r
+  if (stdSort)\r
+    printf("std::sort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);\r
+  else\r
+    printf("spreadsort clock time %lf\n", elapsed/CLOCKS_PER_SEC/threadCount);\r
+  return 0;\r
+}\r