starpu_fxt_mpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014,2016,2017 Université de Bordeaux
  4. * Copyright (C) 2011-2013,2017 Inria
  5. * Copyright (C) 2010-2012,2014-2018 CNRS
  6. * Copyright (C) 2017 Federal University of Rio Grande do Sul (UFRGS)
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #ifdef STARPU_USE_FXT
  22. #include "starpu_fxt.h"
  23. #ifdef STARPU_HAVE_POTI
  24. #include <poti.h>
  25. #define STARPU_POTI_STR_LEN 200
  26. #endif
  27. #define MAX_MPI_NODES 64
  28. LIST_TYPE(mpi_transfer,
  29. unsigned matched;
  30. int src;
  31. int dst;
  32. long mpi_tag;
  33. size_t size;
  34. float date;
  35. long jobid;
  36. double bandwidth;
  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[MAX_MPI_NODES] = {NULL};
  94. static struct mpi_transfer *mpi_recvs[MAX_MPI_NODES] = {NULL};
  95. /* number of available slots in the lists */
  96. unsigned mpi_sends_list_size[MAX_MPI_NODES] = {0};
  97. unsigned mpi_recvs_list_size[MAX_MPI_NODES] = {0};
  98. /* number of slots actually used in the list */
  99. unsigned mpi_sends_used[MAX_MPI_NODES] = {0};
  100. unsigned mpi_recvs_used[MAX_MPI_NODES] = {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. unsigned mpi_recvs_matched[MAX_MPI_NODES][MAX_MPI_NODES] = { {0} };
  105. unsigned mpi_sends_matched[MAX_MPI_NODES][MAX_MPI_NODES] = { {0} };
  106. void _starpu_fxt_mpi_add_send_transfer(int src, int dst STARPU_ATTRIBUTE_UNUSED, long mpi_tag, size_t size, float date, long jobid)
  107. {
  108. STARPU_ASSERT(src >= 0);
  109. if (src >= MAX_MPI_NODES)
  110. return;
  111. unsigned slot = mpi_sends_used[src]++;
  112. if (mpi_sends_used[src] > mpi_sends_list_size[src])
  113. {
  114. if (mpi_sends_list_size[src] > 0)
  115. {
  116. mpi_sends_list_size[src] *= 2;
  117. }
  118. else
  119. {
  120. mpi_sends_list_size[src] = 1;
  121. }
  122. _STARPU_REALLOC(mpi_sends[src], mpi_sends_list_size[src]*sizeof(struct mpi_transfer));
  123. }
  124. mpi_sends[src][slot].matched = 0;
  125. mpi_sends[src][slot].src = src;
  126. mpi_sends[src][slot].dst = dst;
  127. mpi_sends[src][slot].mpi_tag = mpi_tag;
  128. mpi_sends[src][slot].size = size;
  129. mpi_sends[src][slot].date = date;
  130. mpi_sends[src][slot].jobid = jobid;
  131. }
  132. void _starpu_fxt_mpi_add_recv_transfer(int src STARPU_ATTRIBUTE_UNUSED, int dst, long mpi_tag, float date, long jobid)
  133. {
  134. if (dst >= MAX_MPI_NODES)
  135. return;
  136. unsigned slot = mpi_recvs_used[dst]++;
  137. if (mpi_recvs_used[dst] > mpi_recvs_list_size[dst])
  138. {
  139. if (mpi_recvs_list_size[dst] > 0)
  140. {
  141. mpi_recvs_list_size[dst] *= 2;
  142. }
  143. else
  144. {
  145. mpi_recvs_list_size[dst] = 1;
  146. }
  147. _STARPU_REALLOC(mpi_recvs[dst], mpi_recvs_list_size[dst]*sizeof(struct mpi_transfer));
  148. }
  149. mpi_recvs[dst][slot].matched = 0;
  150. mpi_recvs[dst][slot].src = src;
  151. mpi_recvs[dst][slot].dst = dst;
  152. mpi_recvs[dst][slot].mpi_tag = mpi_tag;
  153. mpi_recvs[dst][slot].date = date;
  154. mpi_recvs[dst][slot].jobid = jobid;
  155. }
  156. static
  157. struct mpi_transfer *try_to_match_send_transfer(int src STARPU_ATTRIBUTE_UNUSED, int dst, long mpi_tag)
  158. {
  159. unsigned slot;
  160. unsigned firstslot = mpi_recvs_matched[src][dst];
  161. unsigned all_previous_were_matched = 1;
  162. for (slot = firstslot; slot < mpi_recvs_used[dst]; slot++)
  163. {
  164. if (!mpi_recvs[dst][slot].matched)
  165. {
  166. if (mpi_recvs[dst][slot].mpi_tag == mpi_tag)
  167. {
  168. /* we found a match ! */
  169. mpi_recvs[dst][slot].matched = 1;
  170. return &mpi_recvs[dst][slot];
  171. }
  172. all_previous_were_matched = 0;
  173. }
  174. else
  175. {
  176. if (all_previous_were_matched)
  177. {
  178. /* All previous transfers are already matched,
  179. * we need not consider them anymore */
  180. mpi_recvs_matched[src][dst] = slot;
  181. }
  182. }
  183. }
  184. /* If we reached that point, we could not find a match */
  185. return NULL;
  186. }
  187. static unsigned long mpi_com_id = 0;
  188. static void display_all_transfers_from_trace(FILE *out_paje_file, unsigned n)
  189. {
  190. unsigned slot[MAX_MPI_NODES] = { 0 }, node;
  191. struct mpi_transfer_list pending_receives; /* Sorted list of matches which have not happened yet */
  192. double current_out_bandwidth[MAX_MPI_NODES] = { 0. };
  193. double current_in_bandwidth[MAX_MPI_NODES] = { 0. };
  194. #ifdef STARPU_HAVE_POTI
  195. char mpi_container[STARPU_POTI_STR_LEN];
  196. #endif
  197. //bwi_mpi and bwo_mpi are set to zero when MPI thread containers are created
  198. mpi_transfer_list_init(&pending_receives);
  199. while (1)
  200. {
  201. float start_date;
  202. struct mpi_transfer *cur, *match;
  203. int src;
  204. /* Find out which event comes first: a pending receive, or a new send */
  205. if (mpi_transfer_list_empty(&pending_receives))
  206. start_date = INFINITY;
  207. else
  208. start_date = mpi_transfer_list_front(&pending_receives)->date;
  209. src = MAX_MPI_NODES;
  210. for (node = 0; node < n; node++)
  211. {
  212. if (slot[node] < mpi_sends_used[node] && mpi_sends[node][slot[node]].date < start_date)
  213. {
  214. /* next send for node is earlier than others */
  215. src = node;
  216. start_date = mpi_sends[src][slot[src]].date;
  217. }
  218. }
  219. if (start_date == INFINITY)
  220. /* No event any more, we're finished! */
  221. break;
  222. if (src == MAX_MPI_NODES)
  223. {
  224. /* Pending match is earlier than all new sends, finish its communication */
  225. match = mpi_transfer_list_pop_front(&pending_receives);
  226. current_out_bandwidth[match->src] -= match->bandwidth;
  227. current_in_bandwidth[match->dst] -= match->bandwidth;
  228. #ifdef STARPU_HAVE_POTI
  229. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", match->src);
  230. poti_SetVariable(match->date, mpi_container, "bwo_mpi", current_out_bandwidth[match->src]);
  231. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", match->dst);
  232. poti_SetVariable(match->date, mpi_container, "bwi_mpi", current_in_bandwidth[match->dst]);
  233. #else
  234. fprintf(out_paje_file, "13 %.9f %d_mpict bwo_mpi %f\n", match->date, match->src, current_out_bandwidth[match->src]);
  235. fprintf(out_paje_file, "13 %.9f %d_mpict bwi_mpi %f\n", match->date, match->dst, current_in_bandwidth[match->dst]);
  236. #endif
  237. continue;
  238. }
  239. cur = &mpi_sends[src][slot[src]];
  240. int dst = cur->dst;
  241. long mpi_tag = cur->mpi_tag;
  242. size_t size = cur->size;
  243. if (dst < MAX_MPI_NODES)
  244. match = try_to_match_send_transfer(src, dst, mpi_tag);
  245. else
  246. match = NULL;
  247. if (match)
  248. {
  249. float end_date = match->date;
  250. struct mpi_transfer *prev;
  251. match->bandwidth = (0.001*size)/(end_date - start_date);
  252. current_out_bandwidth[src] += match->bandwidth;
  253. current_in_bandwidth[dst] += match->bandwidth;
  254. /* Insert in sorted list, most probably at the end so let's use a mere insertion sort */
  255. for (prev = mpi_transfer_list_last(&pending_receives);
  256. prev != mpi_transfer_list_alpha(&pending_receives);
  257. prev = mpi_transfer_list_prev(prev))
  258. if (prev->date <= end_date)
  259. {
  260. /* Found its place */
  261. mpi_transfer_list_insert_after(&pending_receives, match, prev);
  262. break;
  263. }
  264. if (prev == mpi_transfer_list_alpha(&pending_receives))
  265. {
  266. /* No element earlier than this one, put it at the head */
  267. mpi_transfer_list_push_front(&pending_receives, match);
  268. }
  269. unsigned long id = mpi_com_id++;
  270. if (cur->jobid != -1)
  271. _starpu_fxt_dag_add_send(src, cur->jobid, mpi_tag, id);
  272. if (match->jobid != -1)
  273. _starpu_fxt_dag_add_receive(dst, match->jobid, mpi_tag, id);
  274. #ifdef STARPU_HAVE_POTI
  275. char paje_value[STARPU_POTI_STR_LEN], paje_key[STARPU_POTI_STR_LEN];
  276. snprintf(paje_value, sizeof(paje_value), "%lu", (long unsigned) size);
  277. snprintf(paje_key, sizeof(paje_key), "mpicom_%lu", id);
  278. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", src);
  279. char str_mpi_tag[STARPU_POTI_STR_LEN];
  280. snprintf(str_mpi_tag, sizeof(str_mpi_tag), "%ld", mpi_tag);
  281. poti_user_StartLink(_starpu_poti_MpiLinkStart, start_date, "MPIroot", "MPIL", mpi_container, paje_value, paje_key, 1, str_mpi_tag);
  282. poti_SetVariable(start_date, mpi_container, "bwo_mpi", current_out_bandwidth[src]);
  283. snprintf(mpi_container, sizeof(mpi_container), "%d_mpict", dst);
  284. poti_EndLink(end_date, "MPIroot", "MPIL", mpi_container, paje_value, paje_key);
  285. poti_SetVariable(start_date, mpi_container, "bwo_mpi", current_in_bandwidth[dst]);
  286. #else
  287. fprintf(out_paje_file, "13 %.9f %d_mpict bwo_mpi %f\n", start_date, src, current_out_bandwidth[src]);
  288. fprintf(out_paje_file, "13 %.9f %d_mpict bwi_mpi %f\n", start_date, dst, current_in_bandwidth[dst]);
  289. fprintf(out_paje_file, "23 %.9f MPIL MPIroot %lu %d_mpict mpicom_%lu %ld\n", start_date, (unsigned long)size, src, id, mpi_tag);
  290. fprintf(out_paje_file, "19 %.9f MPIL MPIroot %lu %d_mpict mpicom_%lu\n", end_date, (unsigned long)size, dst, id);
  291. #endif
  292. }
  293. else
  294. {
  295. _STARPU_DISP("Warning, could not match MPI transfer from %d to %d (tag %x) starting at %f\n", src, dst, mpi_tag, start_date);
  296. }
  297. slot[src]++;
  298. }
  299. }
  300. void _starpu_fxt_display_mpi_transfers(struct starpu_fxt_options *options, int *ranks STARPU_ATTRIBUTE_UNUSED, FILE *out_paje_file)
  301. {
  302. if (options->ninputfiles > MAX_MPI_NODES)
  303. {
  304. _STARPU_DISP("Warning: %u files given, maximum %u supported, truncating to %u\n", options->ninputfiles, MAX_MPI_NODES, MAX_MPI_NODES);
  305. options->ninputfiles = MAX_MPI_NODES;
  306. }
  307. /* display the MPI transfers if possible */
  308. if (out_paje_file)
  309. display_all_transfers_from_trace(out_paje_file, options->ninputfiles);
  310. }
  311. #endif // STARPU_USE_FXT