sort.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <map>
  2. #include <deque>
  3. #include <pthread.h>
  4. #include "sort.hpp"
  5. // A set of precomputed WaksmanNetworks of a given size
  6. struct SizedWNs {
  7. pthread_mutex_t mutex;
  8. std::deque<WaksmanNetwork> wns;
  9. SizedWNs() { pthread_mutex_init(&mutex, NULL); }
  10. };
  11. // A (mutexed) map mapping sizes to SizedWNs
  12. struct PrecompWNs {
  13. pthread_mutex_t mutex;
  14. std::map<uint32_t,SizedWNs> sized_wns;
  15. PrecompWNs() { pthread_mutex_init(&mutex, NULL); }
  16. };
  17. static PrecompWNs precomp_wns;
  18. // A (mutexed) vector of sizes we've used since we were last asked
  19. struct UsedSizes {
  20. pthread_mutex_t mutex;
  21. std::vector<uint32_t> used;
  22. UsedSizes() { pthread_mutex_init(&mutex, NULL); }
  23. };
  24. static UsedSizes used_sizes;
  25. // A (mutexed) map mapping (N, nthreads) pairs to WNEvalPlans
  26. struct EvalPlans {
  27. pthread_mutex_t mutex;
  28. std::map<std::pair<uint32_t,threadid_t>,WNEvalPlan> eval_plans;
  29. EvalPlans() { pthread_mutex_init(&mutex, NULL); }
  30. };
  31. static EvalPlans precomp_eps;
  32. size_t sort_precompute(uint32_t N)
  33. {
  34. uint32_t *random_permutation = NULL;
  35. try {
  36. random_permutation = new uint32_t[N];
  37. } catch (std::bad_alloc&) {
  38. printf("Allocating memory failed in sort_precompute\n");
  39. assert(false);
  40. }
  41. for (uint32_t i=0;i<N;++i) {
  42. random_permutation[i] = i;
  43. }
  44. RecursiveShuffle_M2((unsigned char *) random_permutation, N, sizeof(uint32_t));
  45. WaksmanNetwork wnet(N);
  46. wnet.setPermutation(random_permutation);
  47. // Note that sized_wns[N] creates a map entry for N if it doesn't yet exist
  48. pthread_mutex_lock(&precomp_wns.mutex);
  49. SizedWNs& szwn = precomp_wns.sized_wns[N];
  50. pthread_mutex_unlock(&precomp_wns.mutex);
  51. pthread_mutex_lock(&szwn.mutex);
  52. szwn.wns.push_back(std::move(wnet));
  53. size_t ret = szwn.wns.size();
  54. pthread_mutex_unlock(&szwn.mutex);
  55. return ret;
  56. }
  57. void sort_precompute_evalplan(uint32_t N, threadid_t nthreads)
  58. {
  59. std::pair<uint32_t,threadid_t> idx = {N, nthreads};
  60. pthread_mutex_lock(&precomp_eps.mutex);
  61. if (!precomp_eps.eval_plans.count(idx)) {
  62. precomp_eps.eval_plans.try_emplace(idx, N, nthreads);
  63. }
  64. pthread_mutex_unlock(&precomp_eps.mutex);
  65. }
  66. // Shuffle Nr items at the beginning of an allocated array of Na items
  67. // using up to nthreads threads. The items to shuffle are byte arrays
  68. // of size msg_size. Return Nw, the size of the Waksman network we
  69. // used, which must satisfy Nr <= Nw <= Na.
  70. uint32_t shuffle_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  71. uint32_t Nr, uint32_t Na)
  72. {
  73. // Find the smallest Nw for which we have a precomputed
  74. // WaksmanNetwork with Nr <= Nw <= Na
  75. pthread_mutex_lock(&precomp_wns.mutex);
  76. std::optional<WaksmanNetwork> wn;
  77. uint32_t Nw = 0;
  78. for (auto& N : precomp_wns.sized_wns) {
  79. if (N.first > Na) {
  80. printf("No precomputed WaksmanNetworks of size at most %u\n", Na);
  81. assert(false);
  82. }
  83. if (N.first < Nr) {
  84. continue;
  85. }
  86. // We're in the right range, but see if we have an actual
  87. // precomputed WaksmanNetwork
  88. pthread_mutex_lock(&N.second.mutex);
  89. if (N.second.wns.size() == 0) {
  90. pthread_mutex_unlock(&N.second.mutex);
  91. continue;
  92. }
  93. pthread_mutex_lock(&used_sizes.mutex);
  94. used_sizes.used.push_back(N.first);
  95. pthread_mutex_unlock(&used_sizes.mutex);
  96. wn = std::move(N.second.wns.front());
  97. N.second.wns.pop_front();
  98. Nw = N.first;
  99. pthread_mutex_unlock(&N.second.mutex);
  100. break;
  101. }
  102. pthread_mutex_unlock(&precomp_wns.mutex);
  103. if (!wn) {
  104. printf("No precomputed WaksmanNetwork of size range [%u,%u] found.\n",
  105. Nr, Na);
  106. assert(wn);
  107. }
  108. std::pair<uint32_t,threadid_t> epidx = {Nw, nthreads};
  109. pthread_mutex_lock(&precomp_eps.mutex);
  110. if (!precomp_eps.eval_plans.count(epidx)) {
  111. printf("No precomputed WNEvalPlan with N=%u, nthreads=%hu\n",
  112. Nw, nthreads);
  113. assert(false);
  114. }
  115. const WNEvalPlan &eval_plan = precomp_eps.eval_plans.at(epidx);
  116. pthread_mutex_unlock(&precomp_eps.mutex);
  117. // Mark Nw-Nr items as padding (Nr, Na, and Nw are _not_ private)
  118. for (uint32_t i=Nr; i<Nw; ++i) {
  119. (*(uint32_t*)(items+msg_size*i)) = uint32_t(-1);
  120. }
  121. // Shuffle Nw items
  122. wn.value().applyInversePermutation<OSWAP_16X>(
  123. items, msg_size, eval_plan);
  124. return Nw;
  125. }
  126. std::vector<uint32_t> sort_get_used()
  127. {
  128. std::vector<uint32_t> res;
  129. pthread_mutex_lock(&used_sizes.mutex);
  130. res = std::move(used_sizes.used);
  131. pthread_mutex_unlock(&used_sizes.mutex);
  132. return res;
  133. }