worker.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #undef VERBOSE
  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 VERBOSE
  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 VERBOSE
  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 VERBOSE
  111. cerr << "Starting work\n";
  112. #endif
  113. cout << "Subproblem " << wrkctrlstate.current_problem->problemid << "\n";
  114. cout.flush();
  115. wrkctrlstate.worker_thread_state = WT_RUNNING;
  116. if (pthread_create(&wrkctrlstate.worker_thread, NULL,
  117. worker_thread_start, NULL)) {
  118. wrkctrlstate.worker_thread = WT_NOT_RUNNING;
  119. cerr << "Could not start worker thread\n";
  120. }
  121. }
  122. static void dpconn_event_cb(struct bufferevent *bev, short events,
  123. void *ctx)
  124. {
  125. if (events & BEV_EVENT_CONNECTED) {
  126. // We have successfully connected to the dpnode
  127. #ifdef VERBOSE
  128. cerr << "Connection established to dpnode " << bev << "\n";
  129. #endif
  130. bufferevent_enable(bev, EV_WRITE);
  131. ++wrkctrlstate.num_connected_dpnodes;
  132. if (wrkctrlstate.num_connected_dpnodes ==
  133. wrkctrlstate.num_expected_dpnodes) {
  134. start_working();
  135. }
  136. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  137. #ifdef VERBOSE
  138. cerr << "Closing connection to dpnode " << bev << " ";
  139. if (events & BEV_EVENT_EOF) {
  140. cerr << "EOF";
  141. }
  142. if (events & BEV_EVENT_ERROR) {
  143. cerr << "ERR (" << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()) << ")";
  144. }
  145. cerr << "\n";
  146. #endif
  147. stop_working();
  148. }
  149. }
  150. static void controllerconn_reader(struct bufferevent *bev, void *ctx)
  151. {
  152. struct evbuffer *input = bufferevent_get_input(bev);
  153. WrkControllerConnInfo *info = (WrkControllerConnInfo *)ctx;
  154. unsigned char cmd[1];
  155. unsigned char subproblem[SUBPROBLEM_DESC_LEN];
  156. while(1) {
  157. size_t len = evbuffer_get_length(input);
  158. switch(info->state) {
  159. case WRKCCSTATE_AWAITCMD:
  160. if (len < 1) return;
  161. bufferevent_read(bev, cmd, 1);
  162. #ifdef VERBOSE
  163. cerr << "Command " << cmd[0] << " received\n";
  164. #endif
  165. switch(cmd[0]) {
  166. case 'P':
  167. info->state = WRKCCSTATE_RDPROBLEM;
  168. break;
  169. case 'S':
  170. stop_working();
  171. break;
  172. default:
  173. /* Unknown command received */
  174. fprintf(stderr, "Unknown command in "
  175. "controllerconn_reader: %c\n", cmd[0]);
  176. info->state = WRKCCSTATE_END;
  177. break;
  178. }
  179. break;
  180. case WRKCCSTATE_RDPROBLEM:
  181. if (len < SUBPROBLEM_DESC_LEN+2) return;
  182. stop_working();
  183. bufferevent_read(bev, subproblem, SUBPROBLEM_DESC_LEN);
  184. bufferevent_read(bev, &(info->num_dpnodes), 2);
  185. // Careful! Subproblem uses NTL, so we must be sure
  186. // we're not multithreaded at this point.
  187. wrkctrlstate.current_problem = new Subproblem(subproblem);
  188. info->state = WRKCCSTATE_RDDPNODES;
  189. /* FALLTHROUGH */
  190. case WRKCCSTATE_RDDPNODES:
  191. if (len < 6*(info->num_dpnodes)) return;
  192. wrkctrlstate.num_expected_dpnodes = info->num_dpnodes;
  193. {
  194. unsigned short i;
  195. for(i=0;i<info->num_dpnodes;++i) {
  196. unsigned char ipport[6];
  197. bufferevent_read(bev, ipport, 6);
  198. #ifdef VERBOSE
  199. cerr << "Connecting to DPnode " <<
  200. int(ipport[0]) << "." <<
  201. int(ipport[1]) << "." <<
  202. int(ipport[2]) << "." <<
  203. int(ipport[3]) << ":" <<
  204. ((ipport[4] << 8) + ipport[5]) <<
  205. "\n";
  206. #endif
  207. struct bufferevent *dpbev = client_create(
  208. bufferevent_get_base(bev), ipport,
  209. dpconn_event_cb, true);
  210. #ifdef VERBOSE
  211. cerr << "Starting connection to dpnode " << dpbev << "\n";
  212. #endif
  213. if (dpbev) {
  214. wrkctrlstate.dpnodes.push_back(dpbev);
  215. } else {
  216. stop_working();
  217. }
  218. }
  219. }
  220. info->state = WRKCCSTATE_AWAITCMD;
  221. break;
  222. case WRKCCSTATE_END:
  223. // Shut down
  224. delete info;
  225. event_base_loopbreak(bufferevent_get_base(bev));
  226. #ifdef VERBOSE
  227. cerr << "Closing connection to " << bev << "\n";
  228. #endif
  229. bufferevent_free(bev);
  230. return;
  231. }
  232. }
  233. }
  234. static void controllerconn_event_cb(struct bufferevent *bev, short events,
  235. void *ctx)
  236. {
  237. if (events & BEV_EVENT_CONNECTED) {
  238. // We have successfully connected to the controller
  239. char id[1] = { 'W' };
  240. bufferevent_enable(bev, EV_READ|EV_WRITE);
  241. bufferevent_write(bev, id, 1);
  242. bufferevent_setcb(bev, controllerconn_reader, NULL,
  243. controllerconn_event_cb, new WrkControllerConnInfo());
  244. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  245. fprintf(stderr, "Closing connection to controller and exiting\n");
  246. event_base_loopbreak(bufferevent_get_base(bev));
  247. #ifdef VERBOSE
  248. cerr << "Closing connection to " << bev << "\n";
  249. #endif
  250. bufferevent_free(bev);
  251. }
  252. }
  253. int worker_main(const char *controller_host, unsigned short controller_port)
  254. {
  255. evthread_use_pthreads();
  256. signal(SIGPIPE, SIG_IGN);
  257. return controller_client(controller_host, controller_port,
  258. controllerconn_event_cb, true);
  259. }