starpu_replay.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 Erwan Leria
  4. * Copyright (C) 2018 Inria
  5. * Copyright (C) 2017,2018,2019 CNRS
  6. * Copyright (C) 2016-2019 Université de Bordeaux
  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. /*
  20. * This reads a tasks.rec file and replays the recorded task graph.
  21. * Currently, this version is done to run with simgrid.
  22. *
  23. * For further information, contact erwan.leria@inria.fr
  24. */
  25. #include <starpu.h>
  26. #include <unistd.h>
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <common/uthash.h>
  30. #include <common/utils.h>
  31. #include <starpu_scheduler.h>
  32. #include <common/rbtree.h>
  33. #define REPLAY_NMAX_DEPENDENCIES 8
  34. #define ARRAY_DUP(in, out, n) memcpy(out, in, n * sizeof(*out))
  35. #define ARRAY_INIT(array, n) memset(array, 0, n * sizeof(*array))
  36. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  37. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  38. * Declarations of global variables, structures, pointers, ... *
  39. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  40. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  41. static int static_workerid;
  42. /* TODO: move to core header while moving starpu_replay_sched to core */
  43. extern void schedRecInit(const char * filename);
  44. extern void applySchedRec(struct starpu_task * starpu_task, long submit_order);
  45. /* Enum for normal and "wontuse" tasks */
  46. enum task_type {NormalTask, WontUseTask};
  47. typedef unsigned long jobid_t;
  48. enum task_type control;
  49. static char *name = NULL;
  50. static char *model = NULL;
  51. static jobid_t jobid;
  52. static jobid_t *dependson;
  53. static long submitorder = -1;
  54. static starpu_tag_t tag;
  55. static int workerid;
  56. static uint32_t footprint;
  57. static double flops, total_flops = 0.;
  58. static double startTime; //start time (The instant when the task starts)
  59. static double endTime; //end time (The instant when the task ends)
  60. static int iteration = -1;
  61. static starpu_data_handle_t handles[STARPU_NMAXBUFS];
  62. static enum starpu_data_access_mode modes[STARPU_NMAXBUFS];
  63. static char normal_reg_signal[STARPU_NMAXBUFS];
  64. /* Use the following arrays when the number of data is greater than STARPU_NMAXBUFS */
  65. starpu_data_handle_t * handles_ptr;
  66. enum starpu_data_access_mode * modes_ptr;
  67. size_t * sizes_set;
  68. static size_t dependson_size;
  69. static size_t ndependson;
  70. static unsigned nb_parameters = 0; /* Number of parameters */
  71. static int alloc_mode; /* If alloc_mode value is 1, then the handles are stored in dyn_handles, else they are in handles */
  72. static int priority = 0;
  73. char * reg_signal = NULL; /* The register signal (0 or 1 coded on 8 bit) is used to know which handle of the task has to be registered in StarPU (in fact to avoid handle twice)*/
  74. /* Record all tasks, hashed by jobid. */
  75. static struct task
  76. {
  77. struct starpu_rbtree_node node;
  78. UT_hash_handle hh;
  79. jobid_t jobid;
  80. int iteration;
  81. long submit_order;
  82. jobid_t *deps;
  83. size_t ndependson;
  84. struct starpu_task task;
  85. enum task_type type;
  86. int reg_signal;
  87. } *tasks;
  88. /* Record handles */
  89. static struct handle
  90. {
  91. UT_hash_handle hh;
  92. starpu_data_handle_t mem_ptr; /* This value should be the registered handle */
  93. starpu_data_handle_t handle; /* The key is the original value of the handle in the file */
  94. } * handles_hash;
  95. /* Record models */
  96. static struct perfmodel
  97. {
  98. UT_hash_handle hh;
  99. struct starpu_perfmodel perfmodel;
  100. char * model_name;
  101. } * model_hash;
  102. /*
  103. * Replay data interface
  104. * We don't care about many things anyway, essentially only sizes.
  105. */
  106. struct replay_interface
  107. {
  108. enum starpu_data_interface_id id;
  109. starpu_data_handle_t orig_handle;
  110. size_t size;
  111. size_t alloc_size;
  112. size_t max_size;
  113. };
  114. static struct starpu_data_interface_ops replay_interface_ops;
  115. static void register_replay(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  116. {
  117. (void) home_node;
  118. struct replay_interface *replay_interface = data_interface;
  119. unsigned node;
  120. for (node = 0; node < STARPU_MAXNODES; node++)
  121. {
  122. struct replay_interface *local_interface =
  123. starpu_data_get_interface_on_node(handle, node);
  124. local_interface->id = replay_interface->id;
  125. local_interface->orig_handle = replay_interface->orig_handle;
  126. local_interface->size = replay_interface->size;
  127. local_interface->alloc_size = replay_interface->alloc_size;
  128. local_interface->max_size = replay_interface->max_size;
  129. }
  130. }
  131. static void replay_data_register(starpu_data_handle_t *handleptr, starpu_data_handle_t orig_handle, int home_node, size_t size, size_t alloc_size, size_t max_size)
  132. {
  133. if (replay_interface_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  134. {
  135. replay_interface_ops.interfaceid = starpu_data_interface_get_next_id();
  136. }
  137. struct replay_interface interface = {
  138. .id = replay_interface_ops.interfaceid,
  139. .orig_handle = orig_handle,
  140. .size = size,
  141. .alloc_size = alloc_size,
  142. .max_size = max_size,
  143. };
  144. starpu_data_register(handleptr, home_node, &interface, &replay_interface_ops);
  145. }
  146. static size_t replay_get_size(starpu_data_handle_t handle)
  147. {
  148. struct replay_interface *interface =
  149. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  150. return interface->size;
  151. }
  152. static size_t replay_get_alloc_size(starpu_data_handle_t handle)
  153. {
  154. struct replay_interface *interface =
  155. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  156. return interface->alloc_size;
  157. }
  158. static size_t replay_get_max_size(starpu_data_handle_t handle)
  159. {
  160. struct replay_interface *interface =
  161. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  162. return interface->max_size;
  163. }
  164. static uint32_t replay_footprint(starpu_data_handle_t handle)
  165. {
  166. return starpu_hash_crc32c_be(replay_get_size(handle), 0);
  167. }
  168. static int replay_compare(void *data_interface_a, void *data_interface_b)
  169. {
  170. struct replay_interface *replay_a = data_interface_a;
  171. struct replay_interface *replay_b = data_interface_b;
  172. /* Two variables are considered compatible if they have the same size */
  173. return replay_a->size == replay_b->size;
  174. }
  175. static void display_replay(starpu_data_handle_t handle, FILE *f)
  176. {
  177. struct replay_interface *replay_interface =
  178. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  179. fprintf(f, "%lu/%lu/%lu\t",
  180. (unsigned long) replay_interface->size,
  181. (unsigned long) replay_interface->alloc_size,
  182. (unsigned long) replay_interface->max_size);
  183. }
  184. static starpu_ssize_t describe_replay(void *data_interface, char *buf, size_t size)
  185. {
  186. struct replay_interface *replay_interface = data_interface;
  187. return snprintf(buf, size, "r%lu/%lu/%lu\t",
  188. (unsigned long) replay_interface->size,
  189. (unsigned long) replay_interface->alloc_size,
  190. (unsigned long) replay_interface->max_size);
  191. }
  192. static starpu_ssize_t allocate_replay_on_node(void *data_interface, unsigned dst_node)
  193. {
  194. struct replay_interface *replay_interface = data_interface;
  195. starpu_memory_allocate(dst_node, replay_interface->alloc_size, STARPU_MEMORY_OVERFLOW);
  196. return 0;
  197. }
  198. static void free_replay_on_node(void *data_interface, unsigned dst_node)
  199. {
  200. struct replay_interface *replay_interface = data_interface;
  201. starpu_memory_deallocate(dst_node, replay_interface->alloc_size);
  202. }
  203. static int replay_copy(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  204. {
  205. (void) dst_interface;
  206. struct replay_interface *src = src_interface;
  207. /* We don't care about pointers */
  208. return starpu_interface_copy(1, 0, src_node, 1, 0, dst_node, src->size, async_data);
  209. }
  210. static const struct starpu_data_copy_methods replay_copy_data_methods =
  211. {
  212. .any_to_any = replay_copy,
  213. };
  214. static struct starpu_data_interface_ops replay_interface_ops =
  215. {
  216. .register_data_handle = register_replay,
  217. .allocate_data_on_node = allocate_replay_on_node,
  218. .free_data_on_node = free_replay_on_node,
  219. .copy_methods = &replay_copy_data_methods,
  220. .get_size = replay_get_size,
  221. .get_alloc_size = replay_get_alloc_size,
  222. .get_max_size = replay_get_max_size,
  223. .footprint = replay_footprint,
  224. .compare = replay_compare,
  225. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  226. .interface_size = sizeof(struct replay_interface),
  227. .display = display_replay,
  228. .pack_data = NULL,
  229. .unpack_data = NULL,
  230. .describe = describe_replay,
  231. /* We want to observe actual allocations/deallocations */
  232. .dontcache = 1,
  233. };
  234. /* [SUBMITORDER] The tree of the submit order */
  235. static struct starpu_rbtree tree = STARPU_RBTREE_INITIALIZER;
  236. /* the cmp_fn arg for rb_tree_insert() */
  237. unsigned int diff(struct starpu_rbtree_node * left_elm, struct starpu_rbtree_node * right_elm)
  238. {
  239. long oleft = ((struct task *) left_elm)->submit_order;
  240. long oright = ((struct task *) right_elm)->submit_order;
  241. if (oleft == -1 && oright == -1)
  242. {
  243. if (left_elm < right_elm)
  244. return -1;
  245. else
  246. return 1;
  247. }
  248. return oleft - oright;
  249. }
  250. /* Settings for the perfmodel */
  251. struct task_arg
  252. {
  253. uint32_t footprint;
  254. unsigned narch;
  255. double perf[];
  256. };
  257. uint32_t get_footprint(struct starpu_task * task)
  258. {
  259. return ((struct task_arg*) (task->cl_arg))->footprint;
  260. }
  261. double arch_cost_function(struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned nimpl)
  262. {
  263. int device = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  264. STARPU_ASSERT(device != -1);
  265. (void) nimpl;
  266. /* Then, get the pointer to the value of the expected time */
  267. struct task_arg *arg = task->cl_arg;
  268. if (device < (int) arg->narch)
  269. {
  270. double val = arg->perf[device];
  271. if (!(val == 0 || isnan(val)))
  272. return val;
  273. }
  274. fprintf(stderr, "[starpu] Error, expected_time is 0 or lower (replay.c line : %d)", __LINE__- 6);
  275. return 0.0;
  276. }
  277. /* End of settings */
  278. static unsigned long nexecuted_tasks;
  279. void dumb_kernel(void *buffers[], void *args) {
  280. (void) buffers;
  281. (void) args;
  282. nexecuted_tasks++;
  283. if (!(nexecuted_tasks % 1000))
  284. {
  285. fprintf(stderr, "\rExecuted task %lu...", nexecuted_tasks);
  286. fflush(stdout);
  287. }
  288. unsigned this_worker = starpu_worker_get_id_check();
  289. struct starpu_perfmodel_arch *perf_arch = starpu_worker_get_perf_archtype(this_worker, STARPU_NMAX_SCHED_CTXS);
  290. struct starpu_task *task = starpu_task_get_current();
  291. unsigned impl = starpu_task_get_implementation(task);
  292. double length = starpu_task_expected_length(task, perf_arch, impl);
  293. STARPU_ASSERT_MSG(!_STARPU_IS_ZERO(length) && !isnan(length),
  294. "Codelet %s does not have a perfmodel, or is not calibrated enough, please re-run in non-simgrid mode until it is calibrated",
  295. starpu_task_get_name(task));
  296. starpu_sleep(length / 1000000);
  297. }
  298. /* [CODELET] Initialization of an unique codelet for all the tasks*/
  299. static int can_execute(unsigned worker_id, struct starpu_task *task, unsigned nimpl)
  300. {
  301. struct starpu_perfmodel_arch * arch = starpu_worker_get_perf_archtype(worker_id, STARPU_NMAX_SCHED_CTXS);
  302. int device = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  303. if (device == -1)
  304. /* Doesn't exist yet, thus unknown, assuming it can not work there. */
  305. return 0;
  306. (void) nimpl;
  307. /* Then, get the pointer to the value of the expected time */
  308. struct task_arg *arg = task->cl_arg;
  309. if (device < (int) arg->narch)
  310. {
  311. double val = arg->perf[device];
  312. if (!(val == 0 || isnan(val)))
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. static struct starpu_perfmodel myperfmodel =
  318. {
  319. .type = STARPU_PER_ARCH,
  320. .arch_cost_function = arch_cost_function,
  321. .footprint = get_footprint,
  322. };
  323. static struct starpu_codelet cl =
  324. {
  325. .cpu_funcs = { dumb_kernel },
  326. .cpu_funcs_name = { "dumb_kernel" },
  327. .cuda_funcs = { dumb_kernel },
  328. .opencl_funcs = { dumb_kernel },
  329. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  330. .can_execute = can_execute,
  331. .model = &myperfmodel,
  332. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  333. };
  334. /* * * * * * * * * * * * * *
  335. * * * * * Functions * * * * *
  336. * * * * * * * * * * * * * * */
  337. /* The following function checks if the program has to use static or dynamic arrays*/
  338. static int set_alloc_mode(int total_parameters)
  339. {
  340. return total_parameters <= STARPU_NMAXBUFS;
  341. }
  342. /* According to the allocation mode, modify handles_ptr and modes_ptr in static or dynamic */
  343. static void arrays_managing(int mode)
  344. {
  345. if (mode)
  346. {
  347. handles_ptr = &handles[0];
  348. modes_ptr = &modes[0];
  349. reg_signal = &normal_reg_signal[0];
  350. }
  351. else
  352. {
  353. _STARPU_MALLOC(handles_ptr, sizeof(*handles_ptr) * nb_parameters);
  354. _STARPU_MALLOC(modes_ptr, sizeof(*modes_ptr) * nb_parameters);
  355. _STARPU_CALLOC(reg_signal, nb_parameters, sizeof(char *));
  356. }
  357. }
  358. /* Check if a handle hasn't been registered yet */
  359. static void variable_data_register_check(size_t * array_of_size, int nb_handles)
  360. {
  361. int h, i;
  362. starpu_data_handle_t orig_handles[nb_handles];
  363. ARRAY_DUP(handles_ptr, orig_handles, nb_handles);
  364. for (h = 0 ; h < nb_handles ; h++)
  365. {
  366. if(reg_signal[h]) /* Get the register signal, if it's 1 do ... */
  367. {
  368. struct handle * handles_cell;
  369. for (i = 0; i < h; i++)
  370. {
  371. /* Maybe we just registered it in this very h loop */
  372. if (handles_ptr[h] == orig_handles[i])
  373. {
  374. handles_ptr[h] = handles_ptr[i];
  375. break;
  376. }
  377. }
  378. if (i == h)
  379. {
  380. _STARPU_MALLOC(handles_cell, sizeof(*handles_cell));
  381. STARPU_ASSERT(handles_cell != NULL);
  382. handles_cell->handle = handles_ptr[h]; /* Get the hidden key (initial handle from the file) to store it as a key*/
  383. replay_data_register(handles_ptr+h, handles_ptr[h],
  384. modes_ptr[h] & STARPU_R ? STARPU_MAIN_RAM : -1,
  385. array_of_size[h], array_of_size[h], array_of_size[h]);
  386. handles_cell->mem_ptr = handles_ptr[h]; /* Store the new value of the handle into the hash table */
  387. HASH_ADD(hh, handles_hash, handle, sizeof(handles_ptr[h]), handles_cell);
  388. }
  389. }
  390. }
  391. }
  392. void reset(void)
  393. {
  394. control = NormalTask;
  395. if (name != NULL)
  396. {
  397. free(name);
  398. name = NULL;
  399. }
  400. if (model != NULL)
  401. {
  402. free(model);
  403. model = NULL;
  404. }
  405. if (sizes_set != NULL)
  406. {
  407. free(sizes_set);
  408. sizes_set = NULL;
  409. }
  410. if (reg_signal != NULL)
  411. {
  412. if (!alloc_mode)
  413. {
  414. free(reg_signal);
  415. reg_signal = NULL;
  416. }
  417. else
  418. {
  419. ARRAY_INIT(reg_signal, nb_parameters);
  420. }
  421. }
  422. jobid = 0;
  423. ndependson = 0;
  424. tag = -1;
  425. workerid = -1;
  426. footprint = 0;
  427. startTime = 0.0;
  428. endTime = 0.0;
  429. if (submitorder != -1)
  430. submitorder = -1;
  431. iteration = -1;
  432. nb_parameters = 0;
  433. alloc_mode = 1;
  434. }
  435. void fix_wontuse_handle(struct task * wontuseTask)
  436. {
  437. STARPU_ASSERT(wontuseTask);
  438. if (!wontuseTask->reg_signal)
  439. /* Data was already registered when we created this task, so it's already a handle */
  440. return;
  441. struct handle *handle_tmp;
  442. /* Data was not registered when we created this task, so this is the application pointer, look it up now */
  443. HASH_FIND(hh, handles_hash, &wontuseTask->task.handles[0], sizeof(wontuseTask->task.handles[0]), handle_tmp);
  444. if (handle_tmp)
  445. wontuseTask->task.handles[0] = handle_tmp->mem_ptr;
  446. else
  447. /* This data wasn't actually used, don't care about it */
  448. wontuseTask->task.handles[0] = NULL;
  449. }
  450. /* Function that submits all the tasks (used when the program reaches EOF) */
  451. int submit_tasks(void)
  452. {
  453. /* Add dependencies */
  454. const struct starpu_rbtree * tmptree = &tree;
  455. struct starpu_rbtree_node * currentNode = starpu_rbtree_first(tmptree);
  456. long last_submitorder = 0;
  457. while (currentNode != NULL)
  458. {
  459. struct task * currentTask = (struct task *) currentNode;
  460. if (currentTask->type == NormalTask)
  461. {
  462. if (currentTask->submit_order != -1)
  463. {
  464. STARPU_ASSERT(currentTask->submit_order >= last_submitorder + 1);
  465. while (currentTask->submit_order > last_submitorder + 1)
  466. {
  467. /* Oops, some tasks were not submitted by original application, fake some */
  468. struct starpu_task *task = starpu_task_create();
  469. int ret;
  470. task->cl = NULL;
  471. ret = starpu_task_submit(task);
  472. STARPU_ASSERT(ret == 0);
  473. last_submitorder++;
  474. }
  475. }
  476. if (currentTask->ndependson > 0)
  477. {
  478. struct starpu_task * taskdeps[currentTask->ndependson];
  479. unsigned i, j = 0;
  480. for (i = 0; i < currentTask->ndependson; i++)
  481. {
  482. struct task * taskdep;
  483. /* Get the ith jobid of deps_jobid */
  484. HASH_FIND(hh, tasks, &currentTask->deps[i], sizeof(jobid), taskdep);
  485. if(taskdep)
  486. {
  487. taskdeps[j] = &taskdep->task;
  488. j ++;
  489. }
  490. }
  491. starpu_task_declare_deps_array(&currentTask->task, j, taskdeps);
  492. }
  493. if (!(currentTask->iteration == -1))
  494. starpu_iteration_push(currentTask->iteration);
  495. applySchedRec(&currentTask->task, currentTask->submit_order);
  496. int ret_val = starpu_task_submit(&currentTask->task);
  497. if (!(currentTask->iteration == -1))
  498. starpu_iteration_pop();
  499. if (ret_val != 0)
  500. {
  501. fprintf(stderr, "\nWhile submitting task %ld (%s): return %d\n",
  502. currentTask->submit_order,
  503. currentTask->task.name? currentTask->task.name : "unknown",
  504. ret_val);
  505. return -1;
  506. }
  507. //fprintf(stderr, "submitting task %s (%lu, %llu)\n", currentTask->task.name?currentTask->task.name:"anonymous", currentTask->jobid, (unsigned long long) currentTask->task.tag_id);
  508. if (!(currentTask->submit_order % 1000))
  509. {
  510. fprintf(stderr, "\rSubmitted task order %ld...", currentTask->submit_order);
  511. fflush(stdout);
  512. }
  513. if (currentTask->submit_order != -1)
  514. last_submitorder++;
  515. }
  516. else
  517. {
  518. fix_wontuse_handle(currentTask); /* Add the handle in the wontuse task */
  519. /* FIXME: can not actually work properly since we have
  520. * disabled sequential consistency, so we don't have any
  521. * easy way to make this wait for the last task that
  522. * wrote to the handle. */
  523. if (currentTask->task.handles[0])
  524. starpu_data_wont_use(currentTask->task.handles[0]);
  525. }
  526. currentNode = starpu_rbtree_next(currentNode);
  527. }
  528. fprintf(stderr, " done.\n");
  529. return 1;
  530. }
  531. /* * * * * * * * * * * * * * * */
  532. /* * * * * * MAIN * * * * * * */
  533. /* * * * * * * * * * * * * * */
  534. static void usage(const char *program)
  535. {
  536. fprintf(stderr,"Usage: %s [--static-workerid] tasks.rec [sched.rec]\n", program);
  537. exit(EXIT_FAILURE);
  538. }
  539. int main(int argc, char **argv)
  540. {
  541. starpu_data_set_default_sequential_consistency_flag(0);
  542. FILE *rec;
  543. char *s;
  544. const char *tasks_rec = NULL;
  545. const char *sched_rec = NULL;
  546. unsigned i;
  547. size_t s_allocated = 128;
  548. unsigned long nread_tasks = 0;
  549. _STARPU_MALLOC(s, s_allocated);
  550. dependson_size = REPLAY_NMAX_DEPENDENCIES; /* Change the value of REPLAY_NMAX_DEPENCIES to modify the number of dependencies */
  551. _STARPU_MALLOC(dependson, dependson_size * sizeof (* dependson));
  552. alloc_mode = 1;
  553. for (i = 1; i < (unsigned) argc; i++)
  554. {
  555. if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
  556. {
  557. usage(argv[0]);
  558. }
  559. else if (!strcmp(argv[i], "--static-workerid"))
  560. {
  561. static_workerid = 1;
  562. }
  563. else
  564. {
  565. if (!tasks_rec)
  566. tasks_rec = argv[i];
  567. else if (!sched_rec)
  568. sched_rec = argv[i];
  569. else
  570. usage(argv[0]);
  571. }
  572. }
  573. if (!tasks_rec)
  574. usage(argv[0]);
  575. if (sched_rec)
  576. schedRecInit(sched_rec);
  577. rec = fopen(tasks_rec, "r");
  578. if (!rec)
  579. {
  580. fprintf(stderr,"unable to open file %s: %s\n", tasks_rec, strerror(errno));
  581. exit(EXIT_FAILURE);
  582. }
  583. int ret = starpu_init(NULL);
  584. if (ret == -ENODEV) goto enodev;
  585. /* Read line by line, and on empty line submit the task with the accumulated information */
  586. reset();
  587. double start = starpu_timing_now();
  588. int linenum = 0;
  589. while(1)
  590. {
  591. char *ln;
  592. if (!fgets(s, s_allocated, rec))
  593. {
  594. fprintf(stderr, " done.\n");
  595. int submitted = submit_tasks();
  596. if (submitted == -1)
  597. {
  598. goto enodev;
  599. }
  600. goto eof;
  601. }
  602. while (!(ln = strchr(s, '\n')))
  603. {
  604. /* fprintf(stderr,"buffer size %d too small, doubling it\n", s_allocated); */
  605. _STARPU_REALLOC(s, s_allocated * 2);
  606. if (!fgets(s + s_allocated-1, s_allocated+1, rec))
  607. {
  608. fprintf(stderr, "\n");
  609. int submitted = submit_tasks();
  610. if (submitted == -1)
  611. {
  612. goto enodev;
  613. }
  614. goto eof;
  615. }
  616. s_allocated *= 2;
  617. }
  618. linenum++;
  619. if (ln == s)
  620. {
  621. /* Empty line, do task */
  622. struct task * task;
  623. _STARPU_MALLOC(task, sizeof(*task));
  624. starpu_task_init(&task->task);
  625. task->deps = NULL;
  626. task->submit_order = submitorder;
  627. starpu_rbtree_node_init(&task->node);
  628. starpu_rbtree_insert(&tree, &task->node, diff);
  629. task->jobid = jobid;
  630. task->iteration = iteration;
  631. if (name != NULL)
  632. task->task.name = strdup(name);
  633. task->type = control;
  634. if (control == NormalTask)
  635. {
  636. if (workerid >= 0)
  637. {
  638. task->task.priority = priority;
  639. task->task.cl = &cl;
  640. if (static_workerid)
  641. {
  642. task->task.workerid = workerid;
  643. task->task.execute_on_a_specific_worker = 1;
  644. }
  645. if (alloc_mode)
  646. {
  647. /* Duplicating the handles stored (and registered in the current context) into the task */
  648. ARRAY_DUP(modes_ptr, task->task.modes, nb_parameters);
  649. ARRAY_DUP(modes_ptr, task->task.cl->modes, nb_parameters);
  650. variable_data_register_check(sizes_set, nb_parameters);
  651. ARRAY_DUP(handles_ptr, task->task.handles, nb_parameters);
  652. }
  653. else
  654. {
  655. task->task.dyn_modes = modes_ptr;
  656. _STARPU_MALLOC(task->task.cl->dyn_modes, (sizeof(*task->task.cl->dyn_modes) * nb_parameters));
  657. ARRAY_DUP(modes_ptr, task->task.cl->dyn_modes, nb_parameters);
  658. variable_data_register_check(sizes_set, nb_parameters);
  659. task->task.dyn_handles = handles_ptr;
  660. }
  661. task->task.nbuffers = nb_parameters;
  662. struct perfmodel * realmodel;
  663. HASH_FIND_STR(model_hash, model, realmodel);
  664. if (realmodel == NULL)
  665. {
  666. int len = strlen(model);
  667. _STARPU_CALLOC(realmodel, 1, sizeof(struct perfmodel));
  668. _STARPU_MALLOC(realmodel->model_name, sizeof(char) * (len+1));
  669. realmodel->model_name = strcpy(realmodel->model_name, model);
  670. starpu_perfmodel_init(&realmodel->perfmodel);
  671. int error = starpu_perfmodel_load_symbol(model, &realmodel->perfmodel);
  672. if (!error)
  673. {
  674. HASH_ADD_STR(model_hash, model_name, realmodel);
  675. }
  676. else
  677. {
  678. fprintf(stderr, "[starpu][Warning] Error loading perfmodel symbol %s\n", model);
  679. fprintf(stderr, "[starpu][Warning] Taking only measurements from the given execution, and forcing execution on worker %d\n", workerid);
  680. starpu_perfmodel_unload_model(&realmodel->perfmodel);
  681. free(realmodel->model_name);
  682. free(realmodel);
  683. realmodel = NULL;
  684. }
  685. }
  686. struct starpu_perfmodel_arch *arch = starpu_worker_get_perf_archtype(workerid, 0);
  687. unsigned comb = starpu_perfmodel_arch_comb_add(arch->ndevices, arch->devices);
  688. unsigned narch = starpu_perfmodel_get_narch_combs();
  689. struct task_arg *arg;
  690. _STARPU_MALLOC(arg, sizeof(struct task_arg) + sizeof(double) * narch);
  691. arg->footprint = footprint;
  692. arg->narch = narch;
  693. double * perfTime = arg->perf;
  694. if (realmodel == NULL)
  695. {
  696. /* Erf, do without perfmodel, for execution there */
  697. task->task.workerid = workerid;
  698. task->task.execute_on_a_specific_worker = 1;
  699. for (i = 0; i < narch ; i++)
  700. {
  701. if (i == comb)
  702. perfTime[i] = endTime - startTime;
  703. else
  704. perfTime[i] = NAN;
  705. }
  706. }
  707. else
  708. {
  709. int one = 0;
  710. for (i = 0; i < narch ; i++)
  711. {
  712. arch = starpu_perfmodel_arch_comb_fetch(i);
  713. perfTime[i] = starpu_perfmodel_history_based_expected_perf(&realmodel->perfmodel, arch, footprint);
  714. if (!(perfTime[i] == 0 || isnan(perfTime[i])))
  715. one = 1;
  716. }
  717. if (!one)
  718. {
  719. fprintf(stderr, "We do not have any performance measurement for symbol '%s' for footprint %x, we can not execute this", model, footprint);
  720. exit(EXIT_FAILURE);
  721. }
  722. }
  723. task->task.cl_arg = arg;
  724. task->task.flops = flops;
  725. total_flops += flops;
  726. }
  727. task->task.cl_arg_size = 0;
  728. task->task.tag_id = tag;
  729. task->task.use_tag = 1;
  730. task->ndependson = ndependson;
  731. if (ndependson > 0)
  732. {
  733. _STARPU_MALLOC(task->deps, ndependson * sizeof (* task->deps));
  734. ARRAY_DUP(dependson, task->deps, ndependson);
  735. }
  736. }
  737. else
  738. {
  739. STARPU_ASSERT(nb_parameters == 1);
  740. task->reg_signal = reg_signal[0];
  741. ARRAY_DUP(handles_ptr, task->task.handles, nb_parameters);
  742. }
  743. /* Add this task to task hash */
  744. HASH_ADD(hh, tasks, jobid, sizeof(jobid), task);
  745. nread_tasks++;
  746. if (!(nread_tasks % 1000))
  747. {
  748. fprintf(stderr, "\rRead task %lu...", nread_tasks);
  749. fflush(stdout);
  750. }
  751. reset();
  752. }
  753. /* Record various information */
  754. #define TEST(field) (!strncmp(s, field": ", strlen(field) + 2))
  755. else if(TEST("Control"))
  756. {
  757. char * c = s+9;
  758. if(!strncmp(c, "WontUse", 7))
  759. {
  760. control = WontUseTask;
  761. nb_parameters = 1;
  762. alloc_mode = set_alloc_mode(nb_parameters);
  763. arrays_managing(alloc_mode);
  764. }
  765. else
  766. control = NormalTask;
  767. }
  768. else if (TEST("Name"))
  769. {
  770. *ln = 0;
  771. name = strdup(s+6);
  772. }
  773. else if (TEST("Model"))
  774. {
  775. *ln = 0;
  776. model = strdup(s+7);
  777. }
  778. else if (TEST("JobId"))
  779. jobid = atol(s+7);
  780. else if(TEST("SubmitOrder"))
  781. submitorder = atoi(s+13);
  782. else if (TEST("DependsOn"))
  783. {
  784. char *c = s + 11;
  785. for (ndependson = 0; *c != '\n'; ndependson++)
  786. {
  787. if (ndependson >= dependson_size)
  788. {
  789. dependson_size *= 2;
  790. _STARPU_REALLOC(dependson, dependson_size * sizeof(*dependson));
  791. }
  792. dependson[ndependson] = strtol(c, &c, 10);
  793. }
  794. }
  795. else if (TEST("Tag"))
  796. {
  797. tag = strtol(s+5, NULL, 16);
  798. }
  799. else if (TEST("WorkerId"))
  800. {
  801. workerid = atoi(s+10);
  802. }
  803. else if (TEST("Footprint"))
  804. {
  805. footprint = strtoul(s+11, NULL, 16);
  806. }
  807. else if (TEST("Parameters"))
  808. {
  809. /* Parameters line format is PARAM1 PARAM2 (...)PARAMi (...)PARAMn */
  810. char * param_str = s + 12;
  811. int count = 0;
  812. for (i = 0 ; param_str[i] != '\n'; i++)
  813. {
  814. if (param_str[i] == ' ') /* Checking the number of ' ' (space), assuming that the file is not corrupted */
  815. {
  816. count++;
  817. }
  818. }
  819. nb_parameters = count + 1; /* There is one space per paramater except for the last one, that's why we have to add +1 (dirty programming) */
  820. /* This part of the algorithm will determine if it needs static or dynamic arrays */
  821. alloc_mode = set_alloc_mode(nb_parameters);
  822. arrays_managing(alloc_mode);
  823. }
  824. else if (TEST("Handles"))
  825. {
  826. char *buffer = s + 9;
  827. const char *delim = " ";
  828. char *token = strtok(buffer, delim);
  829. for (i = 0 ; i < nb_parameters ; i++)
  830. {
  831. STARPU_ASSERT(token);
  832. struct handle *handles_cell; /* A cell of the hash table for the handles */
  833. starpu_data_handle_t handle_value = (starpu_data_handle_t) strtol(token, NULL, 16); /* Get the ith handle on the line (in the file) */
  834. HASH_FIND(hh, handles_hash, &handle_value, sizeof(handle_value), handles_cell); /* Find if the handle_value was already registered as a key in the hash table */
  835. /* If it wasn't, then add it to the hash table */
  836. if (handles_cell == NULL)
  837. {
  838. /* Hide the initial handle from the file into the handles array to find it when necessary */
  839. handles_ptr[i] = handle_value;
  840. reg_signal[i] = 1;
  841. }
  842. else
  843. {
  844. handles_ptr[i] = handles_cell->mem_ptr;
  845. reg_signal[i] = 0;
  846. }
  847. token = strtok(NULL, delim);
  848. }
  849. }
  850. else if (TEST("Modes"))
  851. {
  852. char * buffer = s + 7;
  853. unsigned mode_i = 0;
  854. const char * delim = " ";
  855. char * token = strtok(buffer, delim);
  856. while (token != NULL && mode_i < nb_parameters)
  857. {
  858. /* Subject to the names of starpu modes enumerator are not modified */
  859. if (!strncmp(token, "RW", 2))
  860. {
  861. *(modes_ptr+mode_i) = STARPU_RW;
  862. mode_i++;
  863. }
  864. else if (!strncmp(token, "R", 1))
  865. {
  866. *(modes_ptr+mode_i) = STARPU_R;
  867. mode_i++;
  868. }
  869. else if (!strncmp(token, "W", 1))
  870. {
  871. *(modes_ptr+mode_i) = STARPU_W;
  872. mode_i++;
  873. }
  874. /* Other cases produce a warning*/
  875. else
  876. {
  877. fprintf(stderr, "[Warning] A mode is different from R/W (jobid task : %lu)", jobid);
  878. }
  879. token = strtok(NULL, delim);
  880. }
  881. }
  882. else if (TEST("Sizes"))
  883. {
  884. char * buffer = s + 7;
  885. const char * delim = " ";
  886. char * token = strtok(buffer, delim);
  887. unsigned k = 0;
  888. _STARPU_MALLOC(sizes_set, nb_parameters * sizeof(size_t));
  889. while (token != NULL && k < nb_parameters)
  890. {
  891. sizes_set[k] = strtol(token, NULL, 10);
  892. token = strtok(NULL, delim);
  893. k++;
  894. }
  895. }
  896. else if (TEST("StartTime"))
  897. {
  898. startTime = strtod(s+11, NULL);
  899. }
  900. else if (TEST("EndTime"))
  901. {
  902. endTime = strtod(s+9, NULL);
  903. }
  904. else if (TEST("GFlop"))
  905. {
  906. flops = 1000000000 * strtod(s+7, NULL);
  907. }
  908. else if (TEST("Iteration"))
  909. {
  910. iteration = (unsigned) strtol(s+11, NULL, 10);
  911. }
  912. else if (TEST("Priority"))
  913. {
  914. priority = strtol(s + 10, NULL, 10);
  915. }
  916. }
  917. eof:
  918. starpu_task_wait_for_all();
  919. fprintf(stderr, " done.\n");
  920. printf("%g ms", (starpu_timing_now() - start) / 1000.);
  921. if (total_flops != 0.)
  922. printf("\t%g GF/s", (total_flops / (starpu_timing_now() - start)) / 1000.);
  923. printf("\n");
  924. /* FREE allocated memory */
  925. free(dependson);
  926. free(s);
  927. /* End of FREE */
  928. struct handle *handle=NULL, *handletmp=NULL;
  929. HASH_ITER(hh, handles_hash, handle, handletmp)
  930. {
  931. starpu_data_unregister(handle->mem_ptr);
  932. HASH_DEL(handles_hash, handle);
  933. free(handle);
  934. }
  935. struct perfmodel *model_s=NULL, *modeltmp=NULL;
  936. HASH_ITER(hh, model_hash, model_s, modeltmp)
  937. {
  938. starpu_perfmodel_unload_model(&model_s->perfmodel);
  939. HASH_DEL(model_hash, model_s);
  940. free(model_s->model_name);
  941. free(model_s);
  942. }
  943. struct task *task=NULL, *tasktmp=NULL;
  944. HASH_ITER(hh, tasks, task, tasktmp)
  945. {
  946. free(task->task.cl_arg);
  947. free((char*)task->task.name);
  948. if (task->task.dyn_handles != NULL)
  949. {
  950. free(task->task.dyn_handles);
  951. free(task->task.dyn_modes);
  952. }
  953. HASH_DEL(tasks, task);
  954. starpu_task_clean(&task->task);
  955. free(task->deps);
  956. starpu_rbtree_remove(&tree, &task->node);
  957. free(task);
  958. }
  959. starpu_shutdown();
  960. return 0;
  961. enodev:
  962. starpu_shutdown();
  963. return 77;
  964. }