sort.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef __SORT_HPP__
  2. #define __SORT_HPP__
  3. #include <functional>
  4. #include "WaksmanNetwork.hpp"
  5. // We have Nr elements to sort, at the beginning of an array of
  6. // allocated size Na items. Nr and Na are _not_ secret. The strategy
  7. // is to get the smallest (precomputed) WaksmanNetwork (on a random
  8. // permutation) and WNEvalPlan, each of size Nw, where Nr <= Nw <= Na.
  9. // Nw is also not secret. Then mark the Nw-Nr items following the given
  10. // elements as padding, use the WaksmanNetwork and WNEvalPlan to shuffle
  11. // the Nw items, then non-obliviously sort the non-padding items. The
  12. // sort itself is done by making an index array holding just the sort
  13. // keys and original indices into the array, but only of the non-padding
  14. // items. Sort the index array, and call back a std::function with the
  15. // sorted index array and the shuffled array so that it can read out the
  16. // elements in sorted order. This function does not have to be
  17. // oblivious to what elements it is reading, because they've already
  18. // been obliviously shuffled.
  19. // Precompute a WaksmanNetwork of size N for a random permutation. This
  20. // call does not itself use threads, but may be called from a background
  21. // thread. These are consumed as they are used, so you need to keep
  22. // making more.
  23. void sort_precompute(uint32_t N);
  24. // Precompute a WNEvalPlan for a given size and number of threads.
  25. // These are not consumed as they are used, so you only need to call
  26. // this once for each size/nthreads you need. The precomputation itself
  27. // only uses a single thread, but also may be called from a background
  28. // thread.
  29. void sort_precompute_evalplan(uint32_t N, threadid_t nthreads);
  30. // Perform the sort using up to nthreads threads. The items to sort are
  31. // byte arrays of size msg_size. The key is the 10-bit storage server
  32. // id contatenated with the 22-bit uid at the storage server.
  33. void sort_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  34. uint32_t Nr, uint32_t Na,
  35. // the arguments to the callback are nthreads, items, the sorted
  36. // indices, and the number of non-padding items
  37. std::function<void(threadid_t, const uint8_t*, const uint64_t*, uint32_t Nr)>);
  38. #endif