sc_hypervisor.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <sc_hypervisor_intern.h>
  17. #include <sc_hypervisor_policy.h>
  18. #include <common/uthash.h>
  19. #include <starpu_config.h>
  20. unsigned imposed_resize = 0;
  21. unsigned type_of_tasks_known = 0;
  22. struct starpu_sched_ctx_performance_counters* perf_counters = NULL;
  23. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time);
  24. static void notify_pushed_task(unsigned sched_ctx, int worker);
  25. static void notify_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, size_t data_size, uint32_t footprint);
  26. static void notify_post_exec_hook(unsigned sched_ctx, int taskid);
  27. static void notify_idle_end(unsigned sched_ctx, int worker);
  28. static void notify_submitted_job(struct starpu_task *task, unsigned footprint);
  29. static void notify_delete_context(unsigned sched_ctx);
  30. extern struct sc_hypervisor_policy idle_policy;
  31. extern struct sc_hypervisor_policy app_driven_policy;
  32. extern struct sc_hypervisor_policy gflops_rate_policy;
  33. #ifdef STARPU_HAVE_GLPK_H
  34. extern struct sc_hypervisor_policy feft_lp_policy;
  35. extern struct sc_hypervisor_policy teft_lp_policy;
  36. extern struct sc_hypervisor_policy ispeed_lp_policy;
  37. extern struct sc_hypervisor_policy debit_lp_policy;
  38. #endif // STARPU_HAVE_GLPK_
  39. extern struct sc_hypervisor_policy ispeed_policy;
  40. static struct sc_hypervisor_policy *predefined_policies[] =
  41. {
  42. &idle_policy,
  43. &app_driven_policy,
  44. #ifdef STARPU_HAVE_GLPK_H
  45. &feft_lp_policy,
  46. &teft_lp_policy,
  47. &ispeed_lp_policy,
  48. &debit_lp_policy,
  49. #endif // STARPU_HAVE_GLPK_H
  50. &gflops_rate_policy,
  51. &ispeed_policy
  52. };
  53. static void _load_hypervisor_policy(struct sc_hypervisor_policy *policy)
  54. {
  55. STARPU_ASSERT(policy);
  56. hypervisor.policy.name = policy->name;
  57. hypervisor.policy.size_ctxs = policy->size_ctxs;
  58. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  59. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  60. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  61. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  62. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  63. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  64. hypervisor.policy.end_ctx = policy->end_ctx;
  65. }
  66. static struct sc_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  67. {
  68. if (!policy_name)
  69. return NULL;
  70. unsigned i;
  71. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  72. {
  73. struct sc_hypervisor_policy *p;
  74. p = predefined_policies[i];
  75. if (p->name)
  76. {
  77. if (strcmp(policy_name, p->name) == 0) {
  78. /* we found a policy with the requested name */
  79. return p;
  80. }
  81. }
  82. }
  83. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  84. /* nothing was found */
  85. return NULL;
  86. }
  87. static struct sc_hypervisor_policy *_select_hypervisor_policy(struct sc_hypervisor_policy* hypervisor_policy)
  88. {
  89. struct sc_hypervisor_policy *selected_policy = NULL;
  90. if(hypervisor_policy && hypervisor_policy->custom)
  91. return hypervisor_policy;
  92. /* we look if the application specified the name of a policy to load */
  93. const char *policy_name;
  94. if (hypervisor_policy && hypervisor_policy->name)
  95. {
  96. policy_name = hypervisor_policy->name;
  97. }
  98. else
  99. {
  100. policy_name = getenv("HYPERVISOR_POLICY");
  101. }
  102. if (policy_name)
  103. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  104. /* Perhaps there was no policy that matched the name */
  105. if (selected_policy)
  106. return selected_policy;
  107. /* If no policy was specified, we use the idle policy as a default */
  108. return &idle_policy;
  109. }
  110. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  111. struct starpu_sched_ctx_performance_counters* sc_hypervisor_init(struct sc_hypervisor_policy *hypervisor_policy)
  112. {
  113. hypervisor.min_tasks = 0;
  114. hypervisor.nsched_ctxs = 0;
  115. char* vel_gap = getenv("MAX_VELOCITY_GAP");
  116. hypervisor.max_velocity_gap = vel_gap ? atof(vel_gap) : SC_VELOCITY_MAX_GAP_DEFAULT;
  117. char* crit = getenv("HYPERVISOR_TRIGGER_RESIZE");
  118. hypervisor.resize_criteria = !crit ? SC_NOTHING : strcmp(crit,"idle") == 0 ? SC_IDLE : (strcmp(crit,"speed") == 0 ? SC_VELOCITY : SC_NOTHING);
  119. starpu_pthread_mutex_init(&act_hypervisor_mutex, NULL);
  120. hypervisor.start_executing_time = starpu_timing_now();
  121. int i;
  122. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  123. {
  124. hypervisor.resize[i] = 0;
  125. hypervisor.allow_remove[i] = 1;
  126. hypervisor.configurations[i] = NULL;
  127. hypervisor.sr = NULL;
  128. hypervisor.check_min_tasks[i] = 1;
  129. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  130. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  131. hypervisor.sched_ctx_w[i].config = NULL;
  132. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  133. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  134. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  135. hypervisor.sched_ctx_w[i].start_time = 0.0;
  136. hypervisor.sched_ctx_w[i].real_start_time = 0.0;
  137. hypervisor.sched_ctx_w[i].resize_ack.receiver_sched_ctx = -1;
  138. hypervisor.sched_ctx_w[i].resize_ack.moved_workers = NULL;
  139. hypervisor.sched_ctx_w[i].resize_ack.nmoved_workers = 0;
  140. hypervisor.sched_ctx_w[i].resize_ack.acked_workers = NULL;
  141. starpu_pthread_mutex_init(&hypervisor.sched_ctx_w[i].mutex, NULL);
  142. int j;
  143. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  144. {
  145. hypervisor.sched_ctx_w[i].current_idle_time[j] = 0.0;
  146. hypervisor.sched_ctx_w[i].pushed_tasks[j] = 0;
  147. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  148. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  149. hypervisor.sched_ctx_w[i].elapsed_data[j] = 0;
  150. hypervisor.sched_ctx_w[i].elapsed_tasks[j] = 0;
  151. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  152. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  153. hypervisor.sched_ctx_w[i].ref_velocity[j] = -1.0;
  154. }
  155. }
  156. struct sc_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  157. _load_hypervisor_policy(selected_hypervisor_policy);
  158. perf_counters = (struct starpu_sched_ctx_performance_counters*)malloc(sizeof(struct starpu_sched_ctx_performance_counters));
  159. perf_counters->notify_idle_cycle = notify_idle_cycle;
  160. perf_counters->notify_pushed_task = notify_pushed_task;
  161. perf_counters->notify_poped_task = notify_poped_task;
  162. perf_counters->notify_post_exec_hook = notify_post_exec_hook;
  163. perf_counters->notify_idle_end = notify_idle_end;
  164. perf_counters->notify_submitted_job = notify_submitted_job;
  165. perf_counters->notify_delete_context = notify_delete_context;
  166. starpu_sched_ctx_notify_hypervisor_exists();
  167. return perf_counters;
  168. }
  169. const char* sc_hypervisor_get_policy()
  170. {
  171. return hypervisor.policy.name;
  172. }
  173. /* the user can forbid the resizing process*/
  174. void sc_hypervisor_stop_resize(unsigned sched_ctx)
  175. {
  176. imposed_resize = 1;
  177. hypervisor.resize[sched_ctx] = 0;
  178. }
  179. /* the user can restart the resizing process*/
  180. void sc_hypervisor_start_resize(unsigned sched_ctx)
  181. {
  182. imposed_resize = 1;
  183. hypervisor.resize[sched_ctx] = 1;
  184. }
  185. static void _print_current_time()
  186. {
  187. if(!getenv("HYPERVISOR_STOP_PRINT"))
  188. {
  189. double curr_time = starpu_timing_now();
  190. double elapsed_time = (curr_time - hypervisor.start_executing_time) / 1000000.0; /* in seconds */
  191. fprintf(stdout, "Time: %lf\n", elapsed_time);
  192. int i;
  193. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  194. {
  195. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  196. {
  197. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  198. double cpu_speed = sc_hypervisor_get_velocity(sc_w, STARPU_CPU_WORKER);
  199. double cuda_speed = sc_hypervisor_get_velocity(sc_w, STARPU_CUDA_WORKER);
  200. int ncpus = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CPU_WORKER);
  201. int ncuda = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CUDA_WORKER);
  202. fprintf(stdout, "%d: cpu_v = %lf cuda_v = %lf ncpus = %d ncuda = %d\n", hypervisor.sched_ctxs[i], cpu_speed, cuda_speed, ncpus, ncuda);
  203. }
  204. }
  205. }
  206. return;
  207. }
  208. void sc_hypervisor_shutdown(void)
  209. {
  210. // printf("shutdown\n");
  211. int i;
  212. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  213. {
  214. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  215. {
  216. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  217. sc_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  218. starpu_pthread_mutex_destroy(&hypervisor.sched_ctx_w[i].mutex);
  219. }
  220. }
  221. perf_counters->notify_idle_cycle = NULL;
  222. perf_counters->notify_pushed_task = NULL;
  223. perf_counters->notify_poped_task = NULL;
  224. perf_counters->notify_post_exec_hook = NULL;
  225. perf_counters->notify_idle_end = NULL;
  226. perf_counters->notify_delete_context = NULL;
  227. free(perf_counters);
  228. perf_counters = NULL;
  229. starpu_pthread_mutex_destroy(&act_hypervisor_mutex);
  230. }
  231. /* the hypervisor is in charge only of the contexts registered to it*/
  232. void sc_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
  233. {
  234. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  235. hypervisor.configurations[sched_ctx] = NULL;
  236. hypervisor.resize_requests[sched_ctx] = NULL;
  237. starpu_pthread_mutex_init(&hypervisor.conf_mut[sched_ctx], NULL);
  238. starpu_pthread_mutex_init(&hypervisor.resize_mut[sched_ctx], NULL);
  239. _add_config(sched_ctx);
  240. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = sched_ctx;
  241. hypervisor.sched_ctxs[hypervisor.nsched_ctxs++] = sched_ctx;
  242. hypervisor.sched_ctx_w[sched_ctx].total_flops = total_flops;
  243. hypervisor.sched_ctx_w[sched_ctx].remaining_flops = total_flops;
  244. if(strcmp(hypervisor.policy.name, "app_driven") == 0)
  245. hypervisor.resize[sched_ctx] = 1;
  246. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  247. }
  248. static int _get_first_free_sched_ctx(int *sched_ctxs, int nsched_ctxs)
  249. {
  250. int i;
  251. for(i = 0; i < nsched_ctxs; i++)
  252. if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
  253. return i;
  254. return STARPU_NMAX_SCHED_CTXS;
  255. }
  256. /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}
  257. and have instead {5, 7, MAXVAL, MAXVAL, MAXVAL}
  258. it is easier afterwards to iterate the array
  259. */
  260. static void _rearange_sched_ctxs(int *sched_ctxs, int old_nsched_ctxs)
  261. {
  262. int first_free_id = STARPU_NMAX_SCHED_CTXS;
  263. int i;
  264. for(i = 0; i < old_nsched_ctxs; i++)
  265. {
  266. if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  267. {
  268. first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
  269. if(first_free_id != STARPU_NMAX_SCHED_CTXS)
  270. {
  271. sched_ctxs[first_free_id] = sched_ctxs[i];
  272. sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  273. }
  274. }
  275. }
  276. }
  277. /* unregistered contexts will no longer be resized */
  278. void sc_hypervisor_unregister_ctx(unsigned sched_ctx)
  279. {
  280. if(hypervisor.policy.end_ctx)
  281. hypervisor.policy.end_ctx(sched_ctx);
  282. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  283. unsigned i;
  284. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  285. {
  286. if(hypervisor.sched_ctxs[i] == (int)sched_ctx)
  287. {
  288. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  289. break;
  290. }
  291. }
  292. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  293. hypervisor.nsched_ctxs--;
  294. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  295. _remove_config(sched_ctx);
  296. /* free(hypervisor.configurations[sched_ctx]); */
  297. /* free(hypervisor.resize_requests[sched_ctx]); */
  298. starpu_pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
  299. starpu_pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
  300. if(hypervisor.nsched_ctxs == 1)
  301. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  302. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  303. }
  304. static double _get_best_total_elapsed_flops(struct sc_hypervisor_wrapper* sc_w, int *npus, enum starpu_worker_archtype req_arch)
  305. {
  306. double ret_val = 0.0;
  307. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  308. int worker;
  309. struct starpu_sched_ctx_iterator it;
  310. if(workers->init_iterator)
  311. workers->init_iterator(workers, &it);
  312. while(workers->has_next(workers, &it))
  313. {
  314. worker = workers->get_next(workers, &it);
  315. enum starpu_worker_archtype arch = starpu_worker_get_type(worker);
  316. if(arch == req_arch)
  317. {
  318. if(sc_w->total_elapsed_flops[worker] > ret_val)
  319. ret_val = sc_w->total_elapsed_flops[worker];
  320. (*npus)++;
  321. }
  322. }
  323. return ret_val;
  324. }
  325. double _get_max_velocity_gap()
  326. {
  327. return hypervisor.max_velocity_gap;
  328. }
  329. unsigned sc_hypervisor_get_resize_criteria()
  330. {
  331. return hypervisor.resize_criteria;
  332. }
  333. /* compute an average value of the cpu/cuda velocity */
  334. double sc_hypervisorsc_hypervisor_get_velocity_per_worker_type(struct sc_hypervisor_wrapper* sc_w, enum starpu_worker_archtype arch)
  335. {
  336. int npus = 0;
  337. double elapsed_flops = _get_best_total_elapsed_flops(sc_w, &npus, arch) / 1000000000.0 ; /* in gflops */
  338. if(npus == 0)
  339. return -1.0;
  340. if( elapsed_flops != 0.0)
  341. {
  342. double curr_time = starpu_timing_now();
  343. double elapsed_time = (curr_time - sc_w->real_start_time) / 1000000.0; /* in seconds */
  344. double velocity = (elapsed_flops/elapsed_time); /* in Gflops/s */
  345. return velocity;
  346. }
  347. return -1.0;
  348. }
  349. /* compute an average value of the cpu/cuda old velocity */
  350. double sc_hypervisor_get_ref_velocity_per_worker_type(struct sc_hypervisor_wrapper* sc_w, enum starpu_worker_archtype arch)
  351. {
  352. double ref_velocity = 0.0;
  353. unsigned nw = 0;
  354. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  355. int worker;
  356. struct starpu_sched_ctx_iterator it;
  357. if(workers->init_iterator)
  358. workers->init_iterator(workers, &it);
  359. while(workers->has_next(workers, &it))
  360. {
  361. worker = workers->get_next(workers, &it);
  362. if(sc_w->ref_velocity[worker] > 1.0)
  363. {
  364. ref_velocity += sc_w->ref_velocity[worker];
  365. nw++;
  366. }
  367. }
  368. if(nw > 0)
  369. return ref_velocity / nw;
  370. return -1.0;
  371. }
  372. static int get_ntasks( int *tasks)
  373. {
  374. int ntasks = 0;
  375. int j;
  376. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  377. {
  378. ntasks += tasks[j];
  379. }
  380. return ntasks;
  381. }
  382. static void _get_cpus(int *workers, int nworkers, int *cpus, int *ncpus)
  383. {
  384. int i, worker;
  385. *ncpus = 0;
  386. for(i = 0; i < nworkers; i++)
  387. {
  388. worker = workers[i];
  389. enum starpu_worker_archtype arch = starpu_worker_get_type(worker);
  390. if(arch == STARPU_CPU_WORKER)
  391. cpus[(*ncpus)++] = worker;
  392. }
  393. }
  394. int sc_hypervisor_get_nworkers_ctx(unsigned sched_ctx, enum starpu_worker_archtype arch)
  395. {
  396. int nworkers_ctx = 0;
  397. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  398. int worker;
  399. struct starpu_sched_ctx_iterator it;
  400. if(workers->init_iterator)
  401. workers->init_iterator(workers, &it);
  402. while(workers->has_next(workers, &it))
  403. {
  404. worker = workers->get_next(workers, &it);
  405. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  406. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  407. nworkers_ctx++;
  408. }
  409. return nworkers_ctx;
  410. }
  411. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  412. {
  413. int i;
  414. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  415. {
  416. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  417. if(val == 0)
  418. {
  419. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[i] = 0;
  420. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[i] = 0;
  421. }
  422. }
  423. }
  424. double sc_hypervisor_get_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  425. {
  426. double ret_val = 0.0;
  427. int i;
  428. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  429. ret_val += sc_w->elapsed_flops[i];
  430. return ret_val;
  431. }
  432. double sc_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  433. {
  434. double ret_val = 0.0;
  435. int i;
  436. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  437. ret_val += sc_w->total_elapsed_flops[i];
  438. return ret_val;
  439. }
  440. void _reset_resize_sample_info(unsigned sender_sched_ctx, unsigned receiver_sched_ctx)
  441. {
  442. /* info concerning only the gflops_rate strateg */
  443. struct sc_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  444. struct sc_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  445. double start_time = starpu_timing_now();
  446. sender_sc_w->start_time = start_time;
  447. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  448. receiver_sc_w->start_time = start_time;
  449. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  450. }
  451. /* actually move the workers: the cpus are moved, gpus are only shared */
  452. /* forbids another resize request before this one is take into account */
  453. void sc_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  454. {
  455. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])
  456. {
  457. _print_current_time();
  458. unsigned j;
  459. printf("resize ctx %d with %d workers", sender_sched_ctx, nworkers_to_move);
  460. for(j = 0; j < nworkers_to_move; j++)
  461. printf(" %d", workers_to_move[j]);
  462. printf("\n");
  463. starpu_trace_user_event(1);
  464. hypervisor.allow_remove[receiver_sched_ctx] = 0;
  465. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  466. if(now)
  467. {
  468. unsigned j;
  469. printf("remove now from ctx %d:", sender_sched_ctx);
  470. for(j = 0; j < nworkers_to_move; j++)
  471. printf(" %d", workers_to_move[j]);
  472. printf("\n");
  473. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  474. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  475. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  476. }
  477. else
  478. {
  479. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  480. if(ret != EBUSY)
  481. {
  482. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  483. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  484. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  485. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  486. unsigned i;
  487. for(i = 0; i < nworkers_to_move; i++)
  488. {
  489. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  490. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  491. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  492. }
  493. hypervisor.resize[sender_sched_ctx] = 0;
  494. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  495. }
  496. }
  497. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(receiver_sched_ctx);
  498. unsigned i;
  499. for(i = 0; i < nworkers_to_move; i++)
  500. new_config->max_idle[workers_to_move[i]] = new_config->max_idle[workers_to_move[i]] !=MAX_IDLE_TIME ? new_config->max_idle[workers_to_move[i]] : new_config->new_workers_max_idle;
  501. }
  502. return;
  503. }
  504. void sc_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  505. {
  506. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  507. {
  508. _print_current_time();
  509. unsigned j;
  510. printf("add to ctx %d:", sched_ctx);
  511. for(j = 0; j < nworkers_to_add; j++)
  512. printf(" %d", workers_to_add[j]);
  513. printf("\n");
  514. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  515. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(sched_ctx);
  516. unsigned i;
  517. for(i = 0; i < nworkers_to_add; i++)
  518. new_config->max_idle[workers_to_add[i]] = new_config->max_idle[workers_to_add[i]] != MAX_IDLE_TIME ? new_config->max_idle[workers_to_add[i]] : new_config->new_workers_max_idle;
  519. }
  520. return;
  521. }
  522. unsigned sc_hypervisor_can_resize(unsigned sched_ctx)
  523. {
  524. return hypervisor.resize[sched_ctx];
  525. }
  526. void sc_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  527. {
  528. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx] && hypervisor.allow_remove[sched_ctx])
  529. {
  530. _print_current_time();
  531. unsigned nworkers = 0;
  532. int workers[nworkers_to_remove];
  533. if(now)
  534. {
  535. unsigned j;
  536. printf("remove explicitley now from ctx %d:", sched_ctx);
  537. for(j = 0; j < nworkers_to_remove; j++)
  538. printf(" %d", workers_to_remove[j]);
  539. printf("\n");
  540. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  541. }
  542. else
  543. {
  544. printf("try to remove from ctx %d: ", sched_ctx);
  545. unsigned j;
  546. for(j = 0; j < nworkers_to_remove; j++)
  547. printf(" %d", workers_to_remove[j]);
  548. printf("\n");
  549. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  550. if(ret != EBUSY)
  551. {
  552. unsigned i;
  553. for(i = 0; i < nworkers_to_remove; i++)
  554. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  555. workers[nworkers++] = workers_to_remove[i];
  556. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  557. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  558. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = (int)nworkers;
  559. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  560. for(i = 0; i < nworkers; i++)
  561. {
  562. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  563. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  564. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  565. }
  566. hypervisor.resize[sched_ctx] = 0;
  567. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  568. }
  569. }
  570. }
  571. return;
  572. }
  573. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  574. {
  575. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  576. return 0;
  577. struct sc_hypervisor_resize_ack *resize_ack = NULL;
  578. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  579. int i;
  580. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  581. {
  582. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  583. {
  584. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  585. starpu_pthread_mutex_lock(&sc_w->mutex);
  586. unsigned only_remove = 0;
  587. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != (int)sched_ctx &&
  588. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  589. {
  590. int j;
  591. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  592. if(sc_w->resize_ack.moved_workers[j] == worker)
  593. {
  594. only_remove = 1;
  595. break;
  596. }
  597. }
  598. if(only_remove ||
  599. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == (int)sched_ctx))
  600. {
  601. resize_ack = &sc_w->resize_ack;
  602. sender_sched_ctx = hypervisor.sched_ctxs[i];
  603. starpu_pthread_mutex_unlock(&sc_w->mutex);
  604. break;
  605. }
  606. starpu_pthread_mutex_unlock(&sc_w->mutex);
  607. }
  608. }
  609. /* if there is no ctx waiting for its ack return 1*/
  610. if(resize_ack == NULL)
  611. return 1;
  612. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  613. if(ret != EBUSY)
  614. {
  615. int *moved_workers = resize_ack->moved_workers;
  616. int nmoved_workers = resize_ack->nmoved_workers;
  617. int *acked_workers = resize_ack->acked_workers;
  618. if(worker != -1)
  619. {
  620. for(i = 0; i < nmoved_workers; i++)
  621. {
  622. int moved_worker = moved_workers[i];
  623. if(moved_worker == worker && acked_workers[i] == 0)
  624. {
  625. acked_workers[i] = 1;
  626. }
  627. }
  628. }
  629. int nacked_workers = 0;
  630. for(i = 0; i < nmoved_workers; i++)
  631. {
  632. nacked_workers += (acked_workers[i] == 1);
  633. }
  634. unsigned resize_completed = (nacked_workers == nmoved_workers);
  635. int receiver_sched_ctx = sched_ctx;
  636. if(resize_completed)
  637. {
  638. /* if the permission to resize is not allowed by the user don't do it
  639. whatever the application says */
  640. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  641. {
  642. /* int j; */
  643. /* printf("remove after ack from ctx %d:", sender_sched_ctx); */
  644. /* for(j = 0; j < nmoved_workers; j++) */
  645. /* printf(" %d", moved_workers[j]); */
  646. /* printf("\n"); */
  647. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  648. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  649. hypervisor.resize[sender_sched_ctx] = 1;
  650. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  651. /* if the user allowed resizing leave the decisions to the application */
  652. if(imposed_resize) imposed_resize = 0;
  653. resize_ack->receiver_sched_ctx = -1;
  654. resize_ack->nmoved_workers = 0;
  655. free(resize_ack->moved_workers);
  656. free(resize_ack->acked_workers);
  657. }
  658. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  659. return resize_completed;
  660. }
  661. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  662. }
  663. return 0;
  664. }
  665. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  666. * 'task_tag' tasks of 'sched_ctx' complete. */
  667. void sc_hypervisor_resize(unsigned sched_ctx, int task_tag)
  668. {
  669. struct resize_request_entry *entry;
  670. entry = malloc(sizeof *entry);
  671. STARPU_ASSERT(entry != NULL);
  672. entry->sched_ctx = sched_ctx;
  673. entry->task_tag = task_tag;
  674. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  675. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  676. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  677. }
  678. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  679. static void notify_idle_end(unsigned sched_ctx, int worker)
  680. {
  681. if(hypervisor.resize[sched_ctx])
  682. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  683. if(hypervisor.policy.handle_idle_end)
  684. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  685. }
  686. /* notifies the hypervisor that the worker spent another cycle in idle time */
  687. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  688. {
  689. if(hypervisor.resize[sched_ctx])
  690. {
  691. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  692. sc_w->current_idle_time[worker] += idle_time;
  693. if(hypervisor.policy.handle_idle_cycle)
  694. {
  695. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  696. }
  697. }
  698. return;
  699. }
  700. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  701. static void notify_pushed_task(unsigned sched_ctx, int worker)
  702. {
  703. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  704. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  705. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  706. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].real_start_time == 0.0)
  707. hypervisor.sched_ctx_w[sched_ctx].real_start_time = starpu_timing_now();
  708. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  709. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  710. {
  711. hypervisor.resize[sched_ctx] = 1;
  712. if(imposed_resize) imposed_resize = 0;
  713. hypervisor.check_min_tasks[sched_ctx] = 0;
  714. }
  715. if(hypervisor.policy.handle_pushed_task)
  716. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  717. }
  718. /* notifies the hypervisor that a task was poped from the queue of the worker */
  719. static void notify_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, size_t data_size, uint32_t footprint)
  720. {
  721. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  722. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += task->flops;
  723. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  724. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  725. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += task->flops;
  726. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= task->flops; //sc_hypervisor_get_elapsed_flops_per_sched_ctx(&hypervisor.sched_ctx_w[sched_ctx]);
  727. if(hypervisor.resize[sched_ctx])
  728. {
  729. if(hypervisor.policy.handle_poped_task)
  730. hypervisor.policy.handle_poped_task(sched_ctx, worker, task, footprint);
  731. }
  732. _ack_resize_completed(sched_ctx, worker);
  733. if(hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker] % 200 == 0)
  734. _print_current_time();
  735. }
  736. /* notifies the hypervisor that a tagged task has just been executed */
  737. static void notify_post_exec_hook(unsigned sched_ctx, int task_tag)
  738. {
  739. STARPU_ASSERT(task_tag > 0);
  740. unsigned conf_sched_ctx;
  741. unsigned i;
  742. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  743. unsigned ns = hypervisor.nsched_ctxs;
  744. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  745. for(i = 0; i < ns; i++)
  746. {
  747. struct configuration_entry *entry;
  748. conf_sched_ctx = hypervisor.sched_ctxs[i];
  749. starpu_pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  750. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  751. if (entry != NULL)
  752. {
  753. struct sc_hypervisor_policy_config *config = entry->configuration;
  754. sc_hypervisor_set_config(conf_sched_ctx, config);
  755. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  756. free(config);
  757. }
  758. starpu_pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  759. }
  760. if(hypervisor.resize[sched_ctx])
  761. {
  762. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  763. if(hypervisor.policy.handle_post_exec_hook)
  764. {
  765. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  766. struct resize_request_entry *entry;
  767. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  768. if (entry != NULL)
  769. {
  770. hypervisor.policy.handle_post_exec_hook(sched_ctx, task_tag);
  771. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  772. free(entry);
  773. }
  774. }
  775. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  776. }
  777. return;
  778. }
  779. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint)
  780. {
  781. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  782. hypervisor.sched_ctx_w[task->sched_ctx].submitted_flops += task->flops;
  783. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  784. if(hypervisor.policy.handle_submitted_job && !type_of_tasks_known)
  785. hypervisor.policy.handle_submitted_job(task->cl, task->sched_ctx, footprint);
  786. }
  787. void sc_hypervisor_set_type_of_task(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint)
  788. {
  789. type_of_tasks_known = 1;
  790. if(hypervisor.policy.handle_submitted_job)
  791. hypervisor.policy.handle_submitted_job(cl, sched_ctx, footprint);
  792. }
  793. static void notify_delete_context(unsigned sched_ctx)
  794. {
  795. _print_current_time();
  796. sc_hypervisor_unregister_ctx(sched_ctx);
  797. }
  798. void sc_hypervisor_size_ctxs(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  799. {
  800. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  801. unsigned curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : nsched_ctxs;
  802. int *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  803. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  804. unsigned s;
  805. for(s = 0; s < curr_nsched_ctxs; s++)
  806. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  807. if(hypervisor.policy.size_ctxs)
  808. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  809. }
  810. struct sc_hypervisor_wrapper* sc_hypervisor_get_wrapper(unsigned sched_ctx)
  811. {
  812. return &hypervisor.sched_ctx_w[sched_ctx];
  813. }
  814. int* sc_hypervisor_get_sched_ctxs()
  815. {
  816. return hypervisor.sched_ctxs;
  817. }
  818. int sc_hypervisor_get_nsched_ctxs()
  819. {
  820. int ns;
  821. ns = hypervisor.nsched_ctxs;
  822. return ns;
  823. }
  824. void sc_hypervisor_save_size_req(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  825. {
  826. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  827. hypervisor.sr->sched_ctxs = sched_ctxs;
  828. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  829. hypervisor.sr->workers = workers;
  830. hypervisor.sr->nworkers = nworkers;
  831. }
  832. unsigned sc_hypervisor_get_size_req(int **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  833. {
  834. if(hypervisor.sr != NULL)
  835. {
  836. *sched_ctxs = hypervisor.sr->sched_ctxs;
  837. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  838. *workers = hypervisor.sr->workers;
  839. *nworkers = hypervisor.sr->nworkers;
  840. return 1;
  841. }
  842. return 0;
  843. }
  844. void sc_hypervisor_free_size_req(void)
  845. {
  846. if(hypervisor.sr != NULL)
  847. {
  848. free(hypervisor.sr);
  849. hypervisor.sr = NULL;
  850. }
  851. }
  852. double sc_hypervisor_get_velocity(struct sc_hypervisor_wrapper *sc_w, enum starpu_worker_archtype arch)
  853. {
  854. double velocity = sc_hypervisorsc_hypervisor_get_velocity_per_worker_type(sc_w, arch);
  855. if(velocity == -1.0)
  856. velocity = sc_hypervisor_get_ref_velocity_per_worker_type(sc_w, arch);
  857. if(velocity == -1.0)
  858. velocity = arch == STARPU_CPU_WORKER ? 5.0 : 100.0;
  859. return velocity;
  860. }