worker.cc 8.5 KB

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