worker.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. {
  81. #ifdef DERANDOMIZE
  82. RandomStreamPush push_seed;
  83. // the seed will be reset to its original value
  84. // once we exit this scope
  85. SetSeed(wrkctrlstate.current_problem->base*
  86. wrkctrlstate.current_problem->target*
  87. wrkctrlstate.current_problem->order*
  88. wrkctrlstate.current_problem->modulus);
  89. #endif
  90. cuda_dl(to_ZZ_p(wrkctrlstate.current_problem->base),
  91. to_ZZ_p(wrkctrlstate.current_problem->target),
  92. wrkctrlstate.current_problem->order,
  93. wrkctrlstate.current_problem->modulus,
  94. wrkctrlstate.current_problem->dpfreq, NULL,
  95. &wrkctrlstate.kernel_launch_count);
  96. }
  97. return NULL;
  98. }
  99. // ----- Above this line are the functions running in the worker thread.
  100. // Below are the functions running in the main (communication) thread.
  101. static void stop_working(void)
  102. {
  103. #ifdef VERBOSE
  104. cerr << "Stopping work\n";
  105. #endif
  106. if (wrkctrlstate.worker_thread_state != WT_NOT_RUNNING) {
  107. // Tell the worker thread to stop after its next kernel launch
  108. wrkctrlstate.worker_thread_state = WT_SHOULD_STOP;
  109. pthread_join(wrkctrlstate.worker_thread, NULL);
  110. wrkctrlstate.worker_thread_state = WT_NOT_RUNNING;
  111. }
  112. // Close the connections to the dpnodes
  113. vector<struct bufferevent *>::iterator bevit;
  114. for (bevit = wrkctrlstate.dpnodes.begin();
  115. bevit != wrkctrlstate.dpnodes.end(); ++bevit) {
  116. #ifdef VERBOSE
  117. cerr << "Closing connection to " << *bevit << "\n";
  118. #endif
  119. bufferevent_free(*bevit);
  120. }
  121. wrkctrlstate.dpnodes.clear();
  122. wrkctrlstate.num_connected_dpnodes = 0;
  123. wrkctrlstate.num_expected_dpnodes = 0;
  124. // Careful! Subproblem uses NTL, so we must be sure we're not
  125. // multithreaded at this point.
  126. delete wrkctrlstate.current_problem;
  127. wrkctrlstate.current_problem = NULL;
  128. }
  129. static void start_working(void)
  130. {
  131. #ifdef VERBOSE
  132. cerr << "Starting work\n";
  133. #endif
  134. struct timeval now;
  135. gettimeofday(&now, NULL);
  136. char timestamp[20];
  137. sprintf(timestamp, "%d.%06d", (int)now.tv_sec, (int)now.tv_usec);
  138. cout << timestamp << ":" << output_prefix << ": Subproblem " << wrkctrlstate.current_problem->problemid << "\n";
  139. cout.flush();
  140. wrkctrlstate.kernel_launch_count = 0;
  141. wrkctrlstate.worker_thread_state = WT_RUNNING;
  142. if (pthread_create(&wrkctrlstate.worker_thread, NULL,
  143. worker_thread_start, NULL)) {
  144. wrkctrlstate.worker_thread = WT_NOT_RUNNING;
  145. cerr << "Could not start worker thread\n";
  146. }
  147. }
  148. static void dpconn_event_cb(struct bufferevent *bev, short events,
  149. void *ctx)
  150. {
  151. if (events & BEV_EVENT_CONNECTED) {
  152. // We have successfully connected to the dpnode
  153. #ifdef VERBOSE
  154. cerr << "Connection established to dpnode " << bev << "\n";
  155. #endif
  156. bufferevent_enable(bev, EV_WRITE);
  157. ++wrkctrlstate.num_connected_dpnodes;
  158. if (wrkctrlstate.num_connected_dpnodes ==
  159. wrkctrlstate.num_expected_dpnodes) {
  160. start_working();
  161. }
  162. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  163. #ifdef VERBOSE
  164. cerr << "Closing connection to dpnode " << bev << " ";
  165. if (events & BEV_EVENT_EOF) {
  166. cerr << "EOF";
  167. }
  168. if (events & BEV_EVENT_ERROR) {
  169. cerr << "ERR (" << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()) << ")";
  170. }
  171. cerr << "\n";
  172. #endif
  173. stop_working();
  174. }
  175. }
  176. static void controllerconn_reader(struct bufferevent *bev, void *ctx)
  177. {
  178. struct evbuffer *input = bufferevent_get_input(bev);
  179. WrkControllerConnInfo *info = (WrkControllerConnInfo *)ctx;
  180. unsigned char cmd[1];
  181. unsigned char subproblem[SUBPROBLEM_DESC_LEN];
  182. while(1) {
  183. size_t len = evbuffer_get_length(input);
  184. switch(info->state) {
  185. case WRKCCSTATE_AWAITCMD:
  186. if (len < 1) return;
  187. bufferevent_read(bev, cmd, 1);
  188. #ifdef VERBOSE
  189. cerr << "Command " << cmd[0] << " received\n";
  190. #endif
  191. switch(cmd[0]) {
  192. case 'P':
  193. info->state = WRKCCSTATE_RDPROBLEM;
  194. break;
  195. case 'S':
  196. stop_working();
  197. bufferevent_write(bev, &(wrkctrlstate.kernel_launch_count), sizeof(wrkctrlstate.kernel_launch_count));
  198. cout << "Launch count: " << wrkctrlstate.kernel_launch_count << "\n";
  199. cout.flush();
  200. break;
  201. default:
  202. /* Unknown command received */
  203. fprintf(stderr, "Unknown command in "
  204. "controllerconn_reader: %c\n", cmd[0]);
  205. info->state = WRKCCSTATE_END;
  206. break;
  207. }
  208. break;
  209. case WRKCCSTATE_RDPROBLEM:
  210. if (len < SUBPROBLEM_DESC_LEN+2) return;
  211. stop_working();
  212. bufferevent_read(bev, subproblem, SUBPROBLEM_DESC_LEN);
  213. bufferevent_read(bev, &(info->num_dpnodes), 2);
  214. // Careful! Subproblem uses NTL, so we must be sure
  215. // we're not multithreaded at this point.
  216. wrkctrlstate.current_problem = new Subproblem(subproblem);
  217. info->state = WRKCCSTATE_RDDPNODES;
  218. /* FALLTHROUGH */
  219. case WRKCCSTATE_RDDPNODES:
  220. if (len < 6*(info->num_dpnodes)) return;
  221. wrkctrlstate.num_expected_dpnodes = info->num_dpnodes;
  222. {
  223. unsigned short i;
  224. for(i=0;i<info->num_dpnodes;++i) {
  225. unsigned char ipport[6];
  226. bufferevent_read(bev, ipport, 6);
  227. #ifdef VERBOSE
  228. cerr << "Connecting to DPnode " <<
  229. int(ipport[0]) << "." <<
  230. int(ipport[1]) << "." <<
  231. int(ipport[2]) << "." <<
  232. int(ipport[3]) << ":" <<
  233. ((ipport[4] << 8) + ipport[5]) <<
  234. "\n";
  235. #endif
  236. struct bufferevent *dpbev = client_create(
  237. bufferevent_get_base(bev), ipport,
  238. dpconn_event_cb, true);
  239. #ifdef VERBOSE
  240. cerr << "Starting connection to dpnode " << dpbev << "\n";
  241. #endif
  242. if (dpbev) {
  243. wrkctrlstate.dpnodes.push_back(dpbev);
  244. } else {
  245. stop_working();
  246. }
  247. }
  248. }
  249. info->state = WRKCCSTATE_AWAITCMD;
  250. break;
  251. case WRKCCSTATE_END:
  252. // Shut down
  253. delete info;
  254. event_base_loopbreak(bufferevent_get_base(bev));
  255. #ifdef VERBOSE
  256. cerr << "Closing connection to " << bev << "\n";
  257. #endif
  258. bufferevent_free(bev);
  259. return;
  260. }
  261. }
  262. }
  263. static void controllerconn_event_cb(struct bufferevent *bev, short events,
  264. void *ctx)
  265. {
  266. if (events & BEV_EVENT_CONNECTED) {
  267. // We have successfully connected to the controller
  268. char id[1] = { 'W' };
  269. bufferevent_enable(bev, EV_READ|EV_WRITE);
  270. bufferevent_write(bev, id, 1);
  271. bufferevent_setcb(bev, controllerconn_reader, NULL,
  272. controllerconn_event_cb, new WrkControllerConnInfo());
  273. } else if (events & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
  274. fprintf(stderr, "Closing connection to controller and exiting\n");
  275. event_base_loopbreak(bufferevent_get_base(bev));
  276. #ifdef VERBOSE
  277. cerr << "Closing connection to " << bev << "\n";
  278. #endif
  279. bufferevent_free(bev);
  280. }
  281. }
  282. int worker_main(const char *controller_host, unsigned short controller_port, int gpu_id)
  283. {
  284. // Initialize the prng with some randomness from the kernel
  285. unsigned char randbuf[1024];
  286. ifstream urand("/dev/urandom");
  287. urand.read((char *)randbuf, sizeof(randbuf));
  288. urand.close();
  289. ZZ randzz = ZZFromBytes(randbuf, sizeof(randbuf));
  290. SetSeed(randzz);
  291. evthread_use_pthreads();
  292. signal(SIGPIPE, SIG_IGN);
  293. cuda_device_id = gpu_id;
  294. return controller_client(controller_host, controller_port,
  295. controllerconn_event_cb, true);
  296. }