bound.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010-2013 Université de Bordeaux 1
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * Record which kinds of tasks have been executed, to later on compute an upper
  20. * bound of the performance that could have theoretically been achieved
  21. */
  22. #include <starpu.h>
  23. #include <starpu_config.h>
  24. #include <profiling/bound.h>
  25. #include <core/jobs.h>
  26. #include <core/workers.h>
  27. #ifdef STARPU_HAVE_GLPK_H
  28. #include <glpk.h>
  29. #endif /* STARPU_HAVE_GLPK_H */
  30. /* TODO: output duration between starpu_bound_start and starpu_bound_stop */
  31. /*
  32. * Record without dependencies: just count each kind of task
  33. *
  34. * The linear programming problem will just have as variables:
  35. * - the number of tasks of kind `t' executed by worker `w'
  36. * - the total duration
  37. *
  38. * and the constraints will be:
  39. * - the time taken by each worker to complete its assigned tasks is lower than
  40. * the total duration.
  41. * - the total numer of tasks of a given kind is equal to the number run by the
  42. * application.
  43. */
  44. struct bound_task_pool
  45. {
  46. /* Which codelet has been executed */
  47. struct starpu_codelet *cl;
  48. /* Task footprint key (for history-based perfmodel) */
  49. uint32_t footprint;
  50. /* Number of tasks of this kind */
  51. unsigned long n;
  52. /* Other task kinds */
  53. struct bound_task_pool *next;
  54. };
  55. /*
  56. * Record with dependencies: each task is recorded separately
  57. *
  58. * The linear programming problem will have as variables:
  59. * - The start time of each task
  60. * - The completion time of each tag
  61. * - The total duration
  62. * - For each task and for each worker, whether the task is executing on that worker.
  63. * - For each pair of task, which task is scheduled first.
  64. *
  65. * and the constraints will be:
  66. * - All task start time plus duration are less than total duration
  67. * - Each task is executed on exactly one worker.
  68. * - Each task starts after all its task dependencies finish.
  69. * - Each task starts after all its tag dependencies finish.
  70. * - For each task pair and each worker, if both tasks are executed by that worker,
  71. * one is started after the other's completion.
  72. */
  73. struct task_dep
  74. {
  75. /* Task this depends on */
  76. struct bound_task *dep;
  77. /* Data transferred between tasks (i.e. implicit data dep size) */
  78. size_t size;
  79. };
  80. struct bound_task
  81. {
  82. /* Unique ID */
  83. unsigned long id;
  84. /* Tag ID, if any */
  85. starpu_tag_t tag_id;
  86. int use_tag;
  87. /* Which codelet has been executed */
  88. struct starpu_codelet *cl;
  89. /* Task footprint key */
  90. uint32_t footprint;
  91. /* Task priority */
  92. int priority;
  93. /* Tasks this one depends on */
  94. struct task_dep *deps;
  95. int depsn;
  96. /* Estimated duration */
  97. double** duration[STARPU_NARCH];
  98. /* Other tasks */
  99. struct bound_task *next;
  100. };
  101. struct bound_tag_dep
  102. {
  103. starpu_tag_t tag;
  104. starpu_tag_t dep_tag;
  105. struct bound_tag_dep *next;
  106. };
  107. static struct bound_task_pool *task_pools, *last;
  108. static struct bound_task *tasks;
  109. static struct bound_tag_dep *tag_deps;
  110. int _starpu_bound_recording;
  111. static int recorddeps;
  112. static int recordprio;
  113. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  114. /* Initialization */
  115. void starpu_bound_start(int deps, int prio)
  116. {
  117. struct bound_task_pool *tp;
  118. struct bound_task *t;
  119. struct bound_tag_dep *td;
  120. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  121. tp = task_pools;
  122. task_pools = NULL;
  123. last = NULL;
  124. t = tasks;
  125. tasks = NULL;
  126. td = tag_deps;
  127. tag_deps = NULL;
  128. _starpu_bound_recording = 1;
  129. recorddeps = deps;
  130. recordprio = prio;
  131. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  132. while (tp != NULL)
  133. {
  134. struct bound_task_pool *next = tp->next;
  135. free(tp);
  136. tp = next;
  137. }
  138. while (t != NULL)
  139. {
  140. struct bound_task *next = t->next;
  141. free(t);
  142. t = next;
  143. }
  144. while (td != NULL)
  145. {
  146. struct bound_tag_dep *next = td->next;
  147. free(td);
  148. td = next;
  149. }
  150. }
  151. /* Whether we will include it in the computation */
  152. static int good_job(struct _starpu_job *j)
  153. {
  154. /* No codelet, nothing to measure */
  155. if (j->exclude_from_dag)
  156. return 0;
  157. if (!j->task->cl)
  158. return 0;
  159. /* No performance model, no time duration estimation */
  160. if (!j->task->cl->model)
  161. return 0;
  162. /* Only support history based */
  163. if (j->task->cl->model->type != STARPU_HISTORY_BASED
  164. && j->task->cl->model->type != STARPU_NL_REGRESSION_BASED)
  165. return 0;
  166. return 1;
  167. }
  168. static double** initialize_arch_duration(int maxdevid, unsigned* maxncore_table)
  169. {
  170. int devid, maxncore;
  171. double ** arch_model = malloc(sizeof(*arch_model)*(maxdevid+1));
  172. arch_model[maxdevid] = NULL;
  173. for(devid=0; devid<maxdevid; devid++)
  174. {
  175. if(maxncore_table != NULL)
  176. maxncore = maxncore_table[devid];
  177. else
  178. maxncore = 1;
  179. arch_model[devid] = malloc(sizeof(*arch_model[devid])*(maxncore+1));
  180. }
  181. return arch_model;
  182. }
  183. static void initialize_duration(struct bound_task *task)
  184. {
  185. struct _starpu_machine_config *conf = _starpu_get_machine_config();
  186. task->duration[STARPU_CPU_WORKER] = initialize_arch_duration(1,&conf->topology.ncpus);
  187. task->duration[STARPU_CUDA_WORKER] = initialize_arch_duration(conf->topology.ncudagpus,NULL);
  188. task->duration[STARPU_OPENCL_WORKER] = initialize_arch_duration(conf->topology.nopenclgpus,NULL);
  189. task->duration[STARPU_MIC_WORKER] = initialize_arch_duration(conf->topology.nmicdevices,conf->topology.nmiccores);
  190. task->duration[STARPU_SCC_WORKER] = initialize_arch_duration(conf->topology.nsccdevices,NULL);
  191. }
  192. /* Create a new task (either because it has just been submitted, or a
  193. * dependency was added before submission) */
  194. static void new_task(struct _starpu_job *j)
  195. {
  196. struct bound_task *t;
  197. if (j->bound_task)
  198. return;
  199. t = (struct bound_task *) malloc(sizeof(*t));
  200. memset(t, 0, sizeof(*t));
  201. t->id = j->job_id;
  202. t->tag_id = j->task->tag_id;
  203. t->use_tag = j->task->use_tag;
  204. t->cl = j->task->cl;
  205. t->footprint = _starpu_compute_buffers_footprint(NULL, STARPU_CPU_WORKER, 0, j);
  206. t->priority = j->task->priority;
  207. t->deps = NULL;
  208. t->depsn = 0;
  209. initialize_duration(t);
  210. t->next = tasks;
  211. j->bound_task = t;
  212. tasks = t;
  213. }
  214. /* A new task was submitted, record it */
  215. void _starpu_bound_record(struct _starpu_job *j)
  216. {
  217. if (!_starpu_bound_recording)
  218. return;
  219. if (!good_job(j))
  220. return;
  221. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  222. /* Re-check, this time with mutex held */
  223. if (!_starpu_bound_recording)
  224. {
  225. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  226. return;
  227. }
  228. if (recorddeps)
  229. {
  230. new_task(j);
  231. }
  232. else
  233. {
  234. struct bound_task_pool *tp;
  235. _starpu_compute_buffers_footprint(NULL, STARPU_CPU_WORKER, 0, j);
  236. if (last && last->cl == j->task->cl && last->footprint == j->footprint)
  237. tp = last;
  238. else
  239. for (tp = task_pools; tp; tp = tp->next)
  240. if (tp->cl == j->task->cl && tp->footprint == j->footprint)
  241. break;
  242. if (!tp)
  243. {
  244. tp = (struct bound_task_pool *) malloc(sizeof(*tp));
  245. tp->cl = j->task->cl;
  246. tp->footprint = j->footprint;
  247. tp->n = 0;
  248. tp->next = task_pools;
  249. task_pools = tp;
  250. }
  251. /* One more task of this kind */
  252. tp->n++;
  253. }
  254. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  255. }
  256. /* A tag dependency was emitted, record it */
  257. void _starpu_bound_tag_dep(starpu_tag_t id, starpu_tag_t dep_id)
  258. {
  259. struct bound_tag_dep *td;
  260. if (!_starpu_bound_recording || !recorddeps)
  261. return;
  262. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  263. /* Re-check, this time with mutex held */
  264. if (!_starpu_bound_recording || !recorddeps)
  265. {
  266. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  267. return;
  268. }
  269. td = (struct bound_tag_dep *) malloc(sizeof(*td));
  270. td->tag = id;
  271. td->dep_tag = dep_id;
  272. td->next = tag_deps;
  273. tag_deps = td;
  274. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  275. }
  276. /* A task dependency was emitted, record it */
  277. void _starpu_bound_task_dep(struct _starpu_job *j, struct _starpu_job *dep_j)
  278. {
  279. struct bound_task *t;
  280. int i;
  281. if (!_starpu_bound_recording || !recorddeps)
  282. return;
  283. if (!good_job(j) || !good_job(dep_j))
  284. return;
  285. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  286. /* Re-check, this time with mutex held */
  287. if (!_starpu_bound_recording || !recorddeps)
  288. {
  289. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  290. return;
  291. }
  292. new_task(j);
  293. new_task(dep_j);
  294. t = j->bound_task;
  295. for (i = 0; i < t->depsn; i++)
  296. if (t->deps[i].dep == dep_j->bound_task)
  297. break;
  298. if (i == t->depsn)
  299. {
  300. /* Not already there, add */
  301. t->deps = (struct task_dep *) realloc(t->deps, ++t->depsn * sizeof(t->deps[0]));
  302. t->deps[t->depsn-1].dep = dep_j->bound_task;
  303. t->deps[t->depsn-1].size = 0; /* We don't have data information in that case */
  304. }
  305. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  306. }
  307. /* Look for job with id ID among our tasks */
  308. static struct bound_task *find_job(unsigned long id)
  309. {
  310. struct bound_task *t;
  311. for (t = tasks; t; t = t->next)
  312. if (t->id == id)
  313. return t;
  314. return NULL;
  315. }
  316. /* Job J depends on previous job of id ID (which is already finished) */
  317. void _starpu_bound_job_id_dep(starpu_data_handle_t handle, struct _starpu_job *j, unsigned long id)
  318. {
  319. struct bound_task *t, *dep_t;
  320. int i;
  321. if (!_starpu_bound_recording || !recorddeps)
  322. return;
  323. if (!good_job(j))
  324. return;
  325. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  326. /* Re-check, this time with mutex held */
  327. if (!_starpu_bound_recording || !recorddeps)
  328. {
  329. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  330. return;
  331. }
  332. new_task(j);
  333. dep_t = find_job(id);
  334. if (!dep_t)
  335. {
  336. fprintf(stderr,"dependency %lu not found !\n", id);
  337. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  338. return;
  339. }
  340. t = j->bound_task;
  341. for (i = 0; i < t->depsn; i++)
  342. if (t->deps[i].dep == dep_t)
  343. {
  344. /* Found, just add size */
  345. t->deps[i].size += _starpu_data_get_size(handle);
  346. break;
  347. }
  348. if (i == t->depsn)
  349. {
  350. /* Not already there, add */
  351. t->deps = (struct task_dep *) realloc(t->deps, ++t->depsn * sizeof(t->deps[0]));
  352. t->deps[t->depsn-1].dep = dep_t;
  353. t->deps[t->depsn-1].size = _starpu_data_get_size(handle);
  354. }
  355. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  356. }
  357. void starpu_bound_stop(void)
  358. {
  359. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  360. _starpu_bound_recording = 0;
  361. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  362. }
  363. /* Compute all tasks times on all workers */
  364. static void _starpu_get_tasks_times(int nw, int nt, double *times)
  365. {
  366. struct bound_task_pool *tp;
  367. int w, t;
  368. for (w = 0; w < nw; w++)
  369. {
  370. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  371. {
  372. struct _starpu_job j =
  373. {
  374. .footprint = tp->footprint,
  375. .footprint_is_computed = 1,
  376. };
  377. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(w);
  378. double length = _starpu_history_based_job_expected_perf(tp->cl->model, arch, &j, j.nimpl);
  379. if (isnan(length))
  380. times[w*nt+t] = NAN;
  381. else
  382. times[w*nt+t] = length / 1000.;
  383. }
  384. }
  385. }
  386. /* Return whether PARENT is an ancestor of CHILD */
  387. static int ancestor(struct bound_task *child, struct bound_task *parent)
  388. {
  389. int i;
  390. for (i = 0; i < child->depsn; i++)
  391. {
  392. if (parent == child->deps[i].dep)
  393. return 1;
  394. if (ancestor(child->deps[i].dep, parent))
  395. return -1;
  396. }
  397. return 0;
  398. }
  399. /* Print bound recording in .dot format */
  400. void starpu_bound_print_dot(FILE *output)
  401. {
  402. struct bound_task *t;
  403. struct bound_tag_dep *td;
  404. int i;
  405. if (!recorddeps)
  406. {
  407. fprintf(output, "Not supported\n");
  408. return;
  409. }
  410. fprintf(output, "strict digraph bounddeps {\n");
  411. for (t = tasks; t; t = t->next)
  412. {
  413. fprintf(output, "\"t%lu\" [label=\"%lu: %s\"]\n", t->id, t->id, _starpu_codelet_get_model_name(t->cl));
  414. for (i = 0; i < t->depsn; i++)
  415. fprintf(output, "\"t%lu\" -> \"t%lu\"\n", t->deps[i].dep->id, t->id);
  416. }
  417. for (td = tag_deps; td; td = td->next)
  418. fprintf(output, "\"tag%lu\" -> \"tag%lu\";\n", (unsigned long) td->dep_tag, (unsigned long) td->tag);
  419. fprintf(output, "}\n");
  420. }
  421. /*
  422. * Print bound system in lp_solve format
  423. *
  424. * When dependencies are enabled, you can check the set of tasks and deps that
  425. * were recorded by using tools/lp2paje and vite.
  426. */
  427. void starpu_bound_print_lp(FILE *output)
  428. {
  429. int nt; /* Number of different kinds of tasks */
  430. int nw; /* Number of different workers */
  431. int t;
  432. int w, w2; /* worker */
  433. unsigned n, n2;
  434. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  435. nw = starpu_worker_get_count();
  436. if (recorddeps)
  437. {
  438. struct bound_task *t1, *t2;
  439. struct bound_tag_dep *td;
  440. int i;
  441. nt = 0;
  442. for (t1 = tasks; t1; t1 = t1->next)
  443. {
  444. if (t1->cl->model->type != STARPU_HISTORY_BASED &&
  445. t1->cl->model->type != STARPU_NL_REGRESSION_BASED)
  446. /* TODO: */
  447. fprintf(stderr, "Warning: task %s uses a perf model which is neither history nor non-linear regression-based, support for such model is not implemented yet, system will not be solvable.\n", _starpu_codelet_get_model_name(t1->cl));
  448. struct _starpu_job j =
  449. {
  450. .footprint = t1->footprint,
  451. .footprint_is_computed = 1,
  452. };
  453. for (w = 0; w < nw; w++)
  454. {
  455. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(w);
  456. if (_STARPU_IS_ZERO(t1->duration[arch->type][arch->devid][arch->ncore]))
  457. {
  458. double length = _starpu_history_based_job_expected_perf(t1->cl->model, arch, &j,j.nimpl);
  459. if (isnan(length))
  460. /* Avoid problems with binary coding of doubles */
  461. t1->duration[arch->type][arch->devid][arch->ncore] = NAN;
  462. else
  463. t1->duration[arch->type][arch->devid][arch->ncore] = length / 1000.;
  464. }
  465. }
  466. nt++;
  467. }
  468. fprintf(output, "/* StarPU upper bound linear programming problem, to be run in lp_solve. */\n\n");
  469. fprintf(output, "/* !! This is a big system, it will be long to solve !! */\n\n");
  470. fprintf(output, "/* We want to minimize total execution time (ms) */\n");
  471. fprintf(output, "min: tmax;\n\n");
  472. fprintf(output, "/* Number of tasks */\n");
  473. fprintf(output, "nt = %d;\n", nt);
  474. fprintf(output, "/* Number of workers */\n");
  475. fprintf(output, "nw = %d;\n", nw);
  476. fprintf(output, "/* The total execution time is the maximum of all task completion times (ms) */\n");
  477. for (t1 = tasks; t1; t1 = t1->next)
  478. fprintf(output, "c%lu <= tmax;\n", t1->id);
  479. fprintf(output, "\n/* We have tasks executing on workers, exactly one worker executes each task */\n");
  480. for (t1 = tasks; t1; t1 = t1->next)
  481. {
  482. for (w = 0; w < nw; w++)
  483. {
  484. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(w);
  485. if (!isnan(t1->duration[arch->type][arch->devid][arch->ncore]))
  486. fprintf(output, " +t%luw%d", t1->id, w);
  487. }
  488. fprintf(output, " = 1;\n");
  489. }
  490. fprintf(output, "\n/* Completion time is start time plus computation time */\n");
  491. fprintf(output, "/* According to where the task is indeed executed */\n");
  492. for (t1 = tasks; t1; t1 = t1->next)
  493. {
  494. fprintf(output, "/* %s %x */\tc%lu = s%lu", _starpu_codelet_get_model_name(t1->cl), (unsigned) t1->footprint, t1->id, t1->id);
  495. for (w = 0; w < nw; w++)
  496. {
  497. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(w);
  498. if (!isnan(t1->duration[arch->type][arch->devid][arch->ncore]))
  499. fprintf(output, " + %f t%luw%d", t1->duration[arch->type][arch->devid][arch->ncore], t1->id, w);
  500. }
  501. fprintf(output, ";\n");
  502. }
  503. fprintf(output, "\n/* Each task starts after all its task dependencies finish and data is transferred. */\n");
  504. fprintf(output, "/* Note that the dependency finish time depends on the worker where it's working */\n");
  505. for (t1 = tasks; t1; t1 = t1->next)
  506. for (i = 0; i < t1->depsn; i++)
  507. {
  508. fprintf(output, "/* %lu bytes transferred */\n", (unsigned long) t1->deps[i].size);
  509. fprintf(output, "s%lu >= c%lu", t1->id, t1->deps[i].dep->id);
  510. /* Transfer time: pick up one source node and a worker on it */
  511. for (n = 0; n < starpu_memory_nodes_get_count(); n++)
  512. for (w = 0; w < nw; w++)
  513. if (starpu_worker_get_memory_node(w) == n)
  514. {
  515. /* pick up another destination node and a worker on it */
  516. for (n2 = 0; n2 < starpu_memory_nodes_get_count(); n2++)
  517. if (n2 != n)
  518. {
  519. for (w2 = 0; w2 < nw; w2++)
  520. if (starpu_worker_get_memory_node(w2) == n2)
  521. {
  522. /* If predecessor is on worker w and successor
  523. * on worker w2 on different nodes, we need to
  524. * transfer the data. */
  525. fprintf(output, " + d_t%luw%ut%luw%u", t1->deps[i].dep->id, w, t1->id, w2);
  526. }
  527. }
  528. }
  529. fprintf(output, ";\n");
  530. /* Transfer time: pick up one source node and a worker on it */
  531. for (n = 0; n < starpu_memory_nodes_get_count(); n++)
  532. for (w = 0; w < nw; w++)
  533. if (starpu_worker_get_memory_node(w) == n)
  534. {
  535. /* pick up another destination node and a worker on it */
  536. for (n2 = 0; n2 < starpu_memory_nodes_get_count(); n2++)
  537. if (n2 != n)
  538. {
  539. for (w2 = 0; w2 < nw; w2++)
  540. if (starpu_worker_get_memory_node(w2) == n2)
  541. {
  542. /* The data transfer is at least 0ms */
  543. fprintf(output, "d_t%luw%ut%luw%u >= 0;\n", t1->deps[i].dep->id, w, t1->id, w2);
  544. /* The data transfer from w to w2 only happens if tasks run there */
  545. fprintf(output, "d_t%luw%ut%luw%u >= %f - 2e5 + 1e5 t%luw%u + 1e5 t%luw%u;\n",
  546. t1->deps[i].dep->id, w, t1->id, w2,
  547. starpu_transfer_predict(n, n2, t1->deps[i].size)/1000.,
  548. t1->deps[i].dep->id, w, t1->id, w2);
  549. }
  550. }
  551. }
  552. }
  553. fprintf(output, "\n/* Each tag finishes when its corresponding task finishes */\n");
  554. for (t1 = tasks; t1; t1 = t1->next)
  555. if (t1->use_tag)
  556. {
  557. for (w = 0; w < nw; w++)
  558. fprintf(output, "c%lu = tag%lu;\n", t1->id, (unsigned long) t1->tag_id);
  559. }
  560. fprintf(output, "\n/* tags start after all their tag dependencies finish. */\n");
  561. for (td = tag_deps; td; td = td->next)
  562. fprintf(output, "tag%lu >= tag%lu;\n", (unsigned long) td->tag, (unsigned long) td->dep_tag);
  563. /* TODO: factorize ancestor calls */
  564. fprintf(output, "\n/* For each task pair and each worker, if both tasks are executed by the same worker,\n");
  565. fprintf(output, " one is started after the other's completion */\n");
  566. for (t1 = tasks; t1; t1 = t1->next)
  567. {
  568. for (t2 = t1->next; t2; t2 = t2->next)
  569. {
  570. if (!ancestor(t1, t2) && !ancestor(t2, t1))
  571. {
  572. for (w = 0; w < nw; w++)
  573. {
  574. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(w);
  575. if (!isnan(t1->duration[arch->type][arch->devid][arch->ncore]))
  576. {
  577. fprintf(output, "s%lu - c%lu >= -3e5 + 1e5 t%luw%d + 1e5 t%luw%d + 1e5 t%luafter%lu;\n",
  578. t1->id, t2->id, t1->id, w, t2->id, w, t1->id, t2->id);
  579. fprintf(output, "s%lu - c%lu >= -2e5 + 1e5 t%luw%d + 1e5 t%luw%d - 1e5 t%luafter%lu;\n",
  580. t2->id, t1->id, t1->id, w, t2->id, w, t1->id, t2->id);
  581. }
  582. }
  583. }
  584. }
  585. }
  586. #if 0
  587. /* Doesn't help at all to actually express what "after" means */
  588. for (t1 = tasks; t1; t1 = t1->next)
  589. for (t2 = t1->next; t2; t2 = t2->next)
  590. if (!ancestor(t1, t2) && !ancestor(t2, t1))
  591. {
  592. fprintf(output, "s%lu - s%lu >= -1e5 + 1e5 t%luafter%lu;\n", t1->id, t2->id, t1->id, t2->id);
  593. fprintf(output, "s%lu - s%lu >= -1e5 t%luafter%lu;\n", t2->id, t1->id, t1->id, t2->id);
  594. }
  595. #endif
  596. if (recordprio)
  597. {
  598. fprintf(output, "\n/* For StarPU, a priority means given schedulable tasks it will consider the\n");
  599. fprintf(output, " * more prioritized first */\n");
  600. for (t1 = tasks; t1; t1 = t1->next)
  601. {
  602. for (t2 = t1->next; t2; t2 = t2->next)
  603. {
  604. if (!ancestor(t1, t2) && !ancestor(t2, t1)
  605. && t1->priority != t2->priority)
  606. {
  607. if (t1->priority > t2->priority)
  608. {
  609. /* Either t2 is scheduled before t1, but then it
  610. needs to be scheduled before some t dep finishes */
  611. /* One of the t1 deps to give the maximum start time for t2 */
  612. if (t1->depsn > 1)
  613. {
  614. for (i = 0; i < t1->depsn; i++)
  615. fprintf(output, " + t%lut%lud%d", t2->id, t1->id, i);
  616. fprintf(output, " = 1;\n");
  617. }
  618. for (i = 0; i < t1->depsn; i++)
  619. {
  620. fprintf(output, "c%lu - s%lu >= ", t1->deps[i].dep->id, t2->id);
  621. if (t1->depsn > 1)
  622. /* Only checks this when it's this dependency that is chosen */
  623. fprintf(output, "-2e5 + 1e5 t%lut%lud%d", t2->id, t1->id, i);
  624. else
  625. fprintf(output, "-1e5");
  626. /* Only check this if t1 is after t2 */
  627. fprintf(output, " + 1e5 t%luafter%lu", t1->id, t2->id);
  628. fprintf(output, ";\n");
  629. }
  630. /* Or t2 is scheduled after t1 is. */
  631. fprintf(output, "s%lu - s%lu >= -1e5 t%luafter%lu;\n", t2->id, t1->id, t1->id, t2->id);
  632. }
  633. else
  634. {
  635. /* Either t1 is scheduled before t2, but then it
  636. needs to be scheduled before some t2 dep finishes */
  637. /* One of the t2 deps to give the maximum start time for t1 */
  638. if (t2->depsn > 1)
  639. {
  640. for (i = 0; i < t2->depsn; i++)
  641. fprintf(output, " + t%lut%lud%d", t1->id, t2->id, i);
  642. fprintf(output, " = 1;\n");
  643. }
  644. for (i = 0; i < t2->depsn; i++)
  645. {
  646. fprintf(output, "c%lu - s%lu >= ", t2->deps[i].dep->id, t1->id);
  647. if (t2->depsn > 1)
  648. /* Only checks this when it's this dependency that is chosen */
  649. fprintf(output, "-1e5 + 1e5 t%lut%lud%d", t1->id, t2->id, i);
  650. /* Only check this if t2 is after t1 */
  651. fprintf(output, " - 1e5 t%luafter%lu;\n", t1->id, t2->id);
  652. }
  653. /* Or t1 is scheduled after t2 is. */
  654. fprintf(output, "s%lu - s%lu >= -1e5 + 1e5 t%luafter%lu;\n", t1->id, t2->id, t1->id, t2->id);
  655. }
  656. }
  657. }
  658. }
  659. }
  660. for (t1 = tasks; t1; t1 = t1->next)
  661. for (t2 = t1->next; t2; t2 = t2->next)
  662. if (!ancestor(t1, t2) && !ancestor(t2, t1))
  663. {
  664. fprintf(output, "bin t%luafter%lu;\n", t1->id, t2->id);
  665. if (recordprio && t1->priority != t2->priority)
  666. {
  667. if (t1->priority > t2->priority)
  668. {
  669. if (t1->depsn > 1)
  670. for (i = 0; i < t1->depsn; i++)
  671. fprintf(output, "bin t%lut%lud%d;\n", t2->id, t1->id, i);
  672. }
  673. else
  674. {
  675. if (t2->depsn > 1)
  676. for (i = 0; i < t2->depsn; i++)
  677. fprintf(output, "bin t%lut%lud%d;\n", t1->id, t2->id, i);
  678. }
  679. }
  680. }
  681. for (t1 = tasks; t1; t1 = t1->next)
  682. for (w = 0; w < nw; w++)
  683. fprintf(output, "bin t%luw%d;\n", t1->id, w);
  684. }
  685. else
  686. {
  687. struct bound_task_pool *tp;
  688. nt = 0;
  689. for (tp = task_pools; tp; tp = tp->next)
  690. nt++;
  691. {
  692. double times[nw*nt];
  693. _starpu_get_tasks_times(nw, nt, times);
  694. fprintf(output, "/* StarPU upper bound linear programming problem, to be run in lp_solve. */\n\n");
  695. fprintf(output, "/* We want to minimize total execution time (ms) */\n");
  696. fprintf(output, "min: tmax;\n\n");
  697. fprintf(output, "/* Which is the maximum of all worker execution times (ms) */\n");
  698. for (w = 0; w < nw; w++)
  699. {
  700. char name[32];
  701. starpu_worker_get_name(w, name, sizeof(name));
  702. fprintf(output, "/* worker %s */\n0", name);
  703. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  704. {
  705. if (!isnan(times[w*nt+t]))
  706. fprintf(output, "\t%+f * w%dt%dn", (float) times[w*nt+t], w, t);
  707. }
  708. fprintf(output, " <= tmax;\n");
  709. }
  710. fprintf(output, "\n");
  711. fprintf(output, "/* And we have to have computed exactly all tasks */\n");
  712. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  713. {
  714. int got_one = 0;
  715. fprintf(output, "/* task %s key %x */\n0", _starpu_codelet_get_model_name(tp->cl), (unsigned) tp->footprint);
  716. for (w = 0; w < nw; w++)
  717. {
  718. if (isnan(times[w*nt+t]))
  719. fprintf(stderr, "Warning: task %s has no performance measurement for worker %d.\n", _starpu_codelet_get_model_name(tp->cl), w);
  720. else
  721. {
  722. got_one = 1;
  723. fprintf(output, "\t+w%dt%dn", w, t);
  724. }
  725. }
  726. fprintf(output, " = %lu;\n", tp->n);
  727. if (!got_one)
  728. fprintf(stderr, "Warning: task %s has no performance measurement for any worker, system will not be solvable!\n", _starpu_codelet_get_model_name(tp->cl));
  729. /* Show actual values */
  730. fprintf(output, "/*");
  731. for (w = 0; w < nw; w++)
  732. fprintf(output, "\t+%lu", tp->cl->per_worker_stats[w]);
  733. fprintf(output, "\t*/\n\n");
  734. }
  735. fprintf(output, "/* Optionally tell that tasks can not be divided */\n");
  736. fprintf(output, "/* int ");
  737. int first = 1;
  738. for (w = 0; w < nw; w++)
  739. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  740. {
  741. if (!first)
  742. fprintf(output, ",");
  743. else
  744. first = 0;
  745. fprintf(output, "w%dt%dn", w, t);
  746. }
  747. fprintf(output, "; */\n");
  748. }
  749. }
  750. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  751. }
  752. /*
  753. * Print bound system in MPS output format
  754. */
  755. void starpu_bound_print_mps(FILE *output)
  756. {
  757. struct bound_task_pool * tp;
  758. int nt; /* Number of different kinds of tasks */
  759. int nw; /* Number of different workers */
  760. int t, w;
  761. if (recorddeps)
  762. {
  763. fprintf(output, "Not supported\n");
  764. return;
  765. }
  766. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  767. nw = starpu_worker_get_count();
  768. nt = 0;
  769. for (tp = task_pools; tp; tp = tp->next)
  770. nt++;
  771. {
  772. double times[nw*nt];
  773. _starpu_get_tasks_times(nw, nt, times);
  774. fprintf(output, "NAME StarPU theoretical bound\n");
  775. fprintf(output, "*\nROWS\n");
  776. fprintf(output, "* We want to minimize total execution time (ms)\n");
  777. fprintf(output, " N TMAX\n");
  778. fprintf(output, "* Which is the maximum of all worker execution times (ms)\n");
  779. for (w = 0; w < nw; w++)
  780. {
  781. char name[32];
  782. starpu_worker_get_name(w, name, sizeof(name));
  783. fprintf(output, "* worker %s\n", name);
  784. fprintf(output, " L W%d\n", w);
  785. }
  786. fprintf(output, "*\n* And we have to have computed exactly all tasks\n*\n");
  787. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  788. {
  789. fprintf(output, "* task %s key %x\n", _starpu_codelet_get_model_name(tp->cl), (unsigned) tp->footprint);
  790. fprintf(output, " E T%d\n", t);
  791. }
  792. fprintf(output, "*\nCOLUMNS\n*\n");
  793. fprintf(output, "*\n* Execution times and completion of all tasks\n*\n");
  794. for (w = 0; w < nw; w++)
  795. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  796. if (!isnan(times[w*nt+t]))
  797. {
  798. char name[9];
  799. snprintf(name, sizeof(name), "W%dT%d", w, t);
  800. fprintf(output," %-8s W%-7d %12f\n", name, w, times[w*nt+t]);
  801. fprintf(output," %-8s T%-7d %12d\n", name, t, 1);
  802. }
  803. fprintf(output, "*\n* Total execution time\n*\n");
  804. for (w = 0; w < nw; w++)
  805. fprintf(output," TMAX W%-2d %12d\n", w, -1);
  806. fprintf(output," TMAX TMAX %12d\n", 1);
  807. fprintf(output, "*\nRHS\n*\n");
  808. fprintf(output, "*\n* Total number of tasks\n*\n");
  809. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  810. fprintf(output," NT%-2d T%-7d %12lu\n", t, t, tp->n);
  811. fprintf(output, "ENDATA\n");
  812. }
  813. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  814. }
  815. /*
  816. * Solve bound system thanks to GNU Linear Programming Kit backend
  817. */
  818. #ifdef STARPU_HAVE_GLPK_H
  819. static glp_prob *_starpu_bound_glp_resolve(int integer)
  820. {
  821. struct bound_task_pool * tp;
  822. int nt; /* Number of different kinds of tasks */
  823. int nw; /* Number of different workers */
  824. int t, w;
  825. glp_prob *lp;
  826. int ret;
  827. nw = starpu_worker_get_count();
  828. nt = 0;
  829. for (tp = task_pools; tp; tp = tp->next)
  830. nt++;
  831. lp = glp_create_prob();
  832. glp_set_prob_name(lp, "StarPU theoretical bound");
  833. glp_set_obj_dir(lp, GLP_MIN);
  834. glp_set_obj_name(lp, "total execution time");
  835. {
  836. double times[nw*nt];
  837. int ne =
  838. nw * (nt+1) /* worker execution time */
  839. + nt * nw
  840. + 1; /* glp dumbness */
  841. int n = 1;
  842. int ia[ne], ja[ne];
  843. double ar[ne];
  844. _starpu_get_tasks_times(nw, nt, times);
  845. /* Variables: number of tasks i assigned to worker j, and tmax */
  846. glp_add_cols(lp, nw*nt+1);
  847. #define colnum(w, t) ((t)*nw+(w)+1)
  848. glp_set_obj_coef(lp, nw*nt+1, 1.);
  849. for (w = 0; w < nw; w++)
  850. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  851. {
  852. char name[32];
  853. snprintf(name, sizeof(name), "w%dt%dn", w, t);
  854. glp_set_col_name(lp, colnum(w, t), name);
  855. if (integer)
  856. glp_set_col_kind(lp, colnum(w, t), GLP_IV);
  857. glp_set_col_bnds(lp, colnum(w, t), GLP_LO, 0., 0.);
  858. }
  859. glp_set_col_bnds(lp, nw*nt+1, GLP_LO, 0., 0.);
  860. /* Total worker execution time */
  861. glp_add_rows(lp, nw);
  862. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  863. {
  864. int someone = 0;
  865. for (w = 0; w < nw; w++)
  866. if (!isnan(times[w*nt+t]))
  867. someone = 1;
  868. if (!someone)
  869. {
  870. /* This task does not have any performance model at all, abort */
  871. glp_delete_prob(lp);
  872. return NULL;
  873. }
  874. }
  875. for (w = 0; w < nw; w++)
  876. {
  877. char name[32], title[64];
  878. starpu_worker_get_name(w, name, sizeof(name));
  879. snprintf(title, sizeof(title), "worker %s", name);
  880. glp_set_row_name(lp, w+1, title);
  881. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  882. {
  883. ia[n] = w+1;
  884. ja[n] = colnum(w, t);
  885. if (isnan(times[w*nt+t]))
  886. ar[n] = 1000000000.;
  887. else
  888. ar[n] = times[w*nt+t];
  889. n++;
  890. }
  891. /* tmax */
  892. ia[n] = w+1;
  893. ja[n] = nw*nt+1;
  894. ar[n] = -1;
  895. n++;
  896. glp_set_row_bnds(lp, w+1, GLP_UP, 0, 0);
  897. }
  898. /* Total task completion */
  899. glp_add_rows(lp, nt);
  900. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  901. {
  902. char name[32], title[64];
  903. starpu_worker_get_name(w, name, sizeof(name));
  904. snprintf(title, sizeof(title), "task %s key %x", _starpu_codelet_get_model_name(tp->cl), (unsigned) tp->footprint);
  905. glp_set_row_name(lp, nw+t+1, title);
  906. for (w = 0; w < nw; w++)
  907. {
  908. ia[n] = nw+t+1;
  909. ja[n] = colnum(w, t);
  910. ar[n] = 1;
  911. n++;
  912. }
  913. glp_set_row_bnds(lp, nw+t+1, GLP_FX, tp->n, tp->n);
  914. }
  915. STARPU_ASSERT(n == ne);
  916. glp_load_matrix(lp, ne-1, ia, ja, ar);
  917. }
  918. glp_smcp parm;
  919. glp_init_smcp(&parm);
  920. parm.msg_lev = GLP_MSG_OFF;
  921. ret = glp_simplex(lp, &parm);
  922. if (ret)
  923. {
  924. glp_delete_prob(lp);
  925. lp = NULL;
  926. return NULL;
  927. }
  928. if (integer)
  929. {
  930. glp_iocp iocp;
  931. glp_init_iocp(&iocp);
  932. iocp.msg_lev = GLP_MSG_OFF;
  933. glp_intopt(lp, &iocp);
  934. }
  935. return lp;
  936. }
  937. #endif /* STARPU_HAVE_GLPK_H */
  938. /* Print the computed bound as well as the optimized distribution of tasks */
  939. void starpu_bound_print(FILE *output, int integer STARPU_ATTRIBUTE_UNUSED)
  940. {
  941. #ifdef STARPU_HAVE_GLPK_H
  942. if (recorddeps)
  943. {
  944. fprintf(output, "Not supported\n");
  945. return;
  946. }
  947. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  948. glp_prob *lp = _starpu_bound_glp_resolve(integer);
  949. if (lp)
  950. {
  951. struct bound_task_pool * tp;
  952. int t, w;
  953. int nw; /* Number of different workers */
  954. double tmax;
  955. nw = starpu_worker_get_count();
  956. if (integer)
  957. tmax = glp_mip_obj_val(lp);
  958. else
  959. tmax = glp_get_obj_val(lp);
  960. fprintf(output, "Theoretical minimum execution time: %f ms\n", tmax);
  961. for (t = 0, tp = task_pools; tp; t++, tp = tp->next)
  962. {
  963. fprintf(output, "%s key %x\n", _starpu_codelet_get_model_name(tp->cl), (unsigned) tp->footprint);
  964. for (w = 0; w < nw; w++)
  965. if (integer)
  966. fprintf(output, "\tw%dt%dn %f", w, t, glp_mip_col_val(lp, colnum(w, t)));
  967. else
  968. fprintf(output, "\tw%dt%dn %f", w, t, glp_get_col_prim(lp, colnum(w, t)));
  969. fprintf(output, "\n");
  970. }
  971. glp_delete_prob(lp);
  972. }
  973. else
  974. {
  975. fprintf(stderr, "Simplex failed\n");
  976. }
  977. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  978. #else /* STARPU_HAVE_GLPK_H */
  979. fprintf(output, "Please rebuild StarPU with glpk installed.\n");
  980. #endif /* STARPU_HAVE_GLPK_H */
  981. }
  982. /* Compute and return the bound */
  983. void starpu_bound_compute(double *res, double *integer_res STARPU_ATTRIBUTE_UNUSED, int integer STARPU_ATTRIBUTE_UNUSED)
  984. {
  985. #ifdef STARPU_HAVE_GLPK_H
  986. double ret;
  987. if (recorddeps)
  988. {
  989. *res = 0.;
  990. return;
  991. }
  992. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  993. glp_prob *lp = _starpu_bound_glp_resolve(integer);
  994. if (lp)
  995. {
  996. ret = glp_get_obj_val(lp);
  997. if (integer)
  998. *integer_res = glp_mip_obj_val(lp);
  999. glp_delete_prob(lp);
  1000. }
  1001. else
  1002. ret = 0.;
  1003. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1004. *res = ret;
  1005. #else /* STARPU_HAVE_GLPK_H */
  1006. *res = 0.;
  1007. #endif /* STARPU_HAVE_GLPK_H */
  1008. }