starpu_replay.c 30 KB

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