starpu_replay.c 29 KB

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