sort.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. Returns the number of WaksmanNetworks available at that
  23. // size.
  24. size_t sort_precompute(uint32_t N);
  25. // Precompute a WNEvalPlan for a given size and number of threads.
  26. // These are not consumed as they are used, so you only need to call
  27. // this once for each (size,nthreads) pair you need. The precomputation
  28. // itself only uses a single thread, but also may be called from a
  29. // background thread.
  30. void sort_precompute_evalplan(uint32_t N, threadid_t nthreads);
  31. // Perform the sort using up to nthreads threads. The items to sort are
  32. // byte arrays of size msg_size. The key is the 10-bit storage server
  33. // id contatenated with the 22-bit uid at the storage server.
  34. void sort_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  35. uint32_t Nr, uint32_t Na,
  36. // the arguments to the callback are items, the sorted indices, and
  37. // the number of non-padding items
  38. std::function<void(const uint8_t*, const uint64_t*, uint32_t Nr)>);
  39. #endif