sort.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // Shuffle Nr items at the beginning of an allocated array of Na items
  32. // using up to nthreads threads. The items to shuffle are byte arrays
  33. // of size msg_size. Return Nw, the size of the Waksman network we
  34. // used, which must satisfy Nr <= Nw <= Na.
  35. uint32_t shuffle_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  36. uint32_t Nr, uint32_t Na);
  37. // Perform the sort using up to nthreads threads. The items to sort are
  38. // byte arrays of size msg_size. The key is the 10-bit storage server
  39. // id concatenated with the 22-bit uid at the storage server.
  40. void sort_mtobliv(threadid_t nthreads, uint8_t* items, uint16_t msg_size,
  41. uint32_t Nr, uint32_t Na,
  42. // the arguments to the callback are items, the sorted indices, and
  43. // the number of non-padding items
  44. std::function<void(const uint8_t*, const uint64_t*, uint32_t Nr)>);
  45. #endif