worker.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include <pthread.h>
  2. extern "C" {
  3. #include <event2/thread.h>
  4. #include <event2/bufferevent.h>
  5. #include <event2/buffer.h>
  6. #include <event2/event.h>
  7. #include <event2/util.h>
  8. }
  9. #include <NTL/ZZ_p.h>
  10. #include <vector>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <signal.h>
  14. #include "cudadl.h"
  15. #include "evutils.h"
  16. #include "subproblem.h"
  17. #include "worker.h"
  18. NTL_CLIENT
  19. #define DEBUG
  20. typedef enum {
  21. WRKCCSTATE_AWAITCMD,
  22. WRKCCSTATE_RDPROBLEM,
  23. WRKCCSTATE_RDDPNODES,
  24. WRKCCSTATE_END
  25. } WrkCCState;
  26. struct WrkControllerConnInfo {
  27. WrkCCState state;
  28. unsigned short num_dpnodes;
  29. WrkControllerConnInfo() : state(WRKCCSTATE_AWAITCMD), num_dpnodes(0) {}
  30. };
  31. typedef enum { WT_NOT_RUNNING, WT_RUNNING, WT_SHOULD_STOP} WTState;
  32. static struct WrkControllerState {
  33. Subproblem *current_problem;
  34. unsigned short num_expected_dpnodes;
  35. vector<struct bufferevent *> dpnodes;
  36. unsigned short num_connected_dpnodes;
  37. WTState worker_thread_state;
  38. pthread_t worker_thread;
  39. WrkControllerState(): current_problem(NULL),
  40. worker_thread_state(WT_NOT_RUNNING) {}
  41. } wrkctrlstate;
  42. // ----- Below this line are the functions running in the worker thread.
  43. #if WORDS > 1
  44. #define DEMUXWORD 8
  45. #else
  46. #define DEMUXWORD 7
  47. #endif
  48. // This function is called from inside cuda_dl for each DP it encounters.
  49. // It calls the function named "dpcallback" directly. It would be
  50. // cleaner if this were passed as a function pointer to cuda_dl, but
  51. // that makes nvcc 3.1 segfault. :-p
  52. // dp points to an array of WORDS+7 unsigned ints:
  53. // - 1 word of threadID/blockID
  54. // - WORDS words of the dp value
  55. // - 3 words of a
  56. // - 3 words of b
  57. bool dpcallback(void *cbdata, unsigned int *dpwords)
  58. {
  59. unsigned int demux = dpwords[DEMUXWORD];
  60. struct bufferevent *bev =
  61. wrkctrlstate.dpnodes[demux % wrkctrlstate.num_connected_dpnodes];
  62. bufferevent_write(bev, dpwords+1, (WORDS+6)*sizeof(unsigned int));
  63. // If worker_thread_state changes to WT_SHOULD_STOP, then signal to
  64. // stop computation by returning true. If for some reason, it
  65. // becomes WT_NOT_RUNNING (which it shouldn't), stop as well.
  66. return wrkctrlstate.worker_thread_state != WT_RUNNING;
  67. }
  68. static void *worker_thread_start(void *data)
  69. {
  70. ZZ_p::init(wrkctrlstate.current_problem->modulus);
  71. cuda_dl(to_ZZ_p(wrkctrlstate.current_problem->base),
  72. to_ZZ_p(wrkctrlstate.current_problem->target),
  73. wrkctrlstate.current_problem->order,
  74. wrkctrlstate.current_problem->modulus,
  75. wrkctrlstate.current_problem->dpfreq, NULL);
  76. return NULL;
  77. }
  78. // ----- Above this line are the functions running in the worker thread.
  79. // Below are the functions running in the main (communication) thread.
  80. static void stop_working(void)
  81. {
  82. #ifdef DEBUG
  83. cerr << "Stopping work\n";
  84. #endif
  85. if (wrkctrlstate.worker_thread_state != WT_NOT_RUNNING) {
  86. // Tell the worker thread to stop after its next kernel launch
  87. wrkctrlstate.worker_thread_state = WT_SHOULD_STOP;
  88. pthread_join(wrkctrlstate.worker_thread, NULL);
  89. wrkctrlstate.worker_thread_state = WT_NOT_RUNNING;
  90. }
  91. // Close the connections to the dpnodes
  92. vector<struct bufferevent *>::iterator bevit;
  93. for (bevit = wrkctrlstate.dpnodes.begin();
  94. bevit != wrkctrlstate.dpnodes.end(); ++bevit) {
  95. #ifdef DEBUG
  96. cerr << "Closing connection to " << *bevit << "\n";
  97. #endif
  98. bufferevent_free(*bevit);
  99. }
  100. wrkctrlstate.dpnodes.clear();
  101. wrkctrlstate.num_connected_dpnodes = 0;
  102. wrkctrlstate.num_expected_dpnodes = 0;
  103. // Careful! Subproblem uses NTL, so we must be sure we're not
  104. // multithreaded at this point.
  105. delete wrkctrlstate.current_problem;
  106. wrkctrlstate.current_problem = NULL;
  107. }
  108. static void start_working(void)
  109. {
  110. #ifdef DEBUG
  111. cerr << "Starting work\n";
  112. #endif
  113. wrkctrlstate.worker_thread_state = WT_RUNNING;
  114. if (pthread_create(&wrkctrlstate.worker_thread, NULL,
  115. worker_thread_start, NULL)) {
  116. wrkctrlstate.worker_thread = WT_NOT_RUNNING;
  117. cerr << "Could not start worker thread\n";
  118. }
  119. }
  120. static void dpconn_event_cb(struct bufferevent *bev, short events,
  121. void *ctx)
  122. {
  123. if (events & BEV_EVENT_CONNECTED) {
  124. // We have successfully connected to the dpnode
  125. #ifdef DEBUG
  126. cerr << "Connection established to dpnode " << bev << "\n";
  127. #endif
  128. bufferevent_enable(bev, EV_WRITE);
  129. ++wrkctrlstate.num_connected_dpnodes;
  130. if (wrkctrlstate.num_connected_dpnodes ==
  131. wrkctrlstate.num_expected_dpnodes) {
  132. start_working();
  133. }
  134. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  135. #ifdef DEBUG
  136. cerr << "Closing connection to dpnode " << bev << " ";
  137. if (events & BEV_EVENT_EOF) {
  138. cerr << "EOF";
  139. }
  140. if (events & BEV_EVENT_ERROR) {
  141. cerr << "ERR (" << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()) << ")";
  142. }
  143. cerr << "\n";
  144. #endif
  145. stop_working();
  146. }
  147. }
  148. static void controllerconn_reader(struct bufferevent *bev, void *ctx)
  149. {
  150. struct evbuffer *input = bufferevent_get_input(bev);
  151. WrkControllerConnInfo *info = (WrkControllerConnInfo *)ctx;
  152. unsigned char cmd[1];
  153. unsigned char subproblem[SUBPROBLEM_DESC_LEN];
  154. while(1) {
  155. size_t len = evbuffer_get_length(input);
  156. switch(info->state) {
  157. case WRKCCSTATE_AWAITCMD:
  158. if (len < 1) return;
  159. bufferevent_read(bev, cmd, 1);
  160. #ifdef DEBUG
  161. cerr << "Command " << cmd[0] << " received\n";
  162. #endif
  163. switch(cmd[0]) {
  164. case 'P':
  165. info->state = WRKCCSTATE_RDPROBLEM;
  166. break;
  167. case 'S':
  168. stop_working();
  169. break;
  170. default:
  171. /* Unknown command received */
  172. fprintf(stderr, "Unknown command in "
  173. "controllerconn_reader: %c\n", cmd[0]);
  174. info->state = WRKCCSTATE_END;
  175. break;
  176. }
  177. break;
  178. case WRKCCSTATE_RDPROBLEM:
  179. if (len < SUBPROBLEM_DESC_LEN+2) return;
  180. stop_working();
  181. bufferevent_read(bev, subproblem, SUBPROBLEM_DESC_LEN);
  182. bufferevent_read(bev, &(info->num_dpnodes), 2);
  183. // Careful! Subproblem uses NTL, so we must be sure
  184. // we're not multithreaded at this point.
  185. wrkctrlstate.current_problem = new Subproblem(subproblem);
  186. info->state = WRKCCSTATE_RDDPNODES;
  187. /* FALLTHROUGH */
  188. case WRKCCSTATE_RDDPNODES:
  189. if (len < 6*(info->num_dpnodes)) return;
  190. wrkctrlstate.num_expected_dpnodes = info->num_dpnodes;
  191. {
  192. unsigned short i;
  193. for(i=0;i<info->num_dpnodes;++i) {
  194. unsigned char ipport[6];
  195. bufferevent_read(bev, ipport, 6);
  196. #ifdef DEBUG
  197. cerr << "Connecting to DPnode " <<
  198. int(ipport[0]) << "." <<
  199. int(ipport[1]) << "." <<
  200. int(ipport[2]) << "." <<
  201. int(ipport[3]) << ":" <<
  202. ((ipport[4] << 8) + ipport[5]) <<
  203. "\n";
  204. #endif
  205. struct bufferevent *dpbev = client_create(
  206. bufferevent_get_base(bev), ipport,
  207. dpconn_event_cb, true);
  208. #ifdef DEBUG
  209. cerr << "Starting connection to dpnode " << dpbev << "\n";
  210. #endif
  211. if (dpbev) {
  212. wrkctrlstate.dpnodes.push_back(dpbev);
  213. } else {
  214. stop_working();
  215. }
  216. }
  217. }
  218. info->state = WRKCCSTATE_AWAITCMD;
  219. break;
  220. case WRKCCSTATE_END:
  221. // Shut down
  222. delete info;
  223. event_base_loopbreak(bufferevent_get_base(bev));
  224. #ifdef DEBUG
  225. cerr << "Closing connection to " << bev << "\n";
  226. #endif
  227. bufferevent_free(bev);
  228. return;
  229. }
  230. }
  231. }
  232. static void controllerconn_event_cb(struct bufferevent *bev, short events,
  233. void *ctx)
  234. {
  235. if (events & BEV_EVENT_CONNECTED) {
  236. // We have successfully connected to the controller
  237. char id[1] = { 'W' };
  238. bufferevent_enable(bev, EV_READ|EV_WRITE);
  239. bufferevent_write(bev, id, 1);
  240. bufferevent_setcb(bev, controllerconn_reader, NULL,
  241. controllerconn_event_cb, new WrkControllerConnInfo());
  242. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  243. fprintf(stderr, "Closing connection to controller and exiting\n");
  244. event_base_loopbreak(bufferevent_get_base(bev));
  245. #ifdef DEBUG
  246. cerr << "Closing connection to " << bev << "\n";
  247. #endif
  248. bufferevent_free(bev);
  249. }
  250. }
  251. int worker_main(const char *controller_host, unsigned short controller_port)
  252. {
  253. evthread_use_pthreads();
  254. signal(SIGPIPE, SIG_IGN);
  255. return controller_client(controller_host, controller_port,
  256. controllerconn_event_cb, true);
  257. }