sort.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) map mapping (N, nthreads) pairs to WNEvalPlans
  19. struct EvalPlans {
  20. pthread_mutex_t mutex;
  21. std::map<std::pair<uint32_t,threadid_t>,WNEvalPlan> eval_plans;
  22. EvalPlans() { pthread_mutex_init(&mutex, NULL); }
  23. };
  24. static EvalPlans precomp_eps;
  25. size_t sort_precompute(uint32_t N)
  26. {
  27. uint32_t *random_permutation = NULL;
  28. try {
  29. random_permutation = new uint32_t[N];
  30. } catch (std::bad_alloc&) {
  31. printf("Allocating memory failed in sort_precompute\n");
  32. assert(false);
  33. }
  34. for (uint32_t i=0;i<N;++i) {
  35. random_permutation[i] = i;
  36. }
  37. RecursiveShuffle_M2((unsigned char *) random_permutation, N, sizeof(uint32_t));
  38. WaksmanNetwork wnet(N);
  39. wnet.setPermutation(random_permutation);
  40. // Note that sized_wns[N] creates a map entry for N if it doesn't yet exist
  41. pthread_mutex_lock(&precomp_wns.mutex);
  42. SizedWNs& szwn = precomp_wns.sized_wns[N];
  43. pthread_mutex_unlock(&precomp_wns.mutex);
  44. pthread_mutex_lock(&szwn.mutex);
  45. szwn.wns.push_back(std::move(wnet));
  46. size_t ret = szwn.wns.size();
  47. pthread_mutex_unlock(&szwn.mutex);
  48. return ret;
  49. }
  50. void sort_precompute_evalplan(uint32_t N, threadid_t nthreads)
  51. {
  52. std::pair<uint32_t,threadid_t> idx = {N, nthreads};
  53. pthread_mutex_lock(&precomp_eps.mutex);
  54. if (!precomp_eps.eval_plans.count(idx)) {
  55. precomp_eps.eval_plans.try_emplace(idx, N, nthreads);
  56. }
  57. pthread_mutex_unlock(&precomp_eps.mutex);
  58. }
  59. // Perform the sort using up to nthreads threads. The items to sort are
  60. // byte arrays of size msg_size. The key is the first 4 bytes of each
  61. // item.
  62. void sort_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  63. uint32_t Nr, uint32_t Na,
  64. // the arguments to the callback are nthreads, items, the sorted
  65. // indices, and the number of non-padding items
  66. std::function<void(threadid_t, const uint8_t*, const uint64_t*,
  67. uint32_t Nr)> cb)
  68. {
  69. // Find the smallest Nw for which we have a precomputed
  70. // WaksmanNetwork with Nr <= Nw <= Na
  71. pthread_mutex_lock(&precomp_wns.mutex);
  72. std::optional<WaksmanNetwork> wn;
  73. uint32_t Nw;
  74. for (auto& N : precomp_wns.sized_wns) {
  75. if (N.first > Na) {
  76. printf("No precomputed WaksmanNetworks of size at most %u\n", Na);
  77. assert(false);
  78. }
  79. if (N.first < Nr) {
  80. continue;
  81. }
  82. // We're in the right range, but see if we have an actual
  83. // precomputed WaksmanNetwork
  84. pthread_mutex_lock(&N.second.mutex);
  85. if (N.second.wns.size() == 0) {
  86. pthread_mutex_unlock(&N.second.mutex);
  87. continue;
  88. }
  89. wn = std::move(N.second.wns.front());
  90. N.second.wns.pop_front();
  91. Nw = N.first;
  92. pthread_mutex_unlock(&N.second.mutex);
  93. break;
  94. }
  95. pthread_mutex_unlock(&precomp_wns.mutex);
  96. if (!wn) {
  97. printf("No precomputed WaksmanNetwork of size range [%u,%u] found.\n",
  98. Nr, Na);
  99. assert(wn);
  100. }
  101. std::pair<uint32_t,threadid_t> epidx = {Nw, nthreads};
  102. pthread_mutex_lock(&precomp_eps.mutex);
  103. if (!precomp_eps.eval_plans.count(epidx)) {
  104. printf("No precomputed WNEvalPlan with N=%u, nthreads=%hu\n",
  105. Nw, nthreads);
  106. assert(false);
  107. }
  108. const WNEvalPlan &eval_plan = precomp_eps.eval_plans.at(epidx);
  109. pthread_mutex_unlock(&precomp_eps.mutex);
  110. // Mark Nw-Nr items as padding (Nr, Na, and Nw are _not_ private)
  111. for (uint32_t i=Nr; i<Nw; ++i) {
  112. (*(uint32_t*)(items+msg_size*i)) = uint32_t(-1);
  113. }
  114. // Shuffle Nw items
  115. wn.value().applyInversePermutation<OSWAP_16X>(
  116. items, msg_size, eval_plan);
  117. // Create the indices
  118. uint64_t *idx = new uint64_t[Nr];
  119. uint64_t *nextidx = idx;
  120. for (uint32_t i=0; i<Nw; ++i) {
  121. uint64_t key = (*(uint32_t*)(items+msg_size*i));
  122. if (key != uint32_t(-1)) {
  123. *nextidx = (key<<32) + i;
  124. ++nextidx;
  125. }
  126. }
  127. if (nextidx != idx + Nr) {
  128. printf("Found %u non-padding items, expected %u\n",
  129. nextidx-idx, Nr);
  130. assert(nextidx == idx + Nr);
  131. }
  132. // Sort the keys and indices
  133. uint64_t *backingidx = new uint64_t[Nr];
  134. bool whichbuf = mtmergesort<uint64_t>(idx, Nr, backingidx, nthreads);
  135. uint64_t *sortedidx = whichbuf ? backingidx : idx;
  136. for (uint32_t i=0; i<Nr; ++i) {
  137. sortedidx[i] &= uint64_t(0xffffffff);
  138. }
  139. cb(nthreads, items, sortedidx, Nr);
  140. delete[] idx;
  141. delete[] backingidx;
  142. }