starpu_fxt_mpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2017-2020 Federal University of Rio Grande do Sul (UFRGS)
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #ifdef STARPU_USE_FXT
  20. #include "starpu_fxt.h"
  21. #ifdef STARPU_HAVE_POTI
  22. #include <poti.h>
  23. #define STARPU_POTI_STR_LEN 200
  24. #endif
  25. LIST_TYPE(mpi_transfer,
  26. unsigned matched;
  27. int src;
  28. int dst;
  29. long mpi_tag;
  30. size_t size;
  31. float date;
  32. long jobid;
  33. double bandwidth;
  34. unsigned long handle;
  35. unsigned type;
  36. int prio;
  37. );
  38. /* Returns 0 if a barrier is found, -1 otherwise. In case of success, offset is
  39. * filled with the timestamp of the barrier */
  40. int _starpu_fxt_mpi_find_sync_point(char *filename_in, uint64_t *offset, int *key, int *rank)
  41. {
  42. STARPU_ASSERT(offset);
  43. /* Open the trace file */
  44. int fd_in;
  45. fd_in = open(filename_in, O_RDONLY);
  46. if (fd_in < 0)
  47. {
  48. perror("open failed :");
  49. exit(-1);
  50. }
  51. static fxt_t fut;
  52. fut = fxt_fdopen(fd_in);
  53. if (!fut)
  54. {
  55. perror("fxt_fdopen :");
  56. exit(-1);
  57. }
  58. fxt_blockev_t block;
  59. block = fxt_blockev_enter(fut);
  60. struct fxt_ev_64 ev;
  61. int func_ret = -1;
  62. unsigned found = 0;
  63. while(!found)
  64. {
  65. int ret = fxt_next_ev(block, FXT_EV_TYPE_64, (struct fxt_ev *)&ev);
  66. if (ret != FXT_EV_OK)
  67. {
  68. _STARPU_MSG("no more block ...\n");
  69. break;
  70. }
  71. if (ev.code == _STARPU_MPI_FUT_BARRIER)
  72. {
  73. /* We found the sync point */
  74. *offset = ev.time;
  75. *rank = ev.param[0];
  76. *key = ev.param[2];
  77. found = 1;
  78. func_ret = 0;
  79. }
  80. }
  81. /* Close the trace file */
  82. if (close(fd_in))
  83. {
  84. perror("close failed :");
  85. exit(-1);
  86. }
  87. return func_ret;
  88. }
  89. /*
  90. * Deal with the actual MPI transfers performed with the MPI lib
  91. */
  92. /* the list of MPI transfers found in the different traces */
  93. static struct mpi_transfer *mpi_sends[STARPU_FXT_MAX_FILES] = {NULL};
  94. static struct mpi_transfer *mpi_recvs[STARPU_FXT_MAX_FILES] = {NULL};
  95. /* number of available slots in the lists */
  96. static unsigned mpi_sends_list_size[STARPU_FXT_MAX_FILES] = {0};
  97. static unsigned mpi_recvs_list_size[STARPU_FXT_MAX_FILES] = {0};
  98. /* number of slots actually used in the list */
  99. static unsigned mpi_sends_used[STARPU_FXT_MAX_FILES] = {0};
  100. static unsigned mpi_recvs_used[STARPU_FXT_MAX_FILES] = {0};
  101. /* number of slots already matched at the beginning of the list. This permits
  102. * going through the lists from the beginning to match each and every
  103. * transfer, thus avoiding a quadratic complexity. */
  104. static unsigned mpi_recvs_matched[STARPU_FXT_MAX_FILES][STARPU_FXT_MAX_FILES] = { {0} };
  105. void _starpu_fxt_mpi_add_send_transfer(int src, int dst STARPU_ATTRIBUTE_UNUSED, long mpi_tag, size_t size, float date, long jobid, unsigned long handle, unsigned type, int prio)
  106. {
  107. STARPU_ASSERT(src >= 0);
  108. if (src >= STARPU_FXT_MAX_FILES)
  109. return;
  110. unsigned slot = mpi_sends_used[src]++;
  111. if (mpi_sends_used[src] > mpi_sends_list_size[src])
  112. {
  113. if (mpi_sends_list_size[src] > 0)
  114. {
  115. mpi_sends_list_size[src] *= 2;
  116. }
  117. else
  118. {
  119. mpi_sends_list_size[src] = 1;
  120. }
  121. _STARPU_REALLOC(mpi_sends[src], mpi_sends_list_size[src]*sizeof(struct mpi_transfer));
  122. }
  123. mpi_sends[src][slot].matched = 0;
  124. mpi_sends[src][slot].src = src;
  125. mpi_sends[src][slot].dst = dst;
  126. mpi_sends[src][slot].mpi_tag = mpi_tag;
  127. mpi_sends[src][slot].size = size;
  128. mpi_sends[src][slot].date = date;
  129. mpi_sends[src][slot].jobid = jobid;
  130. mpi_sends[src][slot].handle = handle;
  131. mpi_sends[src][slot].type = type;
  132. mpi_sends[src][slot].prio = prio;
  133. }
  134. void _starpu_fxt_mpi_add_recv_transfer(int src STARPU_ATTRIBUTE_UNUSED, int dst, long mpi_tag, float date, long jobid, unsigned long handle)
  135. {
  136. if (dst >= STARPU_FXT_MAX_FILES)
  137. return;
  138. unsigned slot = mpi_recvs_used[dst]++;
  139. if (mpi_recvs_used[dst] > mpi_recvs_list_size[dst])
  140. {
  141. if (mpi_recvs_list_size[dst] > 0)
  142. {
  143. mpi_recvs_list_size[dst] *= 2;
  144. }
  145. else
  146. {
  147. mpi_recvs_list_size[dst] = 1;
  148. }
  149. _STARPU_REALLOC(mpi_recvs[dst], mpi_recvs_list_size[dst]*sizeof(struct mpi_transfer));
  150. }
  151. mpi_recvs[dst][slot].matched = 0;
  152. mpi_recvs[dst][slot].src = src;
  153. mpi_recvs[dst][slot].dst = dst;
  154. mpi_recvs[dst][slot].mpi_tag = mpi_tag;
  155. mpi_recvs[dst][slot].date = date;
  156. mpi_recvs[dst][slot].jobid = jobid;
  157. mpi_recvs[dst][slot].handle = handle;
  158. }
  159. static
  160. struct mpi_transfer *try_to_match_send_transfer(int src STARPU_ATTRIBUTE_UNUSED, int dst, long mpi_tag)
  161. {
  162. unsigned slot;
  163. unsigned firstslot = mpi_recvs_matched[src][dst];
  164. unsigned all_previous_were_matched = 1;
  165. for (slot = firstslot; slot < mpi_recvs_used[dst]; slot++)
  166. {
  167. if (!mpi_recvs[dst][slot].matched)
  168. {
  169. if (mpi_recvs[dst][slot].mpi_tag == mpi_tag)
  170. {
  171. /* we found a match ! */
  172. mpi_recvs[dst][slot].matched = 1;
  173. return &mpi_recvs[dst][slot];
  174. }
  175. all_previous_were_matched = 0;
  176. }
  177. else
  178. {
  179. if (all_previous_were_matched)
  180. {
  181. /* All previous transfers are already matched,
  182. * we need not consider them anymore */
  183. mpi_recvs_matched[src][dst] = slot;
  184. }
  185. }
  186. }
  187. /* If we reached that point, we could not find a match */
  188. return NULL;
  189. }
  190. static unsigned long mpi_com_id = 0;
  191. static const char* get_mpi_type_str(unsigned mpi_type)
  192. {
  193. switch (mpi_type)
  194. {
  195. case _STARPU_MPI_FUT_POINT_TO_POINT_SEND:
  196. return "PointToPoint";
  197. case _STARPU_MPI_FUT_COLLECTIVE_SEND:
  198. return "Collective";
  199. default:
  200. return "Unknown";
  201. }
  202. }
  203. static void display_all_transfers_from_trace(FILE *out_paje_file, FILE *out_comms_file, unsigned n)
  204. {
  205. unsigned slot[STARPU_FXT_MAX_FILES] = { 0 }, node;
  206. unsigned nb_wrong_comm_timing = 0;
  207. struct mpi_transfer_list pending_receives; /* Sorted list of matches which have not happened yet */
  208. double current_out_bandwidth[STARPU_FXT_MAX_FILES] = { 0. };
  209. double current_in_bandwidth[STARPU_FXT_MAX_FILES] = { 0. };
  210. #ifdef STARPU_HAVE_POTI
  211. char mpi_container[STARPU_POTI_STR_LEN];
  212. #endif
  213. //bwi_mpi and bwo_mpi are set to zero when MPI thread containers are created
  214. mpi_transfer_list_init(&pending_receives);
  215. while (1)
  216. {
  217. float start_date;
  218. struct mpi_transfer *cur, *match;
  219. int src;
  220. /* Find out which event comes first: a pending receive, or a new send */
  221. if (mpi_transfer_list_empty(&pending_receives))
  222. start_date = INFINITY;
  223. else
  224. start_date = mpi_transfer_list_front(&pending_receives)->date;
  225. src = STARPU_FXT_MAX_FILES;
  226. for (node = 0; node < n; node++)
  227. {
  228. if (slot[node] < mpi_sends_used[node] && mpi_sends[node][slot[node]].date < start_date)
  229. {
  230. /* next send for node is earlier than others */
  231. src = node;
  232. start_date = mpi_sends[src][slot[src]].date;
  233. }
  234. }
  235. if (start_date == INFINITY)
  236. /* No event any more, we're finished! */
  237. break;
  238. if (src == STARPU_FXT_MAX_FILES)
  239. {
  240. /* Pending match is earlier than all new sends, finish its communication */
  241. match = mpi_transfer_list_pop_front(&pending_receives);
  242. current_out_bandwidth[match->src] -= match->bandwidth;
  243. current_in_bandwidth[match->dst] -= match->bandwidth;
  244. #ifdef STARPU_HAVE_POTI
  245. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", match->src);
  246. poti_SetVariable(match->date, mpi_container, "bwo_mpi", current_out_bandwidth[match->src]);
  247. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", match->dst);
  248. poti_SetVariable(match->date, mpi_container, "bwi_mpi", current_in_bandwidth[match->dst]);
  249. #else
  250. fprintf(out_paje_file, "13 %.9f %d_mpict bwo_mpi %f\n", match->date, match->src, current_out_bandwidth[match->src]);
  251. fprintf(out_paje_file, "13 %.9f %d_mpict bwi_mpi %f\n", match->date, match->dst, current_in_bandwidth[match->dst]);
  252. #endif
  253. continue;
  254. }
  255. cur = &mpi_sends[src][slot[src]];
  256. int dst = cur->dst;
  257. long mpi_tag = cur->mpi_tag;
  258. size_t size = cur->size;
  259. unsigned long send_handle = cur->handle;
  260. if (dst < STARPU_FXT_MAX_FILES)
  261. match = try_to_match_send_transfer(src, dst, mpi_tag);
  262. else
  263. match = NULL;
  264. if (match)
  265. {
  266. float end_date = match->date;
  267. unsigned long recv_handle = match->handle;
  268. struct mpi_transfer *prev;
  269. if (end_date <= start_date)
  270. nb_wrong_comm_timing++;
  271. match->bandwidth = (0.001*size)/(end_date - start_date);
  272. current_out_bandwidth[src] += match->bandwidth;
  273. current_in_bandwidth[dst] += match->bandwidth;
  274. /* Insert in sorted list, most probably at the end so let's use a mere insertion sort */
  275. for (prev = mpi_transfer_list_last(&pending_receives);
  276. prev != mpi_transfer_list_alpha(&pending_receives);
  277. prev = mpi_transfer_list_prev(prev))
  278. if (prev->date <= end_date)
  279. {
  280. /* Found its place */
  281. mpi_transfer_list_insert_after(&pending_receives, match, prev);
  282. break;
  283. }
  284. if (prev == mpi_transfer_list_alpha(&pending_receives))
  285. {
  286. /* No element earlier than this one, put it at the head */
  287. mpi_transfer_list_push_front(&pending_receives, match);
  288. }
  289. unsigned long id = mpi_com_id++;
  290. if (cur->jobid != -1)
  291. _starpu_fxt_dag_add_send(src, cur->jobid, mpi_tag, id);
  292. if (match->jobid != -1)
  293. _starpu_fxt_dag_add_receive(dst, match->jobid, mpi_tag, id);
  294. #ifdef STARPU_HAVE_POTI
  295. char paje_value[STARPU_POTI_STR_LEN], paje_key[STARPU_POTI_STR_LEN];
  296. snprintf(paje_value, sizeof(paje_value), "%lu", (long unsigned) size);
  297. snprintf(paje_key, sizeof(paje_key), "mpicom_%lu", id);
  298. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", src);
  299. char str_mpi_tag[STARPU_POTI_STR_LEN];
  300. snprintf(str_mpi_tag, sizeof(str_mpi_tag), "%ld", mpi_tag);
  301. char str_priority[STARPU_POTI_STR_LEN];
  302. snprintf(str_priority, sizeof(str_priority), "%d", cur->prio);
  303. char str_handle[STARPU_POTI_STR_LEN];
  304. snprintf(str_handle, sizeof(str_handle), "%lx", send_handle);
  305. poti_user_StartLink(_starpu_poti_MpiLinkStart, start_date, "MPIroot", "MPIL", mpi_container, paje_value, paje_key, 4, str_mpi_tag, get_mpi_type_str(cur->type), str_priority, str_handle);
  306. poti_SetVariable(start_date, mpi_container, "bwo_mpi", current_out_bandwidth[src]);
  307. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", dst);
  308. poti_EndLink(end_date, "MPIroot", "MPIL", mpi_container, paje_value, paje_key);
  309. poti_SetVariable(start_date, mpi_container, "bwo_mpi", current_in_bandwidth[dst]);
  310. #else
  311. fprintf(out_paje_file, "13 %.9f %d_mpict bwo_mpi %f\n", start_date, src, current_out_bandwidth[src]);
  312. fprintf(out_paje_file, "13 %.9f %d_mpict bwi_mpi %f\n", start_date, dst, current_in_bandwidth[dst]);
  313. fprintf(out_paje_file, "23 %.9f MPIL MPIroot %lu %d_mpict mpicom_%lu %ld %s %d %lx\n", start_date, (unsigned long)size, src, id, mpi_tag, get_mpi_type_str(cur->type), cur->prio, send_handle);
  314. fprintf(out_paje_file, "19 %.9f MPIL MPIroot %lu %d_mpict mpicom_%lu\n", end_date, (unsigned long)size, dst, id);
  315. #endif
  316. if (out_comms_file != NULL)
  317. {
  318. fprintf(out_comms_file, "Src: %d\n", src);
  319. fprintf(out_comms_file, "Dst: %d\n", dst);
  320. fprintf(out_comms_file, "Tag: %ld\n", mpi_tag);
  321. fprintf(out_comms_file, "SendTime: %.9f\n", start_date);
  322. fprintf(out_comms_file, "RecvTime: %.9f\n", end_date);
  323. fprintf(out_comms_file, "SendHandle: %lx\n", send_handle);
  324. fprintf(out_comms_file, "RecvHandle: %lx\n", recv_handle);
  325. if (cur->jobid != -1)
  326. fprintf(out_comms_file, "SendJobId: %d_%ld\n", src, cur->jobid);
  327. if (match->jobid != -1)
  328. fprintf(out_comms_file, "RecvJobId: %d_%ld\n", dst, match->jobid);
  329. fprintf(out_comms_file, "Size: %lu\n", (unsigned long)size);
  330. fprintf(out_comms_file, "Priority: %d\n", cur->prio);
  331. fprintf(out_comms_file, "Type: %s\n", get_mpi_type_str(cur->type));
  332. fprintf(out_comms_file, "\n");
  333. }
  334. }
  335. else
  336. {
  337. _STARPU_DISP("Warning, could not match MPI transfer from %d to %d (tag %lx) starting at %f\n", src, dst, mpi_tag, start_date);
  338. }
  339. slot[src]++;
  340. }
  341. if (nb_wrong_comm_timing == 1)
  342. _STARPU_MSG("Warning: a communication finished before it started !\n");
  343. else if (nb_wrong_comm_timing > 1)
  344. _STARPU_MSG("Warning: %u communications finished before they started !\n", nb_wrong_comm_timing);
  345. }
  346. void _starpu_fxt_display_mpi_transfers(struct starpu_fxt_options *options, int *ranks STARPU_ATTRIBUTE_UNUSED, FILE *out_paje_file, FILE* out_comms_file)
  347. {
  348. if (options->ninputfiles > STARPU_FXT_MAX_FILES)
  349. {
  350. _STARPU_DISP("Warning: %u files given, maximum %u supported, truncating to %u\n", options->ninputfiles, STARPU_FXT_MAX_FILES, STARPU_FXT_MAX_FILES);
  351. options->ninputfiles = STARPU_FXT_MAX_FILES;
  352. }
  353. /* display the MPI transfers if possible */
  354. if (out_paje_file)
  355. display_all_transfers_from_trace(out_paje_file, out_comms_file, options->ninputfiles);
  356. }
  357. #endif // STARPU_USE_FXT