route.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "sort.hpp"
  6. #include "comms.hpp"
  7. #include "route.hpp"
  8. #define PROFILE_ROUTING
  9. struct MsgBuffer {
  10. pthread_mutex_t mutex;
  11. uint8_t *buf;
  12. // The number of messages (not bytes) in, or on their way into, the
  13. // buffer
  14. uint32_t reserved;
  15. // The number of messages definitely in the buffer
  16. uint32_t inserted;
  17. // The number of messages that can fit in buf
  18. uint32_t bufsize;
  19. MsgBuffer() : buf(NULL), reserved(0), inserted(0), bufsize(0) {
  20. pthread_mutex_init(&mutex, NULL);
  21. }
  22. ~MsgBuffer() {
  23. delete[] buf;
  24. }
  25. // The number passed is messages, not bytes
  26. void alloc(uint32_t msgs) {
  27. delete[] buf;
  28. buf = NULL;
  29. reserved = 0;
  30. inserted = 0;
  31. // This may throw bad_alloc, but we'll catch it higher up
  32. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  33. bufsize = msgs;
  34. }
  35. // Reset the contents of the buffer
  36. void reset() {
  37. memset(buf, 0, bufsize * g_teems_config.msg_size);
  38. reserved = 0;
  39. inserted = 0;
  40. }
  41. // You can't copy a MsgBuffer
  42. MsgBuffer(const MsgBuffer&) = delete;
  43. MsgBuffer &operator=(const MsgBuffer&) = delete;
  44. };
  45. enum RouteStep {
  46. ROUTE_NOT_STARTED,
  47. ROUTE_ROUND_1,
  48. ROUTE_ROUND_2
  49. };
  50. // The round1 MsgBuffer stores messages we ingest while waiting for
  51. // round 1 to start, which will be sorted and sent out in round 1. The
  52. // round2 MsgBuffer stores messages we receive in round 1, which will be
  53. // padded, sorted, and sent out in round 2.
  54. static struct RouteState {
  55. MsgBuffer round1;
  56. MsgBuffer round2;
  57. RouteStep step;
  58. uint32_t tot_msg_per_ing;
  59. uint32_t max_msg_to_each_str;
  60. uint32_t max_round2_msgs;
  61. } route_state;
  62. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  63. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  64. // Call this near the end of ecall_config_load, but before
  65. // comms_init_nodestate. Returns true on success, false on failure.
  66. bool route_init()
  67. {
  68. // Compute the maximum number of messages we could receive by direct
  69. // ingestion
  70. // Each ingestion node will have at most
  71. // ceil(user_count/num_ingestion_nodes) users, and each user will
  72. // send at most m_priv_out messages.
  73. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  74. g_teems_config.num_ingestion_nodes);
  75. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  76. // Compute the maximum number of messages we could receive in round 1
  77. // Each ingestion node will send us an our_weight/tot_weight
  78. // fraction of the messages they hold
  79. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  80. g_teems_config.tot_weight) * g_teems_config.my_weight;
  81. // And the maximum number we can receive in total is that times the
  82. // number of ingestion nodes
  83. uint32_t max_round1_msgs = max_msg_from_each_ing *
  84. g_teems_config.num_ingestion_nodes;
  85. // Compute the maximum number of messages we could send in round 2
  86. // Each storage node has at most this many users
  87. uint32_t users_per_str = CEILDIV(g_teems_config.user_count,
  88. g_teems_config.num_storage_nodes);
  89. // And so can receive at most this many messages
  90. uint32_t tot_msg_per_str = users_per_str *
  91. g_teems_config.m_priv_in;
  92. // Which will be at most this many from us
  93. uint32_t max_msg_to_each_str = CEILDIV(tot_msg_per_str,
  94. g_teems_config.tot_weight) * g_teems_config.my_weight;
  95. // But we can't send more messages to each storage server than we
  96. // could receive in total
  97. if (max_msg_to_each_str > max_round1_msgs) {
  98. max_msg_to_each_str = max_round1_msgs;
  99. }
  100. // And the max total number of outgoing messages in round 2 is then
  101. uint32_t max_round2_msgs = max_msg_to_each_str *
  102. g_teems_config.num_storage_nodes;
  103. // In case we have a weird configuration where users can send more
  104. // messages per epoch than they can receive, ensure the round 2
  105. // buffer is large enough to hold the incoming messages as well
  106. if (max_round2_msgs < max_round1_msgs) {
  107. max_round2_msgs = max_round1_msgs;
  108. }
  109. /*
  110. printf("round1_msgs = %u, round2_msgs = %u\n",
  111. max_round1_msgs, max_round2_msgs);
  112. */
  113. // Create the route state
  114. try {
  115. route_state.round1.alloc(tot_msg_per_ing);
  116. route_state.round2.alloc(max_round2_msgs);
  117. } catch (std::bad_alloc&) {
  118. printf("Memory allocation failed in route_init\n");
  119. return false;
  120. }
  121. route_state.step = ROUTE_NOT_STARTED;
  122. route_state.tot_msg_per_ing = tot_msg_per_ing;
  123. route_state.max_msg_to_each_str = max_msg_to_each_str;
  124. route_state.max_round2_msgs = max_round2_msgs;
  125. threadid_t nthreads = g_teems_config.nthreads;
  126. #ifdef PROFILE_ROUTING
  127. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  128. #endif
  129. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  130. sort_precompute_evalplan(max_round2_msgs, nthreads);
  131. #ifdef PROFILE_ROUTING
  132. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  133. #endif
  134. return true;
  135. }
  136. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  137. // it will return the number of different sizes it needs. If you pass
  138. // [0,sizes-1], it will compute one WaksmanNetwork with that size index
  139. // and return the number of available WaksmanNetworks of that size.
  140. size_t ecall_precompute_sort(int sizeidx)
  141. {
  142. size_t ret = 0;
  143. switch(sizeidx) {
  144. case 0:
  145. #ifdef PROFILE_ROUTING
  146. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.tot_msg_per_ing);
  147. #endif
  148. ret = sort_precompute(route_state.tot_msg_per_ing);
  149. #ifdef PROFILE_ROUTING
  150. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.tot_msg_per_ing);}
  151. #endif
  152. break;
  153. case 1:
  154. #ifdef PROFILE_ROUTING
  155. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.max_round2_msgs);
  156. #endif
  157. ret = sort_precompute(route_state.max_round2_msgs);
  158. #ifdef PROFILE_ROUTING
  159. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.max_round2_msgs);}
  160. #endif
  161. break;
  162. default:
  163. ret = 2;
  164. break;
  165. }
  166. return ret;
  167. }
  168. // Directly ingest a buffer of num_msgs messages into the round1 buffer.
  169. // Return true on success, false on failure.
  170. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  171. {
  172. uint16_t msg_size = g_teems_config.msg_size;
  173. MsgBuffer &round1 = route_state.round1;
  174. pthread_mutex_lock(&round1.mutex);
  175. uint32_t start = round1.reserved;
  176. if (start + num_msgs > route_state.tot_msg_per_ing) {
  177. pthread_mutex_unlock(&round1.mutex);
  178. printf("Max %u messages exceeded\n",
  179. route_state.tot_msg_per_ing);
  180. return false;
  181. }
  182. round1.reserved += num_msgs;
  183. pthread_mutex_unlock(&round1.mutex);
  184. memmove(round1.buf + start * msg_size,
  185. msgs, num_msgs * msg_size);
  186. pthread_mutex_lock(&round1.mutex);
  187. round1.inserted += num_msgs;
  188. pthread_mutex_unlock(&round1.mutex);
  189. return true;
  190. }
  191. // Send the round 1 messages. Note that N here is not private.
  192. static void send_round1_msgs(const uint8_t *msgs, const uint64_t *indices,
  193. uint32_t N)
  194. {
  195. uint16_t msg_size = g_teems_config.msg_size;
  196. uint16_t tot_weight = g_teems_config.tot_weight;
  197. nodenum_t my_node_num = g_teems_config.my_node_num;
  198. uint32_t full_rows = N / uint32_t(tot_weight);
  199. uint32_t last_row = N % uint32_t(tot_weight);
  200. for (auto &routing_node: g_teems_config.routing_nodes) {
  201. uint8_t weight =
  202. g_teems_config.weights[routing_node].weight;
  203. if (weight == 0) {
  204. // This shouldn't happen, but just in case
  205. continue;
  206. }
  207. uint16_t start_weight =
  208. g_teems_config.weights[routing_node].startweight;
  209. // The number of messages headed for this routing node from the
  210. // full rows
  211. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  212. // The number of messages headed for this routing node from the
  213. // incomplete last row is:
  214. // 0 if last_row < start_weight
  215. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  216. // weight if start_weight + weight <= last_row
  217. uint32_t num_msgs_last_row = 0;
  218. if (start_weight <= last_row && last_row < start_weight + weight) {
  219. num_msgs_last_row = last_row-start_weight;
  220. } else if (start_weight + weight <= last_row) {
  221. num_msgs_last_row = weight;
  222. }
  223. // The total number of messages headed for this routing node
  224. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  225. if (routing_node == my_node_num) {
  226. // Special case: we're sending to ourselves; just put the
  227. // messages in our own round2 buffer
  228. MsgBuffer &round2 = route_state.round2;
  229. pthread_mutex_lock(&round2.mutex);
  230. uint32_t start = round2.reserved;
  231. if (start + num_msgs > round2.bufsize) {
  232. pthread_mutex_unlock(&round2.mutex);
  233. printf("Max %u messages exceeded\n", round2.bufsize);
  234. return;
  235. }
  236. round2.reserved += num_msgs;
  237. pthread_mutex_unlock(&round2.mutex);
  238. uint8_t *buf = round2.buf + start * msg_size;
  239. for (uint32_t i=0; i<full_rows; ++i) {
  240. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  241. for (uint32_t j=0; j<weight; ++j) {
  242. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  243. buf += msg_size;
  244. }
  245. }
  246. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  247. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  248. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  249. buf += msg_size;
  250. }
  251. pthread_mutex_lock(&round2.mutex);
  252. round2.inserted += num_msgs;
  253. pthread_mutex_unlock(&round2.mutex);
  254. } else {
  255. NodeCommState &nodecom = g_commstates[routing_node];
  256. nodecom.message_start(num_msgs * msg_size);
  257. for (uint32_t i=0; i<full_rows; ++i) {
  258. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  259. for (uint32_t j=0; j<weight; ++j) {
  260. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  261. }
  262. }
  263. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  264. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  265. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  266. }
  267. }
  268. }
  269. /*
  270. for (uint32_t i=0;i<N;++i) {
  271. const uint8_t *msg = msgs + indices[i]*msg_size;
  272. for (uint16_t j=0;j<msg_size/4;++j) {
  273. printf("%08x ", ((const uint32_t*)msg)[j]);
  274. }
  275. printf("\n");
  276. }
  277. */
  278. }
  279. // Perform the next round of routing. The callback pointer will be
  280. // passed to ocall_routing_round_complete when the round is complete.
  281. void ecall_routing_proceed(void *cbpointer)
  282. {
  283. if (route_state.step == ROUTE_NOT_STARTED) {
  284. MsgBuffer &round1 = route_state.round1;
  285. pthread_mutex_lock(&round1.mutex);
  286. // Ensure there are no pending messages currently being inserted
  287. // into the buffer
  288. while (round1.reserved != round1.inserted) {
  289. pthread_mutex_unlock(&round1.mutex);
  290. pthread_mutex_lock(&round1.mutex);
  291. }
  292. // Sort the messages we've received
  293. #ifdef PROFILE_ROUTING
  294. uint32_t inserted = round1.inserted;
  295. unsigned long start = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  296. #endif
  297. sort_mtobliv(g_teems_config.nthreads, round1.buf,
  298. g_teems_config.msg_size, round1.inserted,
  299. route_state.tot_msg_per_ing, send_round1_msgs);
  300. round1.reset();
  301. #ifdef PROFILE_ROUTING
  302. printf_with_rtclock_diff(start, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  303. #endif
  304. route_state.step = ROUTE_ROUND_1;
  305. ocall_routing_round_complete(cbpointer, 1);
  306. }
  307. }