starpu_replay.c 29 KB

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