starpu_replay.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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;
  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. printf("\rExecuted task %lu...", nexecuted_tasks);
  286. fflush(stdout);
  287. }
  288. }
  289. /* [CODELET] Initialization of an unique codelet for all the tasks*/
  290. static int can_execute(unsigned worker_id, struct starpu_task *task, unsigned nimpl)
  291. {
  292. struct starpu_perfmodel_arch * arch = starpu_worker_get_perf_archtype(worker_id, STARPU_NMAX_SCHED_CTXS);
  293. int device = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  294. if (device == -1)
  295. /* Doesn't exist yet, thus unknown, assuming it can not work there. */
  296. return 0;
  297. (void) nimpl;
  298. /* Then, get the pointer to the value of the expected time */
  299. struct task_arg *arg = task->cl_arg;
  300. if (device < (int) arg->narch)
  301. {
  302. double val = arg->perf[device];
  303. if (!(val == 0 || isnan(val)))
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. static struct starpu_perfmodel myperfmodel =
  309. {
  310. .type = STARPU_PER_ARCH,
  311. .arch_cost_function = arch_cost_function,
  312. .footprint = get_footprint,
  313. };
  314. static struct starpu_codelet cl =
  315. {
  316. .cpu_funcs = { dumb_kernel },
  317. .cpu_funcs_name = { "dumb_kernel" },
  318. .cuda_funcs = { dumb_kernel },
  319. .opencl_funcs = { dumb_kernel },
  320. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  321. .can_execute = can_execute,
  322. .model = &myperfmodel,
  323. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  324. };
  325. /* * * * * * * * * * * * * *
  326. * * * * * Functions * * * * *
  327. * * * * * * * * * * * * * * */
  328. /* The following function checks if the program has to use static or dynamic arrays*/
  329. static int set_alloc_mode(int total_parameters)
  330. {
  331. return total_parameters <= STARPU_NMAXBUFS;
  332. }
  333. /* According to the allocation mode, modify handles_ptr and modes_ptr in static or dynamic */
  334. static void arrays_managing(int mode)
  335. {
  336. if (mode)
  337. {
  338. handles_ptr = &handles[0];
  339. modes_ptr = &modes[0];
  340. reg_signal = &normal_reg_signal[0];
  341. }
  342. else
  343. {
  344. _STARPU_MALLOC(handles_ptr, sizeof(*handles_ptr) * nb_parameters);
  345. _STARPU_MALLOC(modes_ptr, sizeof(*modes_ptr) * nb_parameters);
  346. _STARPU_CALLOC(reg_signal, nb_parameters, sizeof(char *));
  347. }
  348. }
  349. /* Check if a handle hasn't been registered yet */
  350. static void variable_data_register_check(size_t * array_of_size, int nb_handles)
  351. {
  352. int h, i;
  353. starpu_data_handle_t orig_handles[nb_handles];
  354. ARRAY_DUP(handles_ptr, orig_handles, nb_handles);
  355. for (h = 0 ; h < nb_handles ; h++)
  356. {
  357. if(reg_signal[h]) /* Get the register signal, if it's 1 do ... */
  358. {
  359. struct handle * handles_cell;
  360. for (i = 0; i < h; i++)
  361. {
  362. /* Maybe we just registered it in this very h loop */
  363. if (handles_ptr[h] == orig_handles[i])
  364. {
  365. handles_ptr[h] = handles_ptr[i];
  366. break;
  367. }
  368. }
  369. if (i == h)
  370. {
  371. _STARPU_MALLOC(handles_cell, sizeof(*handles_cell));
  372. STARPU_ASSERT(handles_cell != NULL);
  373. handles_cell->handle = handles_ptr[h]; /* Get the hidden key (initial handle from the file) to store it as a key*/
  374. replay_data_register(handles_ptr+h, handles_ptr[h],
  375. modes_ptr[h] & STARPU_R ? STARPU_MAIN_RAM : -1,
  376. array_of_size[h], array_of_size[h], array_of_size[h]);
  377. handles_cell->mem_ptr = handles_ptr[h]; /* Store the new value of the handle into the hash table */
  378. HASH_ADD(hh, handles_hash, handle, sizeof(handles_ptr[h]), handles_cell);
  379. }
  380. }
  381. }
  382. }
  383. void reset(void)
  384. {
  385. control = NormalTask;
  386. if (name != NULL)
  387. {
  388. free(name);
  389. name = NULL;
  390. }
  391. if (model != NULL)
  392. {
  393. free(model);
  394. model = NULL;
  395. }
  396. if (sizes_set != NULL)
  397. {
  398. free(sizes_set);
  399. sizes_set = NULL;
  400. }
  401. if (reg_signal != NULL)
  402. {
  403. if (!alloc_mode)
  404. {
  405. free(reg_signal);
  406. reg_signal = NULL;
  407. }
  408. else
  409. {
  410. ARRAY_INIT(reg_signal, nb_parameters);
  411. }
  412. }
  413. jobid = 0;
  414. ndependson = 0;
  415. tag = -1;
  416. workerid = -1;
  417. footprint = 0;
  418. startTime = 0.0;
  419. endTime = 0.0;
  420. if (submitorder != -1)
  421. submitorder = -1;
  422. iteration = -1;
  423. nb_parameters = 0;
  424. alloc_mode = 1;
  425. }
  426. void fix_wontuse_handle(struct task * wontuseTask)
  427. {
  428. STARPU_ASSERT(wontuseTask);
  429. if (!wontuseTask->reg_signal)
  430. /* Data was already registered when we created this task, so it's already a handle */
  431. return;
  432. struct handle *handle_tmp;
  433. /* Data was not registered when we created this task, so this is the application pointer, look it up now */
  434. HASH_FIND(hh, handles_hash, &wontuseTask->task.handles[0], sizeof(wontuseTask->task.handles[0]), handle_tmp);
  435. if (handle_tmp)
  436. wontuseTask->task.handles[0] = handle_tmp->mem_ptr;
  437. else
  438. /* This data wasn't actually used, don't care about it */
  439. wontuseTask->task.handles[0] = NULL;
  440. }
  441. /* Function that submits all the tasks (used when the program reaches EOF) */
  442. int submit_tasks(void)
  443. {
  444. /* Add dependencies */
  445. const struct starpu_rbtree * tmptree = &tree;
  446. struct starpu_rbtree_node * currentNode = starpu_rbtree_first(tmptree);
  447. long last_submitorder = 0;
  448. while (currentNode != NULL)
  449. {
  450. struct task * currentTask = (struct task *) currentNode;
  451. if (currentTask->type == NormalTask)
  452. {
  453. if (currentTask->submit_order != -1)
  454. {
  455. STARPU_ASSERT(currentTask->submit_order >= last_submitorder + 1);
  456. while (currentTask->submit_order > last_submitorder + 1)
  457. {
  458. /* Oops, some tasks were not submitted by original application, fake some */
  459. struct starpu_task *task = starpu_task_create();
  460. int ret;
  461. task->cl = NULL;
  462. ret = starpu_task_submit(task);
  463. STARPU_ASSERT(ret == 0);
  464. last_submitorder++;
  465. }
  466. }
  467. if (currentTask->ndependson > 0)
  468. {
  469. struct starpu_task * taskdeps[currentTask->ndependson];
  470. unsigned i, j = 0;
  471. for (i = 0; i < currentTask->ndependson; i++)
  472. {
  473. struct task * taskdep;
  474. /* Get the ith jobid of deps_jobid */
  475. HASH_FIND(hh, tasks, &currentTask->deps[i], sizeof(jobid), taskdep);
  476. if(taskdep)
  477. {
  478. taskdeps[j] = &taskdep->task;
  479. j ++;
  480. }
  481. }
  482. starpu_task_declare_deps_array(&currentTask->task, j, taskdeps);
  483. }
  484. if (!(currentTask->iteration == -1))
  485. starpu_iteration_push(currentTask->iteration);
  486. applySchedRec(&currentTask->task, currentTask->submit_order);
  487. int ret_val = starpu_task_submit(&currentTask->task);
  488. if (!(currentTask->iteration == -1))
  489. starpu_iteration_pop();
  490. if (ret_val != 0)
  491. {
  492. printf("\nWhile submitting task %ld (%s): return %d\n",
  493. currentTask->submit_order,
  494. currentTask->task.name? currentTask->task.name : "unknown",
  495. ret_val);
  496. return -1;
  497. }
  498. //printf("submitting task %s (%lu, %llu)\n", currentTask->task.name?currentTask->task.name:"anonymous", currentTask->jobid, (unsigned long long) currentTask->task.tag_id);
  499. if (!(currentTask->submit_order % 1000))
  500. {
  501. printf("\rSubmitted task order %ld...", currentTask->submit_order);
  502. fflush(stdout);
  503. }
  504. if (currentTask->submit_order != -1)
  505. last_submitorder++;
  506. }
  507. else
  508. {
  509. fix_wontuse_handle(currentTask); /* Add the handle in the wontuse task */
  510. /* FIXME: can not actually work properly since we have
  511. * disabled sequential consistency, so we don't have any
  512. * easy way to make this wait for the last task that
  513. * wrote to the handle. */
  514. if (currentTask->task.handles[0])
  515. starpu_data_wont_use(currentTask->task.handles[0]);
  516. }
  517. currentNode = starpu_rbtree_next(currentNode);
  518. }
  519. printf(" done.\n");
  520. return 1;
  521. }
  522. /* * * * * * * * * * * * * * * */
  523. /* * * * * * MAIN * * * * * * */
  524. /* * * * * * * * * * * * * * */
  525. static void usage(const char *program)
  526. {
  527. fprintf(stderr,"Usage: %s [--static-workerid] tasks.rec [sched.rec]\n", program);
  528. exit(EXIT_FAILURE);
  529. }
  530. int main(int argc, char **argv)
  531. {
  532. starpu_data_set_default_sequential_consistency_flag(0);
  533. FILE *rec;
  534. char *s;
  535. const char *tasks_rec = NULL;
  536. const char *sched_rec = NULL;
  537. unsigned i;
  538. size_t s_allocated = 128;
  539. unsigned long nread_tasks = 0;
  540. _STARPU_MALLOC(s, s_allocated);
  541. dependson_size = REPLAY_NMAX_DEPENDENCIES; /* Change the value of REPLAY_NMAX_DEPENCIES to modify the number of dependencies */
  542. _STARPU_MALLOC(dependson, dependson_size * sizeof (* dependson));
  543. alloc_mode = 1;
  544. for (i = 1; i < (unsigned) argc; i++)
  545. {
  546. if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
  547. {
  548. usage(argv[0]);
  549. }
  550. else if (!strcmp(argv[i], "--static-workerid"))
  551. {
  552. static_workerid = 1;
  553. }
  554. else
  555. {
  556. if (!tasks_rec)
  557. tasks_rec = argv[i];
  558. else if (!sched_rec)
  559. sched_rec = argv[i];
  560. else
  561. usage(argv[0]);
  562. }
  563. }
  564. if (!tasks_rec)
  565. usage(argv[0]);
  566. if (sched_rec)
  567. schedRecInit(sched_rec);
  568. rec = fopen(tasks_rec, "r");
  569. if (!rec)
  570. {
  571. fprintf(stderr,"unable to open file %s: %s\n", tasks_rec, strerror(errno));
  572. exit(EXIT_FAILURE);
  573. }
  574. int ret = starpu_init(NULL);
  575. if (ret == -ENODEV) goto enodev;
  576. /* Read line by line, and on empty line submit the task with the accumulated information */
  577. reset();
  578. double start = starpu_timing_now();
  579. int linenum = 0;
  580. while(1)
  581. {
  582. char *ln;
  583. if (!fgets(s, s_allocated, rec))
  584. {
  585. printf(" done.\n");
  586. int submitted = submit_tasks();
  587. if (submitted == -1)
  588. {
  589. goto enodev;
  590. }
  591. goto eof;
  592. }
  593. while (!(ln = strchr(s, '\n')))
  594. {
  595. /* fprintf(stderr,"buffer size %d too small, doubling it\n", s_allocated); */
  596. _STARPU_REALLOC(s, s_allocated * 2);
  597. if (!fgets(s + s_allocated-1, s_allocated+1, rec))
  598. {
  599. printf("\n");
  600. int submitted = submit_tasks();
  601. if (submitted == -1)
  602. {
  603. goto enodev;
  604. }
  605. goto eof;
  606. }
  607. s_allocated *= 2;
  608. }
  609. linenum++;
  610. if (ln == s)
  611. {
  612. /* Empty line, do task */
  613. struct task * task;
  614. _STARPU_MALLOC(task, sizeof(*task));
  615. starpu_task_init(&task->task);
  616. task->deps = NULL;
  617. task->submit_order = submitorder;
  618. starpu_rbtree_node_init(&task->node);
  619. starpu_rbtree_insert(&tree, &task->node, diff);
  620. task->jobid = jobid;
  621. task->iteration = iteration;
  622. if (name != NULL)
  623. task->task.name = strdup(name);
  624. task->type = control;
  625. if (control == NormalTask)
  626. {
  627. if (workerid >= 0)
  628. {
  629. task->task.priority = priority;
  630. task->task.cl = &cl;
  631. if (static_workerid)
  632. {
  633. task->task.workerid = workerid;
  634. task->task.execute_on_a_specific_worker = 1;
  635. }
  636. if (alloc_mode)
  637. {
  638. /* Duplicating the handles stored (and registered in the current context) into the task */
  639. ARRAY_DUP(modes_ptr, task->task.modes, nb_parameters);
  640. ARRAY_DUP(modes_ptr, task->task.cl->modes, nb_parameters);
  641. variable_data_register_check(sizes_set, nb_parameters);
  642. ARRAY_DUP(handles_ptr, task->task.handles, nb_parameters);
  643. }
  644. else
  645. {
  646. task->task.dyn_modes = modes_ptr;
  647. _STARPU_MALLOC(task->task.cl->dyn_modes, (sizeof(*task->task.cl->dyn_modes) * nb_parameters));
  648. ARRAY_DUP(modes_ptr, task->task.cl->dyn_modes, nb_parameters);
  649. variable_data_register_check(sizes_set, nb_parameters);
  650. task->task.dyn_handles = handles_ptr;
  651. }
  652. task->task.nbuffers = nb_parameters;
  653. struct perfmodel * realmodel;
  654. HASH_FIND_STR(model_hash, model, realmodel);
  655. if (realmodel == NULL)
  656. {
  657. int len = strlen(model);
  658. _STARPU_CALLOC(realmodel, 1, sizeof(struct perfmodel));
  659. _STARPU_MALLOC(realmodel->model_name, sizeof(char) * (len+1));
  660. realmodel->model_name = strcpy(realmodel->model_name, model);
  661. starpu_perfmodel_init(&realmodel->perfmodel);
  662. int error = starpu_perfmodel_load_symbol(model, &realmodel->perfmodel);
  663. if (!error)
  664. {
  665. HASH_ADD_STR(model_hash, model_name, realmodel);
  666. }
  667. else
  668. {
  669. fprintf(stderr, "[starpu][Warning] Error loading perfmodel symbol %s\n", model);
  670. fprintf(stderr, "[starpu][Warning] Taking only measurements from the given execution, and forcing execution on worker %d\n", workerid);
  671. starpu_perfmodel_unload_model(&realmodel->perfmodel);
  672. free(realmodel->model_name);
  673. free(realmodel);
  674. realmodel = NULL;
  675. }
  676. }
  677. struct starpu_perfmodel_arch *arch = starpu_worker_get_perf_archtype(workerid, 0);
  678. unsigned comb = starpu_perfmodel_arch_comb_add(arch->ndevices, arch->devices);
  679. unsigned narch = starpu_perfmodel_get_narch_combs();
  680. struct task_arg *arg;
  681. _STARPU_MALLOC(arg, sizeof(struct task_arg) + sizeof(double) * narch);
  682. arg->footprint = footprint;
  683. arg->narch = narch;
  684. double * perfTime = arg->perf;
  685. if (realmodel == NULL)
  686. {
  687. /* Erf, do without perfmodel, for execution there */
  688. task->task.workerid = workerid;
  689. task->task.execute_on_a_specific_worker = 1;
  690. for (i = 0; i < narch ; i++)
  691. {
  692. if (i == comb)
  693. perfTime[i] = endTime - startTime;
  694. else
  695. perfTime[i] = NAN;
  696. }
  697. }
  698. else
  699. {
  700. int one = 0;
  701. for (i = 0; i < narch ; i++)
  702. {
  703. arch = starpu_perfmodel_arch_comb_fetch(i);
  704. perfTime[i] = starpu_perfmodel_history_based_expected_perf(&realmodel->perfmodel, arch, footprint);
  705. if (!(perfTime[i] == 0 || isnan(perfTime[i])))
  706. one = 1;
  707. }
  708. if (!one)
  709. {
  710. fprintf(stderr, "We do not have any performance measurement for symbol '%s' for footprint %x, we can not execute this", model, footprint);
  711. exit(EXIT_FAILURE);
  712. }
  713. }
  714. task->task.cl_arg = arg;
  715. task->task.flops = flops;
  716. }
  717. task->task.cl_arg_size = 0;
  718. task->task.tag_id = tag;
  719. task->task.use_tag = 1;
  720. task->ndependson = ndependson;
  721. if (ndependson > 0)
  722. {
  723. _STARPU_MALLOC(task->deps, ndependson * sizeof (* task->deps));
  724. ARRAY_DUP(dependson, task->deps, ndependson);
  725. }
  726. }
  727. else
  728. {
  729. STARPU_ASSERT(nb_parameters == 1);
  730. task->reg_signal = reg_signal[0];
  731. ARRAY_DUP(handles_ptr, task->task.handles, nb_parameters);
  732. }
  733. /* Add this task to task hash */
  734. HASH_ADD(hh, tasks, jobid, sizeof(jobid), task);
  735. nread_tasks++;
  736. if (!(nread_tasks % 1000))
  737. {
  738. printf("\rRead task %lu...", nread_tasks);
  739. fflush(stdout);
  740. }
  741. reset();
  742. }
  743. /* Record various information */
  744. #define TEST(field) (!strncmp(s, field": ", strlen(field) + 2))
  745. else if(TEST("Control"))
  746. {
  747. char * c = s+9;
  748. if(!strncmp(c, "WontUse", 7))
  749. {
  750. control = WontUseTask;
  751. nb_parameters = 1;
  752. alloc_mode = set_alloc_mode(nb_parameters);
  753. arrays_managing(alloc_mode);
  754. }
  755. else
  756. control = NormalTask;
  757. }
  758. else if (TEST("Name"))
  759. {
  760. *ln = 0;
  761. name = strdup(s+6);
  762. }
  763. else if (TEST("Model"))
  764. {
  765. *ln = 0;
  766. model = strdup(s+7);
  767. }
  768. else if (TEST("JobId"))
  769. jobid = atol(s+7);
  770. else if(TEST("SubmitOrder"))
  771. submitorder = atoi(s+13);
  772. else if (TEST("DependsOn"))
  773. {
  774. char *c = s + 11;
  775. for (ndependson = 0; *c != '\n'; ndependson++)
  776. {
  777. if (ndependson >= dependson_size)
  778. {
  779. dependson_size *= 2;
  780. _STARPU_REALLOC(dependson, dependson_size * sizeof(*dependson));
  781. }
  782. dependson[ndependson] = strtol(c, &c, 10);
  783. }
  784. }
  785. else if (TEST("Tag"))
  786. {
  787. tag = strtol(s+5, NULL, 16);
  788. }
  789. else if (TEST("WorkerId"))
  790. {
  791. workerid = atoi(s+10);
  792. }
  793. else if (TEST("Footprint"))
  794. {
  795. footprint = strtoul(s+11, NULL, 16);
  796. }
  797. else if (TEST("Parameters"))
  798. {
  799. /* Parameters line format is PARAM1 PARAM2 (...)PARAMi (...)PARAMn */
  800. char * param_str = s + 12;
  801. int count = 0;
  802. for (i = 0 ; param_str[i] != '\n'; i++)
  803. {
  804. if (param_str[i] == ' ') /* Checking the number of ' ' (space), assuming that the file is not corrupted */
  805. {
  806. count++;
  807. }
  808. }
  809. 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) */
  810. /* This part of the algorithm will determine if it needs static or dynamic arrays */
  811. alloc_mode = set_alloc_mode(nb_parameters);
  812. arrays_managing(alloc_mode);
  813. }
  814. else if (TEST("Handles"))
  815. {
  816. char *buffer = s + 9;
  817. const char *delim = " ";
  818. char *token = strtok(buffer, delim);
  819. for (i = 0 ; i < nb_parameters ; i++)
  820. {
  821. STARPU_ASSERT(token);
  822. struct handle *handles_cell; /* A cell of the hash table for the handles */
  823. starpu_data_handle_t handle_value = (starpu_data_handle_t) strtol(token, NULL, 16); /* Get the ith handle on the line (in the file) */
  824. 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 */
  825. /* If it wasn't, then add it to the hash table */
  826. if (handles_cell == NULL)
  827. {
  828. /* Hide the initial handle from the file into the handles array to find it when necessary */
  829. handles_ptr[i] = handle_value;
  830. reg_signal[i] = 1;
  831. }
  832. else
  833. {
  834. handles_ptr[i] = handles_cell->mem_ptr;
  835. reg_signal[i] = 0;
  836. }
  837. token = strtok(NULL, delim);
  838. }
  839. }
  840. else if (TEST("Modes"))
  841. {
  842. char * buffer = s + 7;
  843. unsigned mode_i = 0;
  844. const char * delim = " ";
  845. char * token = strtok(buffer, delim);
  846. while (token != NULL && mode_i < nb_parameters)
  847. {
  848. /* Subject to the names of starpu modes enumerator are not modified */
  849. if (!strncmp(token, "RW", 2))
  850. {
  851. *(modes_ptr+mode_i) = STARPU_RW;
  852. mode_i++;
  853. }
  854. else if (!strncmp(token, "R", 1))
  855. {
  856. *(modes_ptr+mode_i) = STARPU_R;
  857. mode_i++;
  858. }
  859. else if (!strncmp(token, "W", 1))
  860. {
  861. *(modes_ptr+mode_i) = STARPU_W;
  862. mode_i++;
  863. }
  864. /* Other cases produce a warning*/
  865. else
  866. {
  867. fprintf(stderr, "[Warning] A mode is different from R/W (jobid task : %lu)", jobid);
  868. }
  869. token = strtok(NULL, delim);
  870. }
  871. }
  872. else if (TEST("Sizes"))
  873. {
  874. char * buffer = s + 7;
  875. const char * delim = " ";
  876. char * token = strtok(buffer, delim);
  877. unsigned k = 0;
  878. _STARPU_MALLOC(sizes_set, nb_parameters * sizeof(size_t));
  879. while (token != NULL && k < nb_parameters)
  880. {
  881. sizes_set[k] = strtol(token, NULL, 10);
  882. token = strtok(NULL, delim);
  883. k++;
  884. }
  885. }
  886. else if (TEST("StartTime"))
  887. {
  888. startTime = strtod(s+11, NULL);
  889. }
  890. else if (TEST("EndTime"))
  891. {
  892. endTime = strtod(s+9, NULL);
  893. }
  894. else if (TEST("GFlop"))
  895. {
  896. flops = 1000000000 * strtod(s+7, NULL);
  897. }
  898. else if (TEST("Iteration"))
  899. {
  900. iteration = (unsigned) strtol(s+11, NULL, 10);
  901. }
  902. else if (TEST("Priority"))
  903. {
  904. priority = strtol(s + 10, NULL, 10);
  905. }
  906. }
  907. eof:
  908. starpu_task_wait_for_all();
  909. printf(" done.\n");
  910. printf("Simulation ended. Elapsed simulated time: %g ms\n", (starpu_timing_now() - start) / 1000.);
  911. /* FREE allocated memory */
  912. free(dependson);
  913. free(s);
  914. /* End of FREE */
  915. struct handle *handle=NULL, *handletmp=NULL;
  916. HASH_ITER(hh, handles_hash, handle, handletmp)
  917. {
  918. starpu_data_unregister(handle->mem_ptr);
  919. HASH_DEL(handles_hash, handle);
  920. free(handle);
  921. }
  922. struct perfmodel *model_s=NULL, *modeltmp=NULL;
  923. HASH_ITER(hh, model_hash, model_s, modeltmp)
  924. {
  925. starpu_perfmodel_unload_model(&model_s->perfmodel);
  926. HASH_DEL(model_hash, model_s);
  927. free(model_s->model_name);
  928. free(model_s);
  929. }
  930. struct task *task=NULL, *tasktmp=NULL;
  931. HASH_ITER(hh, tasks, task, tasktmp)
  932. {
  933. free(task->task.cl_arg);
  934. free((char*)task->task.name);
  935. if (task->task.dyn_handles != NULL)
  936. {
  937. free(task->task.dyn_handles);
  938. free(task->task.dyn_modes);
  939. }
  940. HASH_DEL(tasks, task);
  941. starpu_task_clean(&task->task);
  942. free(task->deps);
  943. starpu_rbtree_remove(&tree, &task->node);
  944. free(task);
  945. }
  946. starpu_shutdown();
  947. return 0;
  948. enodev:
  949. starpu_shutdown();
  950. return 77;
  951. }