starpu_replay.c 24 KB

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