teft_lp_policy.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. int *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. int *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. sc_hypervisor_get_tasks_times(nw, nt, times, workers, size_ctxs, task_pools);
  48. double res = 0.0;
  49. #ifdef STARPU_HAVE_GLPK_H
  50. res = sc_hypervisor_lp_simulate_distrib_tasks(ns, nw, nt, w_in_s, tasks, times, is_integer, tmax, in_sched_ctxs, tmp_task_pools);
  51. #endif //STARPU_HAVE_GLPK_H
  52. if(res != 0.0)
  53. {
  54. int s, w, t;
  55. for(s = 0; s < ns; s++)
  56. for(w = 0; w < nw; w++)
  57. final_w_in_s[s][w] = w_in_s[s][w];
  58. for(w = 0; w < nw; w++)
  59. for(t = 0; t < nt; t++)
  60. final_tasks[w][t] = tasks[w][t];
  61. }
  62. return res;
  63. }
  64. static void _size_ctxs(int *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  65. {
  66. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  67. int nw = workers == NULL ? (int)starpu_worker_get_count() : nworkers; /* Number of different workers */
  68. int nt = 0; /* Number of different kinds of tasks */
  69. starpu_pthread_mutex_lock(&mutex);
  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[nw][nt];
  75. double **tasks=(double**)malloc(nw*sizeof(double*));
  76. int i;
  77. for(i = 0; i < nw; i++)
  78. tasks[i] = (double*)malloc(nt*sizeof(double));
  79. struct teft_lp_data specific_data;
  80. specific_data.nt = nt;
  81. specific_data.tasks = tasks;
  82. specific_data.in_sched_ctxs = sched_ctxs;
  83. specific_data.workers = workers;
  84. specific_data.tmp_task_pools = task_pools;
  85. specific_data.size_ctxs = 1;
  86. /* smallest possible tmax, difficult to obtain as we
  87. compute the nr of flops and not the tasks */
  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 = smallest_tmax;
  92. unsigned found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  93. tmin, tmax, smallest_tmax, _compute_workers_distrib);
  94. starpu_pthread_mutex_unlock(&mutex);
  95. /* if we did find at least one solution redistribute the resources */
  96. if(found_sol)
  97. sc_hypervisor_lp_place_resources_in_ctx(ns, nw, w_in_s, sched_ctxs, workers, 1);
  98. for(i = 0; i < nw; i++)
  99. free(tasks[i]);
  100. free(tasks);
  101. }
  102. static void size_if_required()
  103. {
  104. int nsched_ctxs, nworkers;
  105. int *sched_ctxs, *workers;
  106. unsigned has_req = sc_hypervisor_get_size_req(&sched_ctxs, &nsched_ctxs, &workers, &nworkers);
  107. if(has_req)
  108. {
  109. struct sc_hypervisor_wrapper* sc_w = NULL;
  110. unsigned ready_to_size = 1;
  111. int s;
  112. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  113. for(s = 0; s < nsched_ctxs; s++)
  114. {
  115. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  116. if(sc_w->submitted_flops < sc_w->total_flops)
  117. ready_to_size = 0;
  118. }
  119. if(ready_to_size)
  120. _size_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  121. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  122. }
  123. }
  124. static void teft_lp_handle_submitted_job(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint)
  125. {
  126. /* count the tasks of the same type */
  127. starpu_pthread_mutex_lock(&mutex);
  128. sc_hypervisor_policy_add_task_to_pool(cl, sched_ctx, footprint, &task_pools);
  129. starpu_pthread_mutex_unlock(&mutex);
  130. size_if_required();
  131. }
  132. static void teft_lp_handle_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, uint32_t footprint)
  133. {
  134. struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx);
  135. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  136. if(ret != EBUSY)
  137. {
  138. if(sc_w->submitted_flops < sc_w->total_flops)
  139. {
  140. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  141. return;
  142. }
  143. if(sc_hypervisor_criteria_fulfilled(sched_ctx, worker))
  144. {
  145. int ns = sc_hypervisor_get_nsched_ctxs();
  146. int nw = starpu_worker_get_count(); /* Number of different workers */
  147. int nt = 0; /* Number of different kinds of tasks */
  148. // starpu_pthread_mutex_lock(&mutex);
  149. /* we don't take the mutex bc a correct value of the number of tasks is
  150. not required but we do a copy in order to be sure
  151. that the linear progr won't segfault if the list of
  152. submitted task will change during the exec */
  153. struct sc_hypervisor_policy_task_pool *tp = NULL;
  154. struct sc_hypervisor_policy_task_pool *tmp_task_pools = sc_hypervisor_policy_clone_task_pool(task_pools);
  155. for (tp = task_pools; tp; tp = tp->next)
  156. nt++;
  157. double w_in_s[ns][nw];
  158. // double tasks_per_worker[nw][nt];
  159. double **tasks_per_worker=(double**)malloc(nw*sizeof(double*));
  160. int i;
  161. for(i = 0; i < nw; i++)
  162. tasks_per_worker[i] = (double*)malloc(nt*sizeof(double));
  163. struct teft_lp_data specific_data;
  164. specific_data.nt = nt;
  165. specific_data.tasks = tasks_per_worker;
  166. specific_data.in_sched_ctxs = NULL;
  167. specific_data.workers = NULL;
  168. specific_data.tmp_task_pools = tmp_task_pools;
  169. specific_data.size_ctxs = 0;
  170. /* smallest possible tmax, difficult to obtain as we
  171. compute the nr of flops and not the tasks */
  172. double possible_tmax = sc_hypervisor_lp_get_tmax(nw, NULL);
  173. double smallest_tmax = possible_tmax / 3;
  174. double tmax = possible_tmax * ns;
  175. double tmin = smallest_tmax;
  176. unsigned found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  177. tmin, tmax, smallest_tmax, _compute_workers_distrib);
  178. // starpu_pthread_mutex_unlock(&mutex);
  179. /* if we did find at least one solution redistribute the resources */
  180. if(found_sol)
  181. sc_hypervisor_lp_place_resources_in_ctx(ns, nw, w_in_s, NULL, NULL, 0);
  182. struct sc_hypervisor_policy_task_pool *next = NULL;
  183. struct sc_hypervisor_policy_task_pool *tmp_tp = tmp_task_pools;
  184. while(tmp_task_pools)
  185. {
  186. next = tmp_tp->next;
  187. free(tmp_tp);
  188. tmp_tp = next;
  189. tmp_task_pools = next;
  190. }
  191. for(i = 0; i < nw; i++)
  192. free(tasks_per_worker[i]);
  193. free(tasks_per_worker);
  194. }
  195. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  196. }
  197. /* too expensive to take this mutex and correct value of the number of tasks is not compulsory */
  198. // starpu_pthread_mutex_lock(&mutex);
  199. sc_hypervisor_policy_remove_task_from_pool(task, footprint, &task_pools);
  200. // starpu_pthread_mutex_unlock(&mutex);
  201. }
  202. static void teft_lp_size_ctxs(int *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  203. {
  204. sc_hypervisor_save_size_req(sched_ctxs, nsched_ctxs, workers, nworkers);
  205. }
  206. struct sc_hypervisor_policy teft_lp_policy = {
  207. .size_ctxs = teft_lp_size_ctxs,
  208. .handle_poped_task = teft_lp_handle_poped_task,
  209. .handle_pushed_task = NULL,
  210. .handle_idle_cycle = NULL,
  211. .handle_idle_end = NULL,
  212. .handle_post_exec_hook = NULL,
  213. .handle_submitted_job = teft_lp_handle_submitted_job,
  214. .end_ctx = NULL,
  215. .custom = 0,
  216. .name = "teft_lp"
  217. };