worker.cc 8.8 KB

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