worker.cc 9.1 KB

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