gflops_rate_policy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_policy.h"
  17. static double _get_total_elapsed_flops_per_sched_ctx(unsigned sched_ctx)
  18. {
  19. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  20. double ret_val = 0.0;
  21. int i;
  22. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  23. ret_val += sc_w->total_elapsed_flops[i];
  24. return ret_val;
  25. }
  26. double _get_exp_end(unsigned sched_ctx)
  27. {
  28. struct sc_hypervisor_wrapper *sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  29. double elapsed_flops = sc_hypervisor_get_elapsed_flops_per_sched_ctx(sc_w);
  30. if( elapsed_flops >= 1.0)
  31. {
  32. double curr_time = starpu_timing_now();
  33. double elapsed_time = curr_time - sc_w->start_time;
  34. double exp_end = (elapsed_time * sc_w->remaining_flops / elapsed_flops) + curr_time;
  35. return exp_end;
  36. }
  37. return -1.0;
  38. }
  39. /* computes the instructions left to be executed out of the total instructions to execute */
  40. double _get_flops_left_pct(unsigned sched_ctx)
  41. {
  42. struct sc_hypervisor_wrapper *wrapper = sc_hypervisor_get_wrapper(sched_ctx);
  43. double total_elapsed_flops = _get_total_elapsed_flops_per_sched_ctx(sched_ctx);
  44. if(wrapper->total_flops == total_elapsed_flops || total_elapsed_flops > wrapper->total_flops)
  45. return 0.0;
  46. return (wrapper->total_flops - total_elapsed_flops)/wrapper->total_flops;
  47. }
  48. /* select the workers needed to be moved in order to force the sender and the receiver context to finish simultaneously */
  49. static int* _get_workers_to_move(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int *nworkers)
  50. {
  51. struct sc_hypervisor_wrapper* sender_sc_w = sc_hypervisor_get_wrapper(sender_sched_ctx);
  52. struct sc_hypervisor_wrapper* receiver_sc_w = sc_hypervisor_get_wrapper(receiver_sched_ctx);
  53. int *workers = NULL;
  54. double v_receiver = sc_hypervisor_get_ctx_speed(receiver_sc_w);
  55. double receiver_remainig_flops = receiver_sc_w->remaining_flops;
  56. double sender_exp_end = _get_exp_end(sender_sched_ctx);
  57. double sender_v_cpu = sc_hypervisor_get_speed_per_worker_type(sender_sc_w, STARPU_CPU_WORKER);
  58. double v_for_rctx = (receiver_remainig_flops/(sender_exp_end - starpu_timing_now())) - v_receiver;
  59. int nworkers_needed = v_for_rctx/sender_v_cpu;
  60. /* printf("%d->%d: v_rec %lf v %lf v_cpu %lf w_needed %d \n", sender_sched_ctx, receiver_sched_ctx, */
  61. /* v_receiver, v_for_rctx, sender_v_cpu, nworkers_needed); */
  62. if(nworkers_needed > 0)
  63. {
  64. struct sc_hypervisor_policy_config *sender_config = sc_hypervisor_get_config(sender_sched_ctx);
  65. int potential_moving_cpus = sc_hypervisor_get_movable_nworkers(sender_config, sender_sched_ctx, STARPU_CPU_WORKER);
  66. int potential_moving_gpus = sc_hypervisor_get_movable_nworkers(sender_config, sender_sched_ctx, STARPU_CUDA_WORKER);
  67. int sender_nworkers = (int)starpu_sched_ctx_get_nworkers(sender_sched_ctx);
  68. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(receiver_sched_ctx);
  69. int nworkers_ctx = (int)starpu_sched_ctx_get_nworkers(receiver_sched_ctx);
  70. if(nworkers_needed < (potential_moving_cpus + 5 * potential_moving_gpus))
  71. {
  72. if((sender_nworkers - nworkers_needed) >= sender_config->min_nworkers)
  73. {
  74. if((nworkers_ctx + nworkers_needed) > config->max_nworkers)
  75. nworkers_needed = nworkers_ctx > config->max_nworkers ? 0 : (config->max_nworkers - nworkers_ctx);
  76. if(nworkers_needed > 0)
  77. {
  78. int ngpus = nworkers_needed / 5;
  79. int *gpus;
  80. gpus = sc_hypervisor_get_idlest_workers(sender_sched_ctx, &ngpus, STARPU_CUDA_WORKER);
  81. int ncpus = nworkers_needed - ngpus;
  82. int *cpus;
  83. cpus = sc_hypervisor_get_idlest_workers(sender_sched_ctx, &ncpus, STARPU_CPU_WORKER);
  84. workers = (int*)malloc(nworkers_needed*sizeof(int));
  85. int i;
  86. printf("%d: gpus: ", nworkers_needed);
  87. for(i = 0; i < ngpus; i++)
  88. {
  89. workers[(*nworkers)++] = gpus[i];
  90. printf("%d ", gpus[i]);
  91. }
  92. printf(" cpus:");
  93. for(i = 0; i < ncpus; i++)
  94. {
  95. workers[(*nworkers)++] = cpus[i];
  96. printf("%d ", cpus[i]);
  97. }
  98. printf("\n");
  99. free(gpus);
  100. free(cpus);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. /*if the needed number of workers is to big we only move the number of workers
  107. corresponding to the granularity set by the user */
  108. int nworkers_to_move = sc_hypervisor_compute_nworkers_to_move(sender_sched_ctx);
  109. if(sender_nworkers - nworkers_to_move >= sender_config->min_nworkers)
  110. {
  111. int nshared_workers = (int)starpu_sched_ctx_get_nshared_workers(sender_sched_ctx, receiver_sched_ctx);
  112. if((nworkers_ctx + nworkers_to_move - nshared_workers) > config->max_nworkers)
  113. nworkers_to_move = nworkers_ctx > config->max_nworkers ? 0 : (config->max_nworkers - nworkers_ctx + nshared_workers);
  114. if(nworkers_to_move > 0)
  115. {
  116. workers = sc_hypervisor_get_idlest_workers(sender_sched_ctx, &nworkers_to_move, STARPU_ANY_WORKER);
  117. *nworkers = nworkers_to_move;
  118. }
  119. }
  120. }
  121. }
  122. return workers;
  123. }
  124. static unsigned _gflops_rate_resize(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, unsigned force_resize)
  125. {
  126. int ret = 1;
  127. if(force_resize)
  128. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  129. else
  130. ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  131. if(ret != EBUSY)
  132. {
  133. int nworkers_to_move = 0;
  134. int *workers_to_move = _get_workers_to_move(sender_sched_ctx, receiver_sched_ctx, &nworkers_to_move);
  135. if(nworkers_to_move > 0)
  136. {
  137. sc_hypervisor_move_workers(sender_sched_ctx, receiver_sched_ctx, workers_to_move, nworkers_to_move, 0);
  138. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(receiver_sched_ctx);
  139. int i;
  140. for(i = 0; i < nworkers_to_move; i++)
  141. 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;
  142. free(workers_to_move);
  143. }
  144. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static int _find_fastest_sched_ctx()
  150. {
  151. unsigned *sched_ctxs = sc_hypervisor_get_sched_ctxs();
  152. int nsched_ctxs = sc_hypervisor_get_nsched_ctxs();
  153. double first_exp_end = _get_exp_end(sched_ctxs[0]);
  154. int fastest_sched_ctx = first_exp_end == -1.0 ? -1 : (int)sched_ctxs[0];
  155. double curr_exp_end = 0.0;
  156. int i;
  157. for(i = 1; i < nsched_ctxs; i++)
  158. {
  159. curr_exp_end = _get_exp_end(sched_ctxs[i]);
  160. if((curr_exp_end < first_exp_end || first_exp_end == -1.0) && curr_exp_end != -1.0)
  161. {
  162. first_exp_end = curr_exp_end;
  163. fastest_sched_ctx = sched_ctxs[i];
  164. }
  165. }
  166. return fastest_sched_ctx;
  167. }
  168. static int _find_slowest_sched_ctx()
  169. {
  170. unsigned *sched_ctxs = sc_hypervisor_get_sched_ctxs();
  171. int nsched_ctxs = sc_hypervisor_get_nsched_ctxs();
  172. int slowest_sched_ctx = -1;
  173. double curr_exp_end = 0.0;
  174. double last_exp_end = -1.0;
  175. int i;
  176. for(i = 0; i < nsched_ctxs; i++)
  177. {
  178. curr_exp_end = _get_exp_end(sched_ctxs[i]);
  179. /*if it hasn't started bc of no ressources give it priority */
  180. if(curr_exp_end == -1.0)
  181. return sched_ctxs[i];
  182. if( curr_exp_end > last_exp_end)
  183. {
  184. slowest_sched_ctx = sched_ctxs[i];
  185. last_exp_end = curr_exp_end;
  186. }
  187. }
  188. return slowest_sched_ctx;
  189. }
  190. static int _find_slowest_available_sched_ctx(unsigned sched_ctx)
  191. {
  192. unsigned *sched_ctxs = sc_hypervisor_get_sched_ctxs();
  193. int nsched_ctxs = sc_hypervisor_get_nsched_ctxs();
  194. int slowest_sched_ctx = -1;
  195. double curr_exp_end = 0.0;
  196. double last_exp_end = -1.0;
  197. int i;
  198. for(i = 0; i < nsched_ctxs; i++)
  199. {
  200. if(sched_ctxs[i] != sched_ctx)
  201. {
  202. curr_exp_end = _get_exp_end(sched_ctxs[i]);
  203. /*if it hasn't started bc of no ressources give it priority */
  204. if(curr_exp_end == -1.0)
  205. return sched_ctxs[i];
  206. if(last_exp_end < curr_exp_end)
  207. {
  208. slowest_sched_ctx = sched_ctxs[i];
  209. last_exp_end = curr_exp_end;
  210. }
  211. }
  212. }
  213. return slowest_sched_ctx;
  214. }
  215. static void gflops_rate_resize(unsigned sched_ctx)
  216. {
  217. _get_exp_end(sched_ctx);
  218. double flops_left_pct = _get_flops_left_pct(sched_ctx);
  219. /* if the context finished all the instructions it had to execute
  220. we move all the resources to the slowest context */
  221. if(flops_left_pct == 0.0f)
  222. {
  223. int slowest_sched_ctx = _find_slowest_available_sched_ctx(sched_ctx);
  224. if(slowest_sched_ctx != -1)
  225. {
  226. double slowest_flops_left_pct = _get_flops_left_pct(slowest_sched_ctx);
  227. if(slowest_flops_left_pct != 0.0f)
  228. {
  229. struct sc_hypervisor_policy_config* config = sc_hypervisor_get_config(sched_ctx);
  230. config->min_nworkers = 0;
  231. config->max_nworkers = 0;
  232. printf("ctx %d finished & gives away the res to %d; slow_left %lf\n", sched_ctx, slowest_sched_ctx, slowest_flops_left_pct);
  233. sc_hypervisor_policy_resize(sched_ctx, slowest_sched_ctx, 1, 1);
  234. sc_hypervisor_stop_resize(slowest_sched_ctx);
  235. }
  236. }
  237. }
  238. int fastest_sched_ctx = _find_fastest_sched_ctx();
  239. int slowest_sched_ctx = _find_slowest_sched_ctx();
  240. if(fastest_sched_ctx != -1 && slowest_sched_ctx != -1 && fastest_sched_ctx != slowest_sched_ctx)
  241. {
  242. double fastest_exp_end = _get_exp_end(fastest_sched_ctx);
  243. double slowest_exp_end = _get_exp_end(slowest_sched_ctx);
  244. if((slowest_exp_end == -1.0 && fastest_exp_end != -1.0) || ((fastest_exp_end + (fastest_exp_end*0.5)) < slowest_exp_end ))
  245. {
  246. double fast_flops_left_pct = _get_flops_left_pct(fastest_sched_ctx);
  247. if(fast_flops_left_pct < 0.8)
  248. {
  249. struct sc_hypervisor_wrapper *sc_w = sc_hypervisor_get_wrapper(slowest_sched_ctx);
  250. double elapsed_flops = sc_hypervisor_get_elapsed_flops_per_sched_ctx(sc_w);
  251. if((elapsed_flops/sc_w->total_flops) > 0.1)
  252. _gflops_rate_resize(fastest_sched_ctx, slowest_sched_ctx, 0);
  253. }
  254. }
  255. }
  256. }
  257. static void gflops_rate_handle_poped_task(unsigned sched_ctx, __attribute__((unused)) int worker,
  258. __attribute__((unused))struct starpu_task *task, __attribute__((unused))uint32_t footprint)
  259. {
  260. gflops_rate_resize(sched_ctx);
  261. }
  262. struct sc_hypervisor_policy gflops_rate_policy = {
  263. .size_ctxs = NULL,
  264. .resize_ctxs = NULL,
  265. .handle_poped_task = gflops_rate_handle_poped_task,
  266. .handle_pushed_task = NULL,
  267. .handle_idle_cycle = NULL,
  268. .handle_idle_end = NULL,
  269. .handle_post_exec_hook = NULL,
  270. .handle_submitted_job = NULL,
  271. .end_ctx = NULL,
  272. .custom = 0,
  273. .name = "gflops_rate"
  274. };