parallel_stable_sort.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. [/===========================================================================
  2. Copyright (c) 2017 Steven Ross, Francisco Tapia, Orson Peters
  3. Distributed under the Boost Software License, Version 1.0
  4. See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. =============================================================================/]
  7. [section:parallel_stable_sort 3.3.- Parallel_stable_sort]
  8. [section:parallel_description Description]
  9. [:
  10. This is an adaptation of the [*[@https://en.wikipedia.org/wiki/Samplesort Samplesort]] algorithm,
  11. using an additional memory a half of the memory used by the data
  12. (the original algorithm uses an additional memory as the used by the data).
  13. It is a highly efficient [*parallel stable sort], optimized for use with many threads.
  14. [*[teletype]
  15. ``
  16. | | | |
  17. Algorithm |Stable | Additional memory |Best, average, and worst case |
  18. ----------------------+-------+------------------------+------------------------------+
  19. parallel_stable_sort | yes | N / 2 | N, N LogN , N LogN |
  20. | | | |
  21. ``
  22. ]
  23. You can see their performance in the [*[link sort.parallel.linux_parallel Benchmarks]] chapter
  24. ]
  25. [endsect]
  26. [section:parallel_programming Programming]
  27. [:
  28. [h4[_Thread specification]]
  29. [:
  30. This algorithm has an integer parameter indicating the *number of threads* to use in the sorting process,
  31. which always is the last value in the call.
  32. The default value (if left unspecified) is the number of HW threads of
  33. the machine where the program is running provided by std::thread::hardware_concurrency().
  34. If the number is 1 or 0, the algorithm runs with only 1 thread.
  35. The number of threads is not a fixed number, but is calculated in each execution. The number of threads passed can be greater
  36. than the number of hardware threads on the machine. We can pass 100 threads in a machine with 4 HW threads,
  37. and in the same mode we can pass a function as (std::thread::hardware_concurrency() / 4 ).
  38. If this value is 0, the program is executed with 1 thread.
  39. ]
  40. [h4[_Programming]]
  41. [:
  42. You only need to include the file boost/sort/sort.hpp to use this code.
  43. This algorithm run in the namespace boost::sort.
  44. [c++]
  45. ``
  46. #include <boost/sort/sort.hpp>
  47. template <class iter_t>
  48. void parallel_stable_sort (iter_t first, iter_t last);
  49. template <class iter_t, typename compare>
  50. void parallel_stable_sort (iter_t first, iter_t last, compare comp);
  51. template <class iter_t>
  52. void parallel_stable_sort (iter_t first, iter_t last, uint32_t num_thread);
  53. template <class iter_t, typename compare>
  54. void parallel_stable_sort (iter_t first, iter_t last, compare comp, uint32_t num_thread);
  55. ``
  56. This algorithm needs a *C++11 compliant compiler*, and doesn't need any other code or library. Correct operation is not guaranteed with older compilers.
  57. If the number of threads is unspecified, this uses the result of std::thread::hardware_concurrency().
  58. This algorithm uses a *comparison object*, in the same way as the standard library sort
  59. algorithms. If not defined, the comparison object is std::less, which uses
  60. the < operator internally.
  61. This algorithm is [*exception safe], meaning that, the exceptions generated by the algorithm
  62. guarantee the integrity of the objects to sort, but not their relative order. If the exception
  63. is generated inside the objects (in the move or in the copy constructor.. ) the results can be
  64. unpredictable.
  65. ]
  66. ]
  67. [endsect]
  68. [endsect]