teft_lp_policy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. }
  104. for(i = 0; i < nw; i++)
  105. free(tasks[i]);
  106. free(tasks);
  107. }
  108. static void size_if_required()
  109. {
  110. int nsched_ctxs, nworkers;
  111. unsigned *sched_ctxs;
  112. int *workers;
  113. unsigned has_req = sc_hypervisor_get_size_req(&sched_ctxs, &nsched_ctxs, &workers, &nworkers);
  114. if(has_req)
  115. {
  116. struct sc_hypervisor_wrapper* sc_w = NULL;
  117. unsigned ready_to_size = 1;
  118. int s;
  119. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  120. for(s = 0; s < nsched_ctxs; s++)
  121. {
  122. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  123. // if(sc_w->submitted_flops < sc_w->total_flops)
  124. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  125. ready_to_size = 0;
  126. }
  127. if(ready_to_size)
  128. {
  129. _size_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  130. sc_hypervisor_free_size_req();
  131. }
  132. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  133. }
  134. }
  135. static void teft_lp_handle_submitted_job(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  136. {
  137. /* count the tasks of the same type */
  138. starpu_pthread_mutex_lock(&mutex);
  139. sc_hypervisor_policy_add_task_to_pool(cl, sched_ctx, footprint, &task_pools, data_size);
  140. starpu_pthread_mutex_unlock(&mutex);
  141. size_if_required();
  142. }
  143. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  144. {
  145. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  146. if(ns < 2) return;
  147. int nw = workers == NULL ? (int)starpu_worker_get_count() : nworkers; /* Number of different workers */
  148. sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  149. int nt = 0; /* Number of different kinds of tasks */
  150. // starpu_pthread_mutex_lock(&mutex);
  151. /* we don't take the mutex bc a correct value of the number of tasks is
  152. not required but we do a copy in order to be sure
  153. that the linear progr won't segfault if the list of
  154. submitted task will change during the exec */
  155. struct sc_hypervisor_policy_task_pool *tp = NULL;
  156. struct sc_hypervisor_policy_task_pool *tmp_task_pools = sc_hypervisor_policy_clone_task_pool(task_pools);
  157. for (tp = task_pools; tp; tp = tp->next)
  158. nt++;
  159. double w_in_s[ns][nw];
  160. double **tasks_per_worker=(double**)malloc(nw*sizeof(double*));
  161. int i;
  162. for(i = 0; i < nw; i++)
  163. tasks_per_worker[i] = (double*)malloc(nt*sizeof(double));
  164. struct teft_lp_data specific_data;
  165. specific_data.nt = nt;
  166. specific_data.tasks = tasks_per_worker;
  167. specific_data.in_sched_ctxs = NULL;
  168. specific_data.workers = NULL;
  169. specific_data.tmp_task_pools = tmp_task_pools;
  170. specific_data.size_ctxs = 0;
  171. /* smallest possible tmax, difficult to obtain as we
  172. compute the nr of flops and not the tasks */
  173. /*lp computes it in s but it's converted to ms just before return */
  174. double possible_tmax = sc_hypervisor_lp_get_tmax(nw, NULL);
  175. double smallest_tmax = possible_tmax/2.0;
  176. double tmax = possible_tmax + smallest_tmax;
  177. double tmin = smallest_tmax;
  178. unsigned found_sol = 0;
  179. if(nt > 0 && tmax > 0.0)
  180. {
  181. struct timeval start_time;
  182. struct timeval end_time;
  183. gettimeofday(&start_time, NULL);
  184. found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  185. tmin, tmax, smallest_tmax, _compute_workers_distrib);
  186. gettimeofday(&end_time, NULL);
  187. long diff_s = end_time.tv_sec - start_time.tv_sec;
  188. long diff_us = end_time.tv_usec - start_time.tv_usec;
  189. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000.0;
  190. }
  191. // starpu_pthread_mutex_unlock(&mutex);
  192. /* if we did find at least one solution redistribute the resources */
  193. if(found_sol)
  194. {
  195. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, nw);
  196. sc_hypervisor_lp_place_resources_in_ctx(ns, nw, w_in_s, sched_ctxs, workers, 0, tw);
  197. }
  198. struct sc_hypervisor_policy_task_pool *next = NULL;
  199. struct sc_hypervisor_policy_task_pool *tmp_tp = tmp_task_pools;
  200. while(tmp_task_pools)
  201. {
  202. next = tmp_tp->next;
  203. free(tmp_tp);
  204. tmp_tp = next;
  205. tmp_task_pools = next;
  206. }
  207. for(i = 0; i < nw; i++)
  208. free(tasks_per_worker[i]);
  209. free(tasks_per_worker);
  210. }
  211. static void teft_lp_handle_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, uint32_t footprint)
  212. {
  213. if(worker > -2)
  214. {
  215. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  216. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  217. if(ret != EBUSY)
  218. {
  219. unsigned criteria = sc_hypervisor_get_resize_criteria();
  220. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  221. {
  222. if(sc_hypervisor_check_speed_gap_btw_ctxs(NULL, -1, NULL, -1))
  223. {
  224. _try_resizing(NULL, -1, NULL, -1);
  225. }
  226. }
  227. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  228. }
  229. }
  230. /* too expensive to take this mutex and correct value of the number of tasks is not compulsory */
  231. starpu_pthread_mutex_lock(&mutex);
  232. sc_hypervisor_policy_remove_task_from_pool(task, footprint, &task_pools);
  233. starpu_pthread_mutex_unlock(&mutex);
  234. }
  235. static void teft_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  236. {
  237. unsigned criteria = sc_hypervisor_get_resize_criteria();
  238. if(criteria != SC_NOTHING)// && criteria == SC_IDLE)
  239. {
  240. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  241. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  242. if(ret != EBUSY)
  243. {
  244. _try_resizing(NULL, -1, NULL, -1);
  245. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  246. }
  247. }
  248. return;
  249. }
  250. static void teft_lp_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  251. {
  252. sc_hypervisor_save_size_req(sched_ctxs, nsched_ctxs, workers, nworkers);
  253. }
  254. static void teft_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  255. {
  256. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  257. if(ret != EBUSY)
  258. {
  259. struct sc_hypervisor_wrapper* sc_w = NULL;
  260. int s = 0;
  261. for(s = 0; s < nsched_ctxs; s++)
  262. {
  263. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  264. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  265. {
  266. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  267. return;
  268. }
  269. }
  270. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  271. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  272. }
  273. }
  274. struct sc_hypervisor_policy teft_lp_policy = {
  275. .size_ctxs = teft_lp_size_ctxs,
  276. .resize_ctxs = teft_lp_resize_ctxs,
  277. .handle_poped_task = teft_lp_handle_poped_task,
  278. .handle_pushed_task = NULL,
  279. .handle_idle_cycle = teft_lp_handle_idle_cycle,
  280. .handle_idle_end = NULL,
  281. .handle_post_exec_hook = NULL,
  282. .handle_submitted_job = teft_lp_handle_submitted_job,
  283. .end_ctx = NULL,
  284. .init_worker = NULL,
  285. .custom = 0,
  286. .name = "teft_lp"
  287. };