starpu_mpi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <starpu_mpi.h>
  18. #include <starpu_mpi_datatype.h>
  19. #include <starpu_mpi_private.h>
  20. /* TODO find a better way to select the polling method (perhaps during the
  21. * configuration) */
  22. //#define USE_STARPU_ACTIVITY 1
  23. static void submit_mpi_req(void *arg);
  24. static void handle_request_termination(struct starpu_mpi_req_s *req);
  25. /* The list of requests that have been newly submitted by the application */
  26. static starpu_mpi_req_list_t new_requests;
  27. /* The list of detached requests that have already been submitted to MPI */
  28. static starpu_mpi_req_list_t detached_requests;
  29. static pthread_mutex_t detached_requests_mutex;
  30. static pthread_cond_t cond;
  31. static pthread_mutex_t mutex;
  32. static pthread_t progress_thread;
  33. static int running = 0;
  34. /*
  35. * Isend
  36. */
  37. static void starpu_mpi_isend_func(struct starpu_mpi_req_s *req)
  38. {
  39. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  40. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  41. MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  42. /* somebody is perhaps waiting for the MPI request to be posted */
  43. pthread_mutex_lock(&req->req_mutex);
  44. req->submitted = 1;
  45. pthread_cond_broadcast(&req->req_cond);
  46. pthread_mutex_unlock(&req->req_mutex);
  47. }
  48. int starpu_mpi_isend(starpu_data_handle data_handle, starpu_mpi_req *public_req, int dest, int mpi_tag, MPI_Comm comm)
  49. {
  50. STARPU_ASSERT(public_req);
  51. struct starpu_mpi_req_s *req = starpu_mpi_req_new();
  52. STARPU_ASSERT(req);
  53. *public_req = req;
  54. memset(req, 0, sizeof(struct starpu_mpi_req_s));
  55. /* Initialize the request structure */
  56. req->submitted = 0;
  57. req->completed = 0;
  58. pthread_mutex_init(&req->req_mutex, NULL);
  59. pthread_cond_init(&req->req_cond, NULL);
  60. req->data_handle = data_handle;
  61. req->srcdst = dest;
  62. req->mpi_tag = mpi_tag;
  63. req->comm = comm;
  64. req->detached = 0;
  65. req->func = starpu_mpi_isend_func;
  66. /* Asynchronously request StarPU to fetch the data in main memory: when
  67. * it is available in main memory, submit_mpi_req(req) is called and
  68. * the request is actually submitted */
  69. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_R,
  70. submit_mpi_req, (void *)req);
  71. return 0;
  72. }
  73. /*
  74. * Isend (detached)
  75. */
  76. int starpu_mpi_isend_detached(starpu_data_handle data_handle,
  77. int dest, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  78. {
  79. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  80. STARPU_ASSERT(req);
  81. /* Initialize the request structure */
  82. req->submitted = 0;
  83. req->completed = 0;
  84. pthread_mutex_init(&req->req_mutex, NULL);
  85. pthread_cond_init(&req->req_cond, NULL);
  86. req->data_handle = data_handle;
  87. req->srcdst = dest;
  88. req->mpi_tag = mpi_tag;
  89. req->comm = comm;
  90. req->func = starpu_mpi_isend_func;
  91. req->detached = 1;
  92. req->callback = callback;
  93. req->callback_arg = arg;
  94. /* Asynchronously request StarPU to fetch the data in main memory: when
  95. * it is available in main memory, submit_mpi_req(req) is called and
  96. * the request is actually submitted */
  97. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_R,
  98. submit_mpi_req, (void *)req);
  99. return 0;
  100. }
  101. /*
  102. * Irecv
  103. */
  104. static void starpu_mpi_irecv_func(struct starpu_mpi_req_s *req)
  105. {
  106. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  107. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  108. MPI_Irecv(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  109. /* somebody is perhaps waiting for the MPI request to be posted */
  110. pthread_mutex_lock(&req->req_mutex);
  111. req->submitted = 1;
  112. pthread_cond_broadcast(&req->req_cond);
  113. pthread_mutex_unlock(&req->req_mutex);
  114. }
  115. int starpu_mpi_irecv(starpu_data_handle data_handle, starpu_mpi_req *public_req, int source, int mpi_tag, MPI_Comm comm)
  116. {
  117. STARPU_ASSERT(public_req);
  118. struct starpu_mpi_req_s *req = starpu_mpi_req_new();
  119. STARPU_ASSERT(req);
  120. *public_req = req;
  121. STARPU_ASSERT(req);
  122. memset(req, 0, sizeof(struct starpu_mpi_req_s));
  123. /* Initialize the request structure */
  124. req->submitted = 0;
  125. pthread_mutex_init(&req->req_mutex, NULL);
  126. pthread_cond_init(&req->req_cond, NULL);
  127. req->data_handle = data_handle;
  128. req->srcdst = source;
  129. req->mpi_tag = mpi_tag;
  130. req->comm = comm;
  131. req->detached = 0;
  132. req->func = starpu_mpi_irecv_func;
  133. /* Asynchronously request StarPU to fetch the data in main memory: when
  134. * it is available in main memory, submit_mpi_req(req) is called and
  135. * the request is actually submitted */
  136. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_W,
  137. submit_mpi_req, (void *)req);
  138. return 0;
  139. }
  140. /*
  141. * Irecv (detached)
  142. */
  143. int starpu_mpi_irecv_detached(starpu_data_handle data_handle, int source, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  144. {
  145. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  146. STARPU_ASSERT(req);
  147. /* Initialize the request structure */
  148. req->submitted = 0;
  149. pthread_mutex_init(&req->req_mutex, NULL);
  150. pthread_cond_init(&req->req_cond, NULL);
  151. req->data_handle = data_handle;
  152. req->srcdst = source;
  153. req->mpi_tag = mpi_tag;
  154. req->comm = comm;
  155. req->detached = 1;
  156. req->callback = callback;
  157. req->callback_arg = arg;
  158. req->func = starpu_mpi_irecv_func;
  159. /* Asynchronously request StarPU to fetch the data in main memory: when
  160. * it is available in main memory, submit_mpi_req(req) is called and
  161. * the request is actually submitted */
  162. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_W,
  163. submit_mpi_req, (void *)req);
  164. return 0;
  165. }
  166. /*
  167. * Recv
  168. */
  169. int starpu_mpi_recv(starpu_data_handle data_handle,
  170. int source, int mpi_tag, MPI_Comm comm, MPI_Status *status)
  171. {
  172. starpu_mpi_req req;
  173. starpu_mpi_irecv(data_handle, &req, source, mpi_tag, comm);
  174. starpu_mpi_wait(&req, status);
  175. return 0;
  176. }
  177. /*
  178. * Send
  179. */
  180. int starpu_mpi_send(starpu_data_handle data_handle,
  181. int dest, int mpi_tag, MPI_Comm comm)
  182. {
  183. starpu_mpi_req req;
  184. MPI_Status status;
  185. memset(&status, 0, sizeof(MPI_Status));
  186. starpu_mpi_isend(data_handle, &req, dest, mpi_tag, comm);
  187. starpu_mpi_wait(&req, &status);
  188. return 0;
  189. }
  190. /*
  191. * Wait
  192. */
  193. static void starpu_mpi_wait_func(struct starpu_mpi_req_s *waiting_req)
  194. {
  195. /* Which is the mpi request we are waiting for ? */
  196. struct starpu_mpi_req_s *req = waiting_req->other_request;
  197. req->ret = MPI_Wait(&req->request, waiting_req->status);
  198. handle_request_termination(req);
  199. }
  200. int starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  201. {
  202. int ret;
  203. struct starpu_mpi_req_s waiting_req;
  204. memset(&waiting_req, 0, sizeof(struct starpu_mpi_req_s));
  205. struct starpu_mpi_req_s *req = *public_req;
  206. /* We cannot try to complete a MPI request that was not actually posted
  207. * to MPI yet. */
  208. pthread_mutex_lock(&req->req_mutex);
  209. while (!req->submitted)
  210. pthread_cond_wait(&req->req_cond, &req->req_mutex);
  211. pthread_mutex_unlock(&req->req_mutex);
  212. /* Initialize the request structure */
  213. pthread_mutex_init(&waiting_req.req_mutex, NULL);
  214. pthread_cond_init(&waiting_req.req_cond, NULL);
  215. waiting_req.status = status;
  216. waiting_req.other_request = req;
  217. waiting_req.func = starpu_mpi_wait_func;
  218. submit_mpi_req(&waiting_req);
  219. /* We wait for the MPI request to finish */
  220. pthread_mutex_lock(&req->req_mutex);
  221. while (!req->completed)
  222. pthread_cond_wait(&req->req_cond, &req->req_mutex);
  223. pthread_mutex_unlock(&req->req_mutex);
  224. ret = req->ret;
  225. /* The internal request structure was automatically allocated */
  226. *public_req = NULL;
  227. free(req);
  228. return ret;
  229. }
  230. /*
  231. * Test
  232. */
  233. static void starpu_mpi_test_func(struct starpu_mpi_req_s *testing_req)
  234. {
  235. /* Which is the mpi request we are testing for ? */
  236. struct starpu_mpi_req_s *req = testing_req->other_request;
  237. int ret = MPI_Test(&req->request, testing_req->flag, testing_req->status);
  238. if (*testing_req->flag)
  239. {
  240. testing_req->ret = ret;
  241. handle_request_termination(req);
  242. }
  243. pthread_mutex_lock(&testing_req->req_mutex);
  244. testing_req->completed = 1;
  245. pthread_cond_signal(&testing_req->req_cond);
  246. pthread_mutex_unlock(&testing_req->req_mutex);
  247. }
  248. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  249. {
  250. int ret = 0;
  251. STARPU_ASSERT(public_req);
  252. struct starpu_mpi_req_s *req = *public_req;
  253. STARPU_ASSERT(!req->detached);
  254. pthread_mutex_lock(&req->req_mutex);
  255. unsigned submitted = req->submitted;
  256. pthread_mutex_unlock(&req->req_mutex);
  257. if (submitted)
  258. {
  259. struct starpu_mpi_req_s testing_req;
  260. memset(&testing_req, 0, sizeof(struct starpu_mpi_req_s));
  261. /* Initialize the request structure */
  262. pthread_mutex_init(&testing_req.req_mutex, NULL);
  263. pthread_cond_init(&testing_req.req_cond, NULL);
  264. testing_req.flag = flag;
  265. testing_req.status = status;
  266. testing_req.other_request = req;
  267. testing_req.func = starpu_mpi_test_func;
  268. testing_req.completed = 0;
  269. submit_mpi_req(&testing_req);
  270. /* We wait for the test request to finish */
  271. pthread_mutex_lock(&testing_req.req_mutex);
  272. while (!testing_req.completed)
  273. pthread_cond_wait(&testing_req.req_cond, &testing_req.req_mutex);
  274. pthread_mutex_unlock(&testing_req.req_mutex);
  275. ret = testing_req.ret;
  276. if (*testing_req.flag)
  277. {
  278. /* The request was completed so we liberate the
  279. * internal request structure which was automatically
  280. * allocated */
  281. *public_req = NULL;
  282. free(req);
  283. }
  284. }
  285. else {
  286. *flag = 0;
  287. }
  288. return ret;
  289. }
  290. /*
  291. * Requests
  292. */
  293. static void handle_request_termination(struct starpu_mpi_req_s *req)
  294. {
  295. MPI_Type_free(&req->datatype);
  296. starpu_release_data_from_mem(req->data_handle);
  297. /* Execute the specified callback, if any */
  298. if (req->callback)
  299. req->callback(req->callback_arg);
  300. /* tell anyone potentiallly waiting on the request that it is
  301. * terminated now */
  302. pthread_mutex_lock(&req->req_mutex);
  303. req->completed = 1;
  304. pthread_cond_broadcast(&req->req_cond);
  305. pthread_mutex_unlock(&req->req_mutex);
  306. }
  307. static void submit_mpi_req(void *arg)
  308. {
  309. struct starpu_mpi_req_s *req = arg;
  310. pthread_mutex_lock(&mutex);
  311. starpu_mpi_req_list_push_front(new_requests, req);
  312. pthread_cond_broadcast(&cond);
  313. pthread_mutex_unlock(&mutex);
  314. }
  315. /*
  316. * Scheduler hook
  317. */
  318. static unsigned progression_hook_func(void *arg __attribute__((unused)))
  319. {
  320. unsigned may_block = 1;
  321. pthread_mutex_lock(&mutex);
  322. if (!starpu_mpi_req_list_empty(detached_requests))
  323. {
  324. pthread_cond_signal(&cond);
  325. may_block = 0;
  326. }
  327. pthread_mutex_unlock(&mutex);
  328. return may_block;
  329. }
  330. /*
  331. * Progression loop
  332. */
  333. static void test_detached_requests(void)
  334. {
  335. int flag;
  336. MPI_Status status;
  337. struct starpu_mpi_req_s *req, *next_req;
  338. pthread_mutex_lock(&detached_requests_mutex);
  339. for (req = starpu_mpi_req_list_begin(detached_requests);
  340. req != starpu_mpi_req_list_end(detached_requests);
  341. req = next_req)
  342. {
  343. next_req = starpu_mpi_req_list_next(req);
  344. pthread_mutex_unlock(&detached_requests_mutex);
  345. MPI_Test(&req->request, &flag, &status);
  346. if (flag)
  347. handle_request_termination(req);
  348. pthread_mutex_lock(&detached_requests_mutex);
  349. if (flag)
  350. starpu_mpi_req_list_erase(detached_requests, req);
  351. #warning TODO fix memleak
  352. /* Detached requests are automatically allocated by the lib */
  353. // if (req->detached)
  354. // free(req);
  355. }
  356. pthread_mutex_unlock(&detached_requests_mutex);
  357. }
  358. static void handle_new_request(struct starpu_mpi_req_s *req)
  359. {
  360. STARPU_ASSERT(req);
  361. /* submit the request to MPI */
  362. req->func(req);
  363. if (req->detached)
  364. {
  365. pthread_mutex_lock(&mutex);
  366. starpu_mpi_req_list_push_front(detached_requests, req);
  367. pthread_mutex_unlock(&mutex);
  368. starpu_wake_all_blocked_workers();
  369. /* put the submitted request into the list of pending requests
  370. * so that it can be handled by the progression mechanisms */
  371. pthread_mutex_lock(&mutex);
  372. pthread_cond_signal(&cond);
  373. pthread_mutex_unlock(&mutex);
  374. }
  375. }
  376. static void *progress_thread_func(void *arg __attribute__((unused)))
  377. {
  378. /* notify the main thread that the progression thread is ready */
  379. pthread_mutex_lock(&mutex);
  380. running = 1;
  381. pthread_cond_signal(&cond);
  382. pthread_mutex_unlock(&mutex);
  383. pthread_mutex_lock(&mutex);
  384. while (running) {
  385. /* shall we block ? */
  386. unsigned block = starpu_mpi_req_list_empty(new_requests);
  387. #ifndef USE_STARPU_ACTIVITY
  388. block = block && starpu_mpi_req_list_empty(detached_requests);
  389. #endif
  390. if (block)
  391. pthread_cond_wait(&cond, &mutex);
  392. if (!running)
  393. break;
  394. /* test whether there are some terminated "detached request" */
  395. pthread_mutex_unlock(&mutex);
  396. test_detached_requests();
  397. pthread_mutex_lock(&mutex);
  398. /* get one request */
  399. struct starpu_mpi_req_s *req;
  400. while (!starpu_mpi_req_list_empty(new_requests))
  401. {
  402. req = starpu_mpi_req_list_pop_back(new_requests);
  403. /* handling a request is likely to block for a while
  404. * (on a sync_data_with_mem call), we want to let the
  405. * application submit requests in the meantime, so we
  406. * release the lock. */
  407. pthread_mutex_unlock(&mutex);
  408. handle_new_request(req);
  409. pthread_mutex_lock(&mutex);
  410. }
  411. }
  412. pthread_mutex_unlock(&mutex);
  413. return NULL;
  414. }
  415. /*
  416. * (De)Initialization methods
  417. */
  418. #ifdef USE_STARPU_ACTIVITY
  419. static int hookid = - 1;
  420. #endif
  421. static void _starpu_mpi_add_sync_point_in_fxt(void)
  422. {
  423. #ifdef USE_FXT
  424. int rank;
  425. int worldsize;
  426. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  427. MPI_Comm_size(MPI_COMM_WORLD, &worldsize);
  428. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  429. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  430. /* We generate a "unique" key so that we can make sure that different
  431. * FxT traces come from the same MPI run. */
  432. int random_number;
  433. /* XXX perhaps we don't want to generate a new seed if the application
  434. * specified some reproductible behaviour ? */
  435. if (rank == 0)
  436. {
  437. srand(time(NULL));
  438. random_number = rand();
  439. }
  440. MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  441. TRACE_MPI_BARRIER(rank, worldsize, random_number);
  442. #ifdef VERBOSE
  443. fprintf(stderr, "StarPU MPI (rank %d): unique key %x\n", rank, random_number);
  444. #endif
  445. #endif
  446. }
  447. int starpu_mpi_initialize(void)
  448. {
  449. pthread_mutex_init(&mutex, NULL);
  450. pthread_cond_init(&cond, NULL);
  451. new_requests = starpu_mpi_req_list_new();
  452. pthread_mutex_init(&detached_requests_mutex, NULL);
  453. detached_requests = starpu_mpi_req_list_new();
  454. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, NULL);
  455. pthread_mutex_lock(&mutex);
  456. while (!running)
  457. pthread_cond_wait(&cond, &mutex);
  458. pthread_mutex_unlock(&mutex);
  459. #ifdef USE_STARPU_ACTIVITY
  460. hookid = starpu_register_progression_hook(progression_hook_func, NULL);
  461. STARPU_ASSERT(hookid >= 0);
  462. #endif
  463. _starpu_mpi_add_sync_point_in_fxt();
  464. return 0;
  465. }
  466. int starpu_mpi_shutdown(void)
  467. {
  468. void *value;
  469. /* kill the progression thread */
  470. pthread_mutex_lock(&mutex);
  471. running = 0;
  472. pthread_cond_broadcast(&cond);
  473. pthread_mutex_unlock(&mutex);
  474. pthread_join(progress_thread, &value);
  475. #ifdef USE_STARPU_ACTIVITY
  476. starpu_deregister_progression_hook(hookid);
  477. #endif
  478. /* liberate the request queues */
  479. starpu_mpi_req_list_delete(detached_requests);
  480. starpu_mpi_req_list_delete(new_requests);
  481. return 0;
  482. }