teft_lp_policy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <starpu_config.h>
  17. #include "sc_hypervisor_lp.h"
  18. #include "sc_hypervisor_policy.h"
  19. #include <math.h>
  20. #include <sys/time.h>
  21. static struct sc_hypervisor_policy_task_pool *task_pools = NULL;
  22. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  23. struct teft_lp_data
  24. {
  25. int nt;
  26. double **tasks;
  27. unsigned *in_sched_ctxs;
  28. int *workers;
  29. struct sc_hypervisor_policy_task_pool *tmp_task_pools;
  30. unsigned size_ctxs;
  31. };
  32. static double _compute_workers_distrib(int ns, int nw, double final_w_in_s[ns][nw],
  33. unsigned is_integer, double tmax, void *specific_data)
  34. {
  35. struct teft_lp_data *sd = (struct teft_lp_data *)specific_data;
  36. int nt = sd->nt;
  37. double **final_tasks = sd->tasks;
  38. unsigned *in_sched_ctxs = sd->in_sched_ctxs;
  39. int *workers = sd->workers;
  40. struct sc_hypervisor_policy_task_pool *tmp_task_pools = sd->tmp_task_pools;
  41. unsigned size_ctxs = sd->size_ctxs;
  42. if(tmp_task_pools == NULL)
  43. return 0.0;
  44. double w_in_s[ns][nw];
  45. double tasks[nw][nt];
  46. double times[nw][nt];
  47. /* times in ms */
  48. sc_hypervisor_get_tasks_times(nw, nt, times, workers, size_ctxs, task_pools);
  49. double res = 0.0;
  50. #ifdef STARPU_HAVE_GLPK_H
  51. res = sc_hypervisor_lp_simulate_distrib_tasks(ns, nw, nt, w_in_s, tasks, times, is_integer, tmax, in_sched_ctxs, tmp_task_pools);
  52. #endif //STARPU_HAVE_GLPK_H
  53. if(res != 0.0)
  54. {
  55. int s, w, t;
  56. for(s = 0; s < ns; s++)
  57. for(w = 0; w < nw; w++)
  58. final_w_in_s[s][w] = w_in_s[s][w];
  59. for(w = 0; w < nw; w++)
  60. for(t = 0; t < nt; t++)
  61. final_tasks[w][t] = tasks[w][t];
  62. }
  63. return res;
  64. }
  65. static void _size_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  66. {
  67. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  68. int nw = workers == NULL ? (int)starpu_worker_get_count() : nworkers; /* Number of different workers */
  69. int nt = 0; /* Number of different kinds of tasks */
  70. struct sc_hypervisor_policy_task_pool * tp;
  71. for (tp = task_pools; tp; tp = tp->next)
  72. nt++;
  73. double w_in_s[ns][nw];
  74. double **tasks=(double**)malloc(nw*sizeof(double*));
  75. int i;
  76. for(i = 0; i < nw; i++)
  77. tasks[i] = (double*)malloc(nt*sizeof(double));
  78. struct teft_lp_data specific_data;
  79. specific_data.nt = nt;
  80. specific_data.tasks = tasks;
  81. specific_data.in_sched_ctxs = sched_ctxs;
  82. specific_data.workers = workers;
  83. specific_data.tmp_task_pools = task_pools;
  84. specific_data.size_ctxs = 1;
  85. /* smallest possible tmax, difficult to obtain as we
  86. compute the nr of flops and not the tasks */
  87. /*lp computes it in s but it's converted to ms just before return */
  88. double possible_tmax = sc_hypervisor_lp_get_tmax(nw, workers);
  89. double smallest_tmax = possible_tmax / 3;
  90. double tmax = possible_tmax * ns;
  91. double tmin = 0.0;
  92. unsigned found_sol = 0;
  93. if(nt > 0 && tmax > 0.0)
  94. {
  95. found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  96. tmin, tmax, smallest_tmax, _compute_workers_distrib);
  97. }
  98. /* if we did find at least one solution redistribute the resources */
  99. if(found_sol)
  100. {
  101. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, nw);
  102. sc_hypervisor_lp_place_resources_in_ctx(ns, nw, w_in_s, sched_ctxs, workers, 1, tw);
  103. free(tw);
  104. }
  105. for(i = 0; i < nw; i++)
  106. free(tasks[i]);
  107. free(tasks);
  108. }
  109. static void size_if_required()
  110. {
  111. int nsched_ctxs, nworkers;
  112. unsigned *sched_ctxs;
  113. int *workers;
  114. unsigned has_req = sc_hypervisor_get_size_req(&sched_ctxs, &nsched_ctxs, &workers, &nworkers);
  115. if(has_req)
  116. {
  117. struct sc_hypervisor_wrapper* sc_w = NULL;
  118. unsigned ready_to_size = 1;
  119. int s;
  120. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  121. for(s = 0; s < nsched_ctxs; s++)
  122. {
  123. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  124. // if(sc_w->submitted_flops < sc_w->total_flops)
  125. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  126. ready_to_size = 0;
  127. }
  128. if(ready_to_size)
  129. {
  130. _size_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  131. sc_hypervisor_free_size_req();
  132. }
  133. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  134. }
  135. }
  136. static void teft_lp_handle_submitted_job(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  137. {
  138. /* count the tasks of the same type */
  139. starpu_pthread_mutex_lock(&mutex);
  140. sc_hypervisor_policy_add_task_to_pool(cl, sched_ctx, footprint, &task_pools, data_size);
  141. starpu_pthread_mutex_unlock(&mutex);
  142. size_if_required();
  143. }
  144. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  145. {
  146. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  147. if(ns < 2) return;
  148. int nw = workers == NULL ? (int)starpu_worker_get_count() : nworkers; /* Number of different workers */
  149. sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  150. int nt = 0; /* Number of different kinds of tasks */
  151. // starpu_pthread_mutex_lock(&mutex);
  152. /* we don't take the mutex bc a correct value of the number of tasks is
  153. not required but we do a copy in order to be sure
  154. that the linear progr won't segfault if the list of
  155. submitted task will change during the exec */
  156. struct sc_hypervisor_policy_task_pool *tp = NULL;
  157. struct sc_hypervisor_policy_task_pool *tmp_task_pools = sc_hypervisor_policy_clone_task_pool(task_pools);
  158. for (tp = task_pools; tp; tp = tp->next)
  159. nt++;
  160. double w_in_s[ns][nw];
  161. double **tasks_per_worker=(double**)malloc(nw*sizeof(double*));
  162. int i;
  163. for(i = 0; i < nw; i++)
  164. tasks_per_worker[i] = (double*)malloc(nt*sizeof(double));
  165. struct teft_lp_data specific_data;
  166. specific_data.nt = nt;
  167. specific_data.tasks = tasks_per_worker;
  168. specific_data.in_sched_ctxs = NULL;
  169. specific_data.workers = NULL;
  170. specific_data.tmp_task_pools = tmp_task_pools;
  171. specific_data.size_ctxs = 0;
  172. /* smallest possible tmax, difficult to obtain as we
  173. compute the nr of flops and not the tasks */
  174. /*lp computes it in s but it's converted to ms just before return */
  175. double possible_tmax = sc_hypervisor_lp_get_tmax(nw, NULL);
  176. double smallest_tmax = possible_tmax/2.0;
  177. double tmax = possible_tmax + smallest_tmax;
  178. double tmin = smallest_tmax;
  179. unsigned found_sol = 0;
  180. if(nt > 0 && tmax > 0.0)
  181. {
  182. struct timeval start_time;
  183. struct timeval end_time;
  184. gettimeofday(&start_time, NULL);
  185. found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  186. tmin, tmax, smallest_tmax, _compute_workers_distrib);
  187. gettimeofday(&end_time, NULL);
  188. long diff_s = end_time.tv_sec - start_time.tv_sec;
  189. long diff_us = end_time.tv_usec - start_time.tv_usec;
  190. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000.0;
  191. }
  192. // starpu_pthread_mutex_unlock(&mutex);
  193. /* if we did find at least one solution redistribute the resources */
  194. if(found_sol)
  195. {
  196. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, nw);
  197. sc_hypervisor_lp_place_resources_in_ctx(ns, nw, w_in_s, sched_ctxs, workers, 0, tw);
  198. free(tw);
  199. }
  200. struct sc_hypervisor_policy_task_pool *next = NULL;
  201. struct sc_hypervisor_policy_task_pool *tmp_tp = tmp_task_pools;
  202. while(tmp_task_pools)
  203. {
  204. next = tmp_tp->next;
  205. free(tmp_tp);
  206. tmp_tp = next;
  207. tmp_task_pools = next;
  208. }
  209. for(i = 0; i < nw; i++)
  210. free(tasks_per_worker[i]);
  211. free(tasks_per_worker);
  212. }
  213. static void teft_lp_handle_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, uint32_t footprint)
  214. {
  215. if(worker > -2)
  216. {
  217. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  218. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  219. if(ret != EBUSY)
  220. {
  221. unsigned criteria = sc_hypervisor_get_resize_criteria();
  222. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  223. {
  224. if(sc_hypervisor_check_speed_gap_btw_ctxs(NULL, -1, NULL, -1))
  225. {
  226. _try_resizing(NULL, -1, NULL, -1);
  227. }
  228. }
  229. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  230. }
  231. }
  232. /* too expensive to take this mutex and correct value of the number of tasks is not compulsory */
  233. starpu_pthread_mutex_lock(&mutex);
  234. sc_hypervisor_policy_remove_task_from_pool(task, footprint, &task_pools);
  235. starpu_pthread_mutex_unlock(&mutex);
  236. }
  237. static void teft_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  238. {
  239. unsigned criteria = sc_hypervisor_get_resize_criteria();
  240. if(criteria != SC_NOTHING)// && criteria == SC_IDLE)
  241. {
  242. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  243. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  244. if(ret != EBUSY)
  245. {
  246. _try_resizing(NULL, -1, NULL, -1);
  247. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  248. }
  249. }
  250. return;
  251. }
  252. static void teft_lp_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  253. {
  254. sc_hypervisor_save_size_req(sched_ctxs, nsched_ctxs, workers, nworkers);
  255. }
  256. static void teft_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  257. {
  258. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  259. if(ret != EBUSY)
  260. {
  261. struct sc_hypervisor_wrapper* sc_w = NULL;
  262. int s = 0;
  263. for(s = 0; s < nsched_ctxs; s++)
  264. {
  265. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  266. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  267. {
  268. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  269. return;
  270. }
  271. }
  272. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  273. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  274. }
  275. }
  276. struct sc_hypervisor_policy teft_lp_policy = {
  277. .size_ctxs = teft_lp_size_ctxs,
  278. .resize_ctxs = teft_lp_resize_ctxs,
  279. .handle_poped_task = teft_lp_handle_poped_task,
  280. .handle_pushed_task = NULL,
  281. .handle_idle_cycle = teft_lp_handle_idle_cycle,
  282. .handle_idle_end = NULL,
  283. .handle_post_exec_hook = NULL,
  284. .handle_submitted_job = teft_lp_handle_submitted_job,
  285. .end_ctx = NULL,
  286. .init_worker = NULL,
  287. .custom = 0,
  288. .name = "teft_lp"
  289. };