worker.cc 9.8 KB

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