sc_hypervisor.c 32 KB

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